Unit 5 Hacks

// 1. 1 public and 1 private attribute
public class Account {
    private String password;
    public String username;

    // 3. static attribute
    public static int totalAccounts;

    public Account(String password, String username) {
        this.password = password;
        this.username = username;
        setTotalAccounts();
    }

    // 2. public and private attributes and methods
    private String getPassword() {
        return this.password;
    }

    public String getUsername() {
        return this.username;
    }

    // 4. setter for private attribute
    private void setPassword(String password) {
        this.password = password;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public static void setTotalAccounts() {
        totalAccounts++;
    }

    public String toString() {
        return ("User: " + this.getUsername() + ", Password: " + this.getPassword() + ", Total Accounts: " + totalAccounts);
    }

    // tester method
    public static void main(String[] args) {
        Account mads = new Account("password", "mads");
        Account meena = new Account("password", "meena");

        // test setters
        mads.setPassword("new password!");
        mads.setUsername("mads!");

        System.out.println(mads.toString());
        System.out.println(meena.toString());
    }
}

Account.main(null);
User: mads!, Password: new password!, Total Accounts: 2
User: meena, Password: password, Total Accounts: 2

Unit 9 Hacks

Hack 1

class Vehicle {
    private String name;

    public Vehicle(String name){
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public void honk() {
        System.out.println("beep beep");
    }

}

class Car extends Vehicle {
    public Car(String name) {
        super(name);
    }

    public static void main(String[] args) {
        Car lambo = new Car("lambo");

        System.out.println(lambo.getName());
        lambo.honk();
    }
}

Car.main(null);
lambo
beep beep

Hack 2

public class Animal {
    private String name; 

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

    public String getName() {
        return this.name;
    }

    public String toString() {
        return ("Name: " + this.getName());
    }

    // tester method
    public static void main(String[] args) {
        Animal goldenRetriever = new Dog("golden retriever");
        System.out.println(goldenRetriever.toString());
    }
}

public class Dog extends Animal {
    private int legs = 4;

    public Dog(String name) {
        super(name);
    }

    // override superclass method
    public String toString() {
        return(super.toString() + ", Legs: " + this.legs);
    }
}

public class Bird extends Animal {
    private int legs = 2;

    public Bird(String name) {
        super(name);
    }

    // override superclass method
    public String toString() {
        return(super.toString() + ", Legs: " + this.legs);
    }

    // tester method
    public static void main(String[] args) {
        Animal ostrich = new Bird("ostrich");
        System.out.println(ostrich.toString());
    }
}

Bird.main(null);
Dog.main(null);
Name: ostrich, Legs: 2
Name: golden retriever, Legs: 4

Unit 10 Hacks

public int factorial(int n) {
    if(n == 0) {
        return 1;
    }

    return n * factorial(n-1);
}

factorial(6);
720