The Car Class

public class Car {
    public static void main(String[] args) {
        int year = 2023;
        double price = 30000.00;
        boolean isElectric = true;
        
        System.out.println("Year: " + year);
        System.out.println("Price: $" + price);
        System.out.println("Is Electric: " + isElectric);
    }
}

Arithmetic Operators

import java.util.Scanner;

public class CarPrice {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the price of the car: ");
        double price = scanner.nextDouble();

        System.out.println("The price of the car is $" + price);

        scanner.close();
    }
}

Compound Operators

int cars = 0; // starting number of cars
int numCarsEntering = 2; // number of cars entering at a time
int numIterations = 5; // number of times to loop

for (int i = 0; i < numIterations; i++) {
    cars += numCarsEntering; // use compound operator to add numCarsEntering to cars
    System.out.println("Number of cars on highway: " + cars);
}

Hack 1

Make a code segment that calculates the number of wheels of x number of cars

import java.util.Scanner;

public class Hack1 {
    public static void main(String[] args) {
        
        //calculate wheels
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of cars: ");
        int num = scanner.nextInt();
        System.out.print(num+"\n");

        int wheels = num * 4;

        System.out.println("The number of wheels is: " + wheels);

        scanner.close();

    }
}
Hack1.main(null);
Enter the number of cars: 32
The number of wheels is: 128

Hack 2

public class Car {

    public static void main(String[] args) {
        String[] make = {"Toyota", "Honda", "Ford"};
        String[] model = {"Corolla", "Accord", "Mustang"};
        int[] year = {2022, 2018, 2020, 2018};
        double[] price = {24999.99, 18999.99, 34999.99};
        boolean[] isUsed = {false, true, true};

        for(int i = 0; i < make.length; i++) { 
            System.out.println("Make: "+make[i]+", Model: "+model[i]+", Year: "+year[i]+", Price: "+price[i]+", Used? "+isUsed[i]);
        }

    }

}
Car.main(null);
Make: Toyota, Model: Corolla, Year: 2022, Price: 24999.99, Used? false
Make: Honda, Model: Accord, Year: 2018, Price: 18999.99, Used? true
Make: Ford, Model: Mustang, Year: 2020, Price: 34999.99, Used? true

Boolean Expressions

boolean isEngineRunning = true;
int speed = 60;
boolean isSpeedAboveThreshold = speed > 50;

if (isEngineRunning && isSpeedAboveThreshold) {
    System.out.println("The car is moving fast!");
} else {
    System.out.println("The car is not moving fast.");
}
The car is moving fast!

Hack 3

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
        System.out.println("Car Details:");
        System.out.println(car1.getMake());
        System.out.println(car1.getModel());
        System.out.println(car1.getYear());
        System.out.println(car1.getPrice()+"\n");
        
        // Check if the car is affordable with a budget of $20000 using an if-else statement
       if (car1.isAffordable(20000.0)) {
        System.out.println("The car is affordable!\n");
       } else {
        System.out.println("The car is not affordable!\n");
       }
        // Check if the car is a luxury car based on its price using if-else-if statement
       if (car1.price > 50000) {
        System.out.println("The car is a luxury car.");
       } else {
        System.out.println("The car is not a luxury car.");
       }

    }
}
Car.main(null);
Car Details:
Toyota
Camry
2019
25000.0

The car is not affordable!

The car is not a luxury car.

Hack 4

You are tasked with creating a program that allows a user to enter the number of cars they own, and then input the make and model of each car. The program will then print out the list of cars.

Here are the specific requirements:

  • Use a try-catch statement to catch any input/output exceptions that may occur.
  • Use a while loop to ensure that the user enters a positive integer for the number of cars they own.
  • Use a for loop to iterate over each car and prompt the user to enter the make and model of the car.
import java.util.Scanner;

public class Car {
    private String make;
    private String model;
    private static String customMake;
    private static String customModel;
    
    // Constructor
    public Car(String make, String model) {
        this.make = make;
        this.model = model;
    }
    
    // Getter methods
    public String getMake() {
        return make;
    }
    
    public String getModel() {
        return model;
    }
    
    
    // Main method
    public static void main(String[] args) {
        
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter the number of cars you own: ");
        int num = scanner.nextInt();
        System.out.println("You own "+num+" cars!\n");

        System.out.println("Let's make a list!\n");
        String yes = scanner.nextLine();

        while (num > 0) {
            String carList[][] = new String[num][2];
            for (int i = 0; i < num; i++) {
                Car customCar = new Car(customMake, customModel);
                
                System.out.println("Enter the make of your car: ");
                String customMake = scanner.nextLine();
                System.out.println("Enter the model of your car: ");
                String customModel = scanner.nextLine();
                System.out.println("You own a "+customMake+" "+customModel+"!\n");

                carList[i][0] = customMake;
                carList[i][1] = customModel;
            }

            scanner.close();

            for(int j = 0; j < carList.length; j++) {
                System.out.print("Car #"+(j+1)+": ");
                for(int k = 0; k < carList[0].length; k++) {
                    System.out.print(carList[j][k] + " ");
                }
                System.out.println(" ");
            }

            num = 0;
        }


    }
}
Car.main(null);
Enter the number of cars you own: 
You own 2 cars!

Let's make a list!

Enter the make of your car: 
Enter the model of your car: 
You own a Ford Mustang!

Enter the make of your car: 
Enter the model of your car: 
You own a Hyundai Palisade!

Car #1: Ford Mustang  
Car #2: Hyundai Palisade  

Extra Credit

public class CarLab { // CarLab is a class that represents a car

    private String name;

    // Hack: add columns for other attributes, (ex total mileage, model, make of car) demonstrating knowledge of different data types

    private double price;
    private String make;
    private String model;
    private int mpg;
    
    public CarLab(String name, double price, String make, String model, int mpg) {
        this.name = name;
        this.price = price;
        this.make = make;
        this.model = model;
        this.mpg = mpg;
    }

    public CarLab(String name, int mpg) {
        this.name = name;
        this.mpg = mpg;
    }

    public CarLab(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public CarLab(String name, double price, int mpg) {
        this.name = name;
        this.price = price;
        this.mpg = mpg;
    }

    public double getPrice() {
        return price;
    }

    public String getName() {
        return name;
    }

    public String getMake() {
        return make;
    }
    
    public String getModel() {
        return model;
    }

    public int getMpg() {
        return mpg;
    }

    public static String cheaperCar(String car1Name, double car1Price, String car2Name, double car2Price) {
        CarLab car1 = new CarLab(car1Name, car1Price);
        CarLab car2 = new CarLab(car2Name, car2Price);
        
        if (car1.getPrice() < car2.getPrice()) {
            return car1.getName();
        } else {
            return car2.getName();
        }
    }

    public static boolean isSustainable(String car5Name, int car5Mpg) {
        CarLab car5 = new CarLab(car5Name, car5Mpg);
        return (car5.getMpg() > 40);
    }

    public static String cheaperSustainableCar(String car3Name, double car3Price, int car3Mpg, String car4Name, double car4Price, int car4Mpg) {
        CarLab car3 = new CarLab(car3Name, car3Price, car3Mpg);
        CarLab car4 = new CarLab(car4Name, car4Price, car4Mpg);
            if (car3.getPrice() < car4.getPrice()) {
                if (car3.getMpg() < car4.getMpg()) {
                    return (car3.getName() + " is cheaper but less sustainable!");
                } else {
                    return (car3.getName() + " is cheaper and more sustainable!");
                }
            } else {
                if (car3.getMpg() < car4.getMpg()) {
                    return (car4.getName() + " is cheaper and more sustainable!");
                } else {
                    return (car4.getName() + " is cheaper but less sustainable!");
                }
            }
    }
    
    public static String cheaperCar(CarLab car1, CarLab car2) { // Overloading the cheaperCar method to allow both String and CarLab objects to be passed in
        if (car1.getPrice() < car2.getPrice()) {
            return car1.getName();
        } else {
            return car2.getName();
        }
    }

    // Hack: demonstrate another operation on a different data type

    public static int acceleration(double s1, double s2, double time) {
        return (Math.abs((int) ((s2 - s1) / time)));
    }

    public String toString() {
        return( "{" + 
            "\"name\": " + name + "," +
            "\"price\": " +  price +
            "}" );
    }

    public static void main(String[] args) {
        String cheaperCarName = cheaperCar("Toyota", 20000.0, "Honda", 25000.0);
        System.out.println("The cheaper car is: " + cheaperCarName);

        System.out.println("The acceleration is: " + acceleration(0, 60, 10) + " m/s^2");

        System.out.println("The car is sustainable: " + isSustainable("Palisade", 60));

        String cheaperSustainableCar = cheaperSustainableCar("Toyota", 20000.0, 56, "Honda", 25000.0, 43);
        System.out.println("The cheaper car with consideration to sustainability: " + cheaperSustainableCar);
    }
}
CarLab.main(null);
The cheaper car is: Toyota
The acceleration is: 6 m/s^2
The car is sustainable: true
The cheaper car with consideration to sustainability: Toyota is cheaper and more sustainable!