Units 1-4 Hacks
// Hack 1
public class Hack1 {
public static void main(String[] args) {
int cars = 7;
int wheels = cars*4;
System.out.println("Numbers of wheels on " + cars + " cars" + " is " + wheels + " wheels");
}
}
Hack1.main(null);
// Hack 2
public class Car {
private String make;
private String model;
private int year;
private double price;
private boolean isUsed;
public Car(String make, String model, int year, double price, boolean isUsed) {
this.make = make;
this.model = model;
this.year = year;
this.price = price;
this.isUsed = isUsed;
}
public void printCarDetails() {
System.out.println("Make: " + make);
System.out.println("Model: " + model);
System.out.println("Year: " + year);
System.out.println("Price: $" + price);
System.out.println("Is used: " + isUsed);
System.out.println();
}
public static void main(String[] args) {
Car[] cars = new Car[3];
cars[0] = new Car("Toyota", "Corolla", 2022, 24999.99, false);
cars[1] = new Car("Honda", "Accord", 2018, 18999.99, true);
cars[2] = new Car("Ford", "Mustang", 2020, 34999.99, true);
for (Car car : cars) {
car.printCarDetails();
}
}
}
Car.main(null);
//Hack 3
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;
}
public boolean isAffordable(double budget) {
return price <= budget;
}
public static void main(String[] args) {
Car myCar = new Car("Toyota", "Camry", 2019, 25000.0);
System.out.println("Make: " + myCar.getMake());
System.out.println("Model: " + myCar.getModel());
System.out.println("Year: " + myCar.getYear());
System.out.println("Price: " + myCar.getPrice());
if (myCar.isAffordable(20000)) {
System.out.println("The car is affordable.");
} else {
System.out.println("The car is not affordable within a budget of $20000.");
}
if (myCar.getPrice() > 50000) {
System.out.println("The car is a luxury car.");
} else if (myCar.getPrice() > 30000) {
System.out.println("The car is a mid-range car.");
} else {
System.out.println("The car is an affordable car.");
}
}
}
Car.main(null);
//Hack 4 (do not run it runs forever)
import java.util.Scanner;
public class CarList {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int numCars = 0;
while (numCars <= 0) {
try {
System.out.print("Enter the number of cars you own: ");
numCars = scanner.nextInt();
if (numCars <= 0) {
System.out.println("Please enter a positive integer.");
}
} catch (Exception e) {
System.out.println("Invalid input. Please enter a positive integer.");
scanner.next(); // clear scanner buffer
}
}
String[] makes = new String[numCars];
String[] models = new String[numCars];
for (int i = 0; i < numCars; i++) {
System.out.println("Car " + (i + 1) + ":");
System.out.print("Make: ");
makes[i] = scanner.next();
System.out.print("Model: ");
models[i] = scanner.next();
}
System.out.println("Your cars:");
for (int i = 0; i < numCars; i++) {
System.out.println((i + 1) + ". " + makes[i] + " " + models[i]);
}
scanner.close();
}
}
// Extra Credit