ABopsC Lesson and Hacks
public class Hack1 {
int x; // x is number of cars
public static void main(String[] args) {
int wheels; // counting # of wheels
int x = 4;
wheels = 4 * x; // x is # of cars, 4 wheels per car
System.out.println(wheels);
}
}
// hack 2
public class Car {
private String make;
private String model;
private int year;
private double price;
public Car(String make, String model, int year, double price) {
this.make = make;
this.model = model;
this.year = year;
this.price = price;
}
public String getMake() {
return make;
}
public String getModel() {
return model;
}
public int getYear() {
return year;
}
public double getPrice() {
return price;
}
List<Car> listOfCars = new ArrayList<>();
// create constructor
public String printCarDetails() {
// method to print car details
for(Cars f : listOfCars){
f.getMake();
f.getModel();
f.getYear();
f.getPrice();
}
}
public static void main(String[] args) {
Car car1 = new Car("Toyota", "Corolla", 2022, 24999.99);
Car car2 = new Car("Honda", "Accord", 2018, 18999.99);
Car car3 = new Car("Ford", "Mustang", 2020, 34999.99);
listOfCars.add(car1);
listOfCars.add(car2);
listOfCars.add(car3);
listOfCars.printCarDetails();
}
}
public class Car {
private String make;
private String model;
private int year;
private double price;
// Constructor
public Car(String make, String model, int year, double price) {
this.make = make;
this.model = model;
this.year = year;
this.price = price;
}
// Getter methods
public String getMake() {
return make;
}
public String getModel() {
return model;
}
public int getYear() {
return year;
}
public double getPrice() {
return price;
}
// Method to determine if the car is affordable or not
public boolean isAffordable(double budget) {
if (price <= budget) {
return true;
} else {
return false;
}
}
// Main method
public static void main(String[] args) {
// Create a new Car object
Car car1 = new Car(“Toyota”, “Camry”, 2019, 25000.0);
// Print the car details
car1.getMake();
car1.getModel();
car1.getYear();
car1.getPrice();
// Check if the car is affordable with a budget of $20000 using an if-else statement
budget = 20000.0;
car1.isAffordable();
// Check if the car is a luxury car based on its price using if-else-if statement
if (price > 50000.0) {
System.out.println("The car is a luxury car.");
}
else if (price > 30000.0) {
System.out.println("The car is a mid-range car.");
}
else {
System.out.println("The car is an affordable car.");
}
}
}
Car.main(null);