Unit 5 Hack

Class which has a constructor which sets at least 1 public and 1 private attribute (incorporate “this” keyword)


Class which has public and private attributes and methods
Class which has at least 1 static attribute
Class which has getters and setters for at least 1 private attribute
public class Car {
    private static HashMap<String, Car> Garage = new HashMap<String, Car>();

    private String make;
    private String model;
    private static int carCount;

    private long start;
    private long finish;

    public String nothing = "why do we need a public attribute? does private instance and the getter doesn't seems good enough?";
    
    // Constructor
    public Car(String make, String model) {
        this.make = make;
        this.model = model;
        this.start = System.currentTimeMillis()/100;
        Garage.put((make + " " + model), this);
    }
    
    // Getter methods
    public String getMake() {
        return make;
    }
    
    public String getModel() {
        return model;
    }

    public long getCarEnter(){
        return start;
    }

    public long getCarExit(){
        finish = System.currentTimeMillis()/100;
        return finish;
    }

    public static void CarExit(String carMakeAndModel){
        Garage.remove(carMakeAndModel);
    }

    public static long getCurrentCarTime(Car check){
        return (check.getCarExit() - check.getCarEnter());
    }

    public static int getCarCount(){
        return Garage.size();
    }
    
    public String toString() {
        return ("Make: " + getMake() + ", Model: " + getModel() + ", Vehicle residence time: " + getCurrentCarTime(this) + " days");
    }

    // Main method
    public static void main(String[] args) {
        boolean checkUser = true;
        while(true){
            while (checkUser){
                try{
                    Scanner scanner = new Scanner(System.in);
                    System.out.println("Enter the number of cars: ");
                    int carNum = scanner.nextInt();
                    while(carNum >0){ //Use a while loop to ensure that the user enters a positive integer for the number of cars they own.
                        Scanner scanner2 = new Scanner(System.in);
                        System.out.println("Enter the make of your " + carNum + " car: ");
                        String carMake = scanner2.nextLine();

                        Scanner scanner3 = new Scanner(System.in);
                        System.out.println("Enter the model of your " + carNum + " car: ");
                        String carModel = scanner3.nextLine();

                        Car car = new Car(carMake, carModel);
                        carNum--;
                    }
                    if (carNum == 0){
                        checkUser = false;
                    }
                    if (carNum < 0){
                        System.out.println("Try to enter a positive integer");
                    }
                }catch (ArithmeticException e) {
                    System.out.println("Error: Wrong input, please enter a positive integer!"); // Catching and handling the exception
                }
            }

            Scanner carList = new Scanner(System.in);
            System.out.println("Want to check the cars you own in the garage? Type yes or no: ");
            String userChose3 = carList.nextLine();

            if(userChose3.equals("yes")){
                if(Garage.size() == 0){
                    System.out.println("There is no car in the garage.");
                    System.out.println();
                }
                else{
                    System.out.println("You have " + getCarCount() + " cars in your garage.");
                    for (Map.Entry entry : Garage.entrySet()) {
                        System.out.println(entry.getValue());
                    }
                    System.out.println();
                }

            }

            Scanner carExit = new Scanner(System.in);
            System.out.println("Do you want to get any cars out from the garage? If yes, enter the car make + space + car model, if no, type no: ");
            System.out.println();
            String userChose2 = carExit.nextLine();

            if(userChose2.equals("no") !=true){
                Garage.get(userChose2);
                System.out.println("The car you chose is: " + userChose2 + ", the vehicle stayed in garage for about :" + getCurrentCarTime(Garage.get(userChose2)) + " days");
                System.out.println();
                CarExit(userChose2);
                System.out.println("You have " + getCarCount() + " cars in your garage.");
                for (Map.Entry entry : Garage.entrySet()) {
                    System.out.println(entry.getValue());
                }
                System.out.println();
            }

            Scanner scanner4 = new Scanner(System.in);
            System.out.println("Want to add more car? Enter yes or no: ");
            String userChose = scanner4.nextLine();
            if (userChose.equals("yes")){
                System.out.println();
                checkUser = true;
            }
            else{
                break;
            }
        }
    }
}
Car.main(null);
Enter the number of cars: 
Enter the make of your 2 car: 
Enter the model of your 2 car: 
Enter the make of your 1 car: 
Enter the model of your 1 car: 
Want to check the cars you own in the garage? Type yes or no: 
You have 2 cars in your garage.
Make: a, Model: b, Vehicle residence time: 47 days
Make: b, Model: a, Vehicle residence time: 39 days

Do you want to get any cars out from the garage? If yes, enter the car make + space + car model, if no, type no: 

Want to add more car? Enter yes or no: 

Unit 9 Hack

  1. Create a parent class of your choice
  2. Create 2 subclasses from that parent superclass
  3. incorporate some data and atleast one method into each class, using super keyword
  4. Override superclass method in the subclasses
  5. Use a tester method to create objects from the superclass that take on the forms of the subclasses, and print some outputs
public class Animal{
    private String name;

    public Animal(String nameAnimal){
        this.name = nameAnimal;
    }

    public String getName(){
        return name;
    }

    public String toString(){
        return "Animal name: " + this.getName();
    }
    
    public static void main(String[] args) {
        Animal tester = new Bird("1", "Bird");
        System.out.println(tester);
        Animal tester2 = new Cat("2", "Cat");
        System.out.println(tester2);
    }
}
Animal.main(null);
Animal name: 1, type: Bird
Animal name: 2, type: Cat
public class Bird extends Animal{
    private String type;
    
    public Bird(String birdName, String typeAnimal){
        super(birdName);
        this.type = typeAnimal;
    }

    public String getType(){
        return type;
    }

    public String toString(){
        return super.toString() + ", type: " + this.getType();
    }

}
public class Cat extends Animal{
    private String type;

    public Cat(String catName, String typeAnimal){
        super(catName);
        this.type = typeAnimal;
    }

    public String getType(){
        return type;
    }

    public String toString(){
        return super.toString() + ", type: " + this.getType();
    }
}

Unit 10 Hack

  1. Fibonacci, or factorial, although this is pretty basic
  2. A more advanced version of this would be to create your own searching or sorting algorithm that utilizes recursive
public class Main { //as you said, I just need to copy and paste the code from tutorial
    public static int factorial(int n) {
        if (n == 0) {
            return 1;
        } else {
            return n * factorial(n - 1);
        }
    }

    public static void main(String[] args) {
        System.out.println(factorial(5));
    }
}
Main.main(null);
120
public class Main { 
    public static int letterSorting(String tester, String letter, int indexStart) { // instead of using for loop to search, I utilize recursive
        int i = indexStart;
        if (tester.substring(i, i + 1).equals(letter)) {
            return i;
        } else {
            return letterSorting(tester, letter, i + 1);
        }
    

    }

    public static void main(String[] args) {
        System.out.println(letterSorting("air", "i", 0));
    }
}
Main.main(null);
1