Car Created FRQ

Part 1

Define 1 argument constructor using title

Define toString method for id and title

Create a public getter that has attributes of car

Define tester method that initializes at least 2 cars, date, and cost

public class Food {
    
    public String food; 
    static int orders = 0;
    protected double startTime; 
    private int id;
    //protected so it can be accessed through the extended classes

    public Food(String food) {
        this.food = food;
        orders++; //increases the orders since its static
        this.startTime = System.nanoTime(); //starts the time when the food is prepared
        this.id = orders; //unique id is the order 
    }

    public int getId() {
        return id;
    }

    public String toString() { //uses toString method to get the order
        return "Restaurant order: " + food + "\n Order Number: " + id;
    }

    public int getOrders() {
        return orders;
    }
    

    public double waitingTime() {
        return (System.nanoTime() - startTime);
    } //returns the waiting time after food is prepared to when its picked up

    public boolean goneBad() {
        return (this.waitingTime() >= 4 * 100000000);
    } //food has gone bad once time is equal to ~ 1 hr


    public static void main(String[] args){

        System.out.println("Part 1:");
        System.out.println("~~~~~~~~~~~");

        Food[] foods = {
        new Food("Pasta"),
        new Food("Tacos"),
        new Food("Bagels")
        }; //puts all the foods into an array for simplicity

        for(Food food1 : foods){
            System.out.println(food1);
        } //enhanced for loop to print out all foods

        System.out.println("\nNumber of Total Orders: " + orders);
    }
}
Food.main(null);
Part 1:
~~~~~~~~~~~
Restaurant order: Pasta
 Order Number: 1
Restaurant order: Tacos
 Order Number: 2
Restaurant order: Bagels
 Order Number: 3

Number of Total Orders: 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 void setPrice(double price) {
        this.price = price;
    }

    public String toString() {
        return year + " " + make + " " + model + ", price: $" + price;
    }

    public static void main(String[] args) {
        Car car1 = new Car("Toyota", "Camry", 2018, 25000.00);
        Car car2 = new Car("Honda", "Civic", 2021, 28000.00);
        Car car3 = new Car("Ford", "Mustang", 2016, 32000.00);

        System.out.println(car1);
        System.out.println(car2);
        System.out.println(car3);
    }
}

Car.main(null);
2018 Toyota Camry, price: $25000.0
2021 Honda Civic, price: $28000.0
2016 Ford Mustang, price: $32000.0

Part 2

class Car {
    private String make;
private String model;
private String color;
private double price;

Car(String make, String model, String color, double price) {
    this.make = make;
    this.model = model;
    this.color = color;
    this.price = price;
}

public String getMake() {
    return make;
}

public void setMake(String make) {
    this.make = make;
}

public String getModel() {
    return model;
}

public void setModel(String model) {
    this.model = model;
}

public String getColor() {
    return color;
}

public void setColor(String color) {
    this.color = color;
}

public double getPrice() {
    return price;
}

public void setPrice(double price) {
    this.price = price;
}

@Override
public String toString() {
    return "Car Details: " + make + " " + model + " (" + color + ") " + "Price: $" + price;
}

public boolean isLuxury() {
    if (price > 50000) {
        return true;
    } else {
        return false;
    }
}

public boolean isSedan() {
    if (model.toLowerCase().contains("sedan")) {
        return true;
    } else {
        return false;
    }
}
}

public class CarDealer extends Car {
public static void main(String[] args) {
System.out.println("Part 2:");
System.out.println("~~~~~~~~~~~");
Car[] cars = {
    new Car("Tesla", "Model S", "Black", 80000.0),
    new Car("Toyota", "Camry", "White", 28000.0),
    new Car("Mercedes-Benz", "E-Class Sedan", "Silver", 62000.0)
};

cars[0].setColor("Red");
cars[1].setPrice(30000.0);
cars[2].setMake("BMW");

for (Car car : cars) {
    System.out.println(car);
    System.out.println("Is Luxury Car: " + car.isLuxury());
    System.out.println("Is Sedan: " + car.isSedan());
    System.out.println();
}

System.out.println("Part 3:");
System.out.println("~~~~~~~~~~~");

Car c = new Car("Ford", "Mustang", "Yellow", 45000.0);
Car d = new Car("Honda", "Civic", "Blue", 25000.0);

System.out.println("Is Luxury Car: " + c.isLuxury());
System.out.println("Is Sedan: " + c.isSedan());

System.out.println("Is Luxury Car: " + d.isLuxury());
System.out.println("Is Sedan: " + d.isSedan());

}
class Takeout extends Food {

    private String orderName;
    private int pickups;

    Takeout(String food) {
        super(food); //calls super from the Food class
        this.pickups = 0; //sets the pickups amount to 0
    }

    @Override
    public String toString() {
        return "Restaurant order: " + food + "\n Pickup Order Name: " + orderName;
        //overrides to return the order name
    }

    public String getOrderName() {
        return orderName;
    }
    //setter and getter for order name
    public void setOrderName(String orderName) {
        this.orderName = orderName;
    }

    //number of orders picked up
    public void pickups() {
        pickups++;
        this.startTime = System.nanoTime();
        //starts the time at the pickup
    }

    public int getPickups() {
        return this.pickups; //returns pickups
    }

}

class Dine extends Food {

    private String dineIn;

    Dine(String food) {
        super(food);
    } //constructor for dine in people

    public String getDineIn() {
        return dineIn;
    } //gets the name of the person

    public void setDineIn(String dineIn) {
        this.dineIn = dineIn;
    } //setter for dine in

    @Override
    public String toString() {
        return "Restaurant order: " + food + "\n Dine in Order Name: " + dineIn;
        //overrides to return the order name
    }

}

public class Owner{
    public static void main(String[] args){
        System.out.println("Part 2:");
        System.out.println("~~~~~~~~~~~");

        Takeout[] takeouts = {
        new Takeout("Burrito"),
        new Takeout("Chicken Curry"),
        new Takeout("Vegetarian Noodles")
        }; //puts all the foods into an array for simplicity

        Dine[] diners = {
            new Dine("Salad"),
            new Dine("Pizza"),
            new Dine("Gobi Manchurian")
            }; //puts all the foods into an array for simplicity
    
        takeouts[0].setOrderName("Riya");
        takeouts[1].setOrderName("Vidhi");
        takeouts[2].setOrderName("Vika");
        diners[0].setDineIn("Riya");
        diners[1].setDineIn("Vidhi");
        diners[2].setDineIn("Vika");
        //sets all the takeout order names and diner names

        for(Takeout take : takeouts){
            System.out.println(take);
            System.out.println("Waiting Time: " + take.waitingTime());
            System.out.println();
        } //enhanced for loop to print out all takeout orders

        for(Dine dine : diners){
            System.out.println(dine);
            System.out.println("Waiting Time: " + dine.waitingTime());
            System.out.println();
        } //enhanced for loop to print out all diners

        System.out.println("Part 3:");
        System.out.println("~~~~~~~~~~~");

        Takeout t = new Takeout("Burgers");
        Dine d = new Dine("Soup");

        //checks if gone bad
        System.out.println("Has it gone bad? " + t.goneBad());
        System.out.println("Has it gone bad? " + d.goneBad());
        
        try{
            Thread.sleep(1000); //sleep in java to forward time and check
        } catch (Exception e) {
            Thread.currentThread().interrupt();
        }

        System.out.println();
        System.out.println("After some time...");
        System.out.println();
        //checks if gone bad after time skip
        System.out.println("Has it gone bad? " + t.goneBad());
        System.out.println("Has it gone bad? " + d.goneBad());
        
    }
}
Owner.main(null);
Part 2:
~~~~~~~~~~~
Restaurant order: Burrito
 Pickup Order Name: Riya
Waiting Time: 5714930.0

Restaurant order: Chicken Curry
 Pickup Order Name: Vidhi
Waiting Time: 1.3976192E7

Restaurant order: Vegetarian Noodles
 Pickup Order Name: Vika
Waiting Time: 1.5580933E7

Restaurant order: Salad
 Dine in Order Name: Riya
Waiting Time: 2.4903908E7

Restaurant order: Pizza
 Dine in Order Name: Vidhi
Waiting Time: 3.0058618E7

Restaurant order: Gobi Manchurian
 Dine in Order Name: Vika
Waiting Time: 3.6386993E7

Part 3:
~~~~~~~~~~~
Has it gone bad? false
Has it gone bad? false

After some time...

Has it gone bad? true
Has it gone bad? true

Ensure Novel and Textbook run the Constructor from Book.

Create instance variables unique to Novel has Author, Textbook has publishing company. New items are not required by Constructor.

Make Getters and Setters for all new items. For instance, be able to set items not required by constructor.

Add a time when book entered the library. This should be same for Parent and Subclasses.

Define tester method to test all items

Methods and Control Structures FRQ

public class StringManip {

    // Define a static method that takes a String parameter and returns a reversed String
    public static String reverseString(String str) {
        
        // Get the length of the input String
        int length = str.length();
        
        // Initialize an empty String to store the reversed String
        String result = "";
        
        // Convert the input String to a character array
        char[] strArray = str.toCharArray();
        
        // Iterate through the character array backwards and append each character to the result String
        for (int i = length - 1; i >= 0; i--) {
            result = result + strArray[i];
        }
        
        // Return the reversed String
        return result;
    }

    // Define a static method that takes a String parameter and checks if it is a palindrome
    public static String palindromeChecker(String str) {
        
        // Call the reverseString method to get the reversed String
        String reverse = reverseString(str);
        
        // Compare the original String to the reversed String and check if they are equal
        if (str.compareTo(reverse) == 0) {
            
            // Return a message indicating that the String is a palindrome
            return (str + " is a palindrome");
        } else {
            
            // Return a message indicating that the String is not a palindrome
            return (str + " is not a palindrome");
        }
    }

    // Define the main tester method
    public static void main(String[] args) {
        
        // Print the result of reversing the String "apple"
        System.out.println("Reverse the String 'apple': " + StringManip.reverseString("apple"));
        
        // Print the result of checking if the String "mom" is a palindrome
        System.out.println("Check if 'mom' is a Palindrome: " + StringManip.palindromeChecker("mom"));
        
        // Print the result of checking if the String "computer" is a palindrome
        System.out.println("Check if 'computer' is a Palindrome: " + StringManip.palindromeChecker("computer"));
    }
}
StringManip.main(null);
Reverse the String 'apple': elppa
Check if 'mom' is a Palindrome: mom is a palindrome
Check if 'computer' is a Palindrome: computer is not a palindrome

Writing Classes FRQ

public class AdditionPattern {
    // private instance variables to store start and add values
    private int start;
    private int add;

    // constructor to initialize instance variables
    public AdditionPattern(int start, int add) {
        this.start = start;
        this.add = add;
    }

    // method to return current number in sequence
    public int currentNumber() {
        return start;
    }

    // method to move to next number in sequence
    public void next() {
        start += add;
    }
    
    // method to move to previous number in sequence
    public void previous() {
        start -= add;
    }

    // main method to test the class
    public static void main(String[] args) {
        // create a new instance of the class with start=10 and add=3
        AdditionPattern addition = new AdditionPattern(10, 3);

        // print the current number (should be 10)
        System.out.println(addition.currentNumber());

        // move to next number and print (should be 13)
        addition.next();
        System.out.println(addition.currentNumber());

        // move to previous number and print (should be 10 again)
        addition.previous();
        System.out.println(addition.currentNumber());
    }
}

// call the main method to run the program
AdditionPattern.main(null);
10
13
10

Arrays/Arraylist FRQ

// The CarRepair class represents a repair job on a car.
public class CarRepair {
    private int mechanicNum; // The mechanic number responsible for the repair job
    private int bayNum; // The bay number where the repair job will take place

    // Constructor for creating a new CarRepair object.
    public CarRepair(int m, int b) {
        mechanicNum = m;
        bayNum = b;
    }

    // Returns the mechanic number for this repair job.
    public int getMechanicNum() {
        return mechanicNum;
    }

    // Returns the bay number for this repair job.
    public int getBayNum() {
        return bayNum;
    }
    // There may be other instance variables, constructors, and methods not shown.
}

// The RepairSchedule class manages the schedule of car repairs.
public class RepairSchedule {
    private ArrayList<CarRepair> schedule; // An arraylist to store the CarRepair objects
    private int numberOfMechanics; // The total number of mechanics available to do repairs

    // Constructor for creating a new RepairSchedule object.
    public RepairSchedule(int n) {
        schedule = new ArrayList<CarRepair>();
        numberOfMechanics = n;
    }

    // Adds a new CarRepair object to the schedule if the mechanic and bay are not already in use.
    public boolean addRepair(int m, int b) {
        for (int i = 0; i < schedule.size(); i++) {
            if (schedule.get(i).getMechanicNum() == m || schedule.get(i).getBayNum() == b) {
                return false;
            }
        }
        schedule.add(new CarRepair(m, b));
        return true;
    }

    public static void main(String[] args) {
        RepairSchedule schedule = new RepairSchedule(3); // create a schedule with 3 mechanics
        System.out.println(schedule.addRepair(1, 1)); // should print true
        System.out.println(schedule.addRepair(2, 1)); // should print true
        System.out.println(schedule.addRepair(1, 2)); // should print true
        System.out.println(schedule.addRepair(2, 2)); // should print false (both mechanic 2 and bay 2 are already in use)
        System.out.println(schedule.addRepair(3, 3)); // should print true
    }
}

// Call the main method of the RepairSchedule class
RepairSchedule.main(null);
true
false
false
true
true

2D Arrays FRQ

public class ArrayResizer {
    
    // Check if a given row in a 2D array has all non-zero values
    public static boolean isNonZeroRow(int[][] array2D, int r) {
        // Use == instead of = to compare values
        for (int col = 0; col < array2D[0].length; col++) {
            if (array2D[r][col] == 0) {
                return false;
            }
        }
        // If all values are non-zero, return true
        return true;
    }

    // Resize a 2D array by copying its elements into a new array
    public static int[][] resize(int[][] array2D) {
        int rownum = 0;
        int col = 0;
        int[][] arr = new int[array2D.length][array2D[0].length]; // Add semicolon at the end of the line

        for (int row = 0; row < array2D.length; row++) {
            for (col = 0; col < array2D[0].length; col++) {
                arr[rownum][col] = array2D[row][col];
            }
            rownum++; // Increment rownum to copy elements to the next row in the new array
        }
        // Return the resized array
        return arr;
    }

    public static void main(String[] args) {
        // Call methods inside the main method
        // Test isNonZeroRow method
        int[][] testArray1 = {{1, 2, 3}, {4, 0, 6}, {7, 8, 9}};
        int[][] testArray2 = {{0, 0, 0}, {4, 5, 6}, {7, 8, 9}};

        System.out.println("Testing isNonZeroRow method:");
        System.out.println(isNonZeroRow(testArray1, 0)); // Expected output: true
        System.out.println(isNonZeroRow(testArray1, 1)); // Expected output: false
        System.out.println(isNonZeroRow(testArray2, 0)); // Expected output: false

        // Test resize method
        int[][] testArray3 = {{1, 2}, {3, 4}, {5, 6}};
        int[][] resizedArray = resize(testArray3);

        System.out.println("Testing resize method:");
        for (int i = 0; i < resizedArray.length; i++) {
            for (int j = 0; j < resizedArray[0].length; j++) {
                System.out.print(resizedArray[i][j] + " ");
            }
            System.out.println();
        }
        // Expected output:
        // 1 2
        // 3 4
        // 5 6
    }
}
ArrayResizer.main(null);
Testing isNonZeroRow method:
true
false
false
Testing resize method:
1 2 
3 4 
5 6