Week 20

Source Code

import java.util.*;

public class Week20 {
    public static void main(String[] args) {
        String myName = ""; // add your name
        System.out.println("Week 20" + " - " + myName);

        // Menu
        System.out.println("Menu");
        System.out.println("E1 - Example 1");
        System.out.println("Q  - Quit");

        // setup Scanner
        Scanner in = new Scanner(System.in);
        System.out.print("Choice: ");
        String choice = in.nextLine();

        // switch choices
        switch (choice) {
            case "E1":
                System.out.println("Example 1");
                example1();
                break;
            case "E2":
                System.out.println("Example 2");
                example2();
                break;
            case "E3":
                System.out.println("Example 3");
                example3();
                break;
            case "E4":
                System.out.println("Example 4");
                example4();
                break;
            case "Q":
                System.out.println("Quitting..!");
                break;
        }
    }

    // example1 method
    public static void example1() {
        // Create Objects
        

        // Create an array of Felines
        

        // populate the array with Cat and Tiger objects
        

        // loop through array and call method
        

        // instanceOf Examples
        // is catty a Feline?
        

        // is tigris a Feline?
        

        // is felix a Cat?
        
        
    }

    // example2 method
    public static void example2() {
        // create a new Zoo
        

        // Adding different animals to the zoo
        

        // Displaying all animals in the zoo
        

    }

    // example3 method
    public static void example3() {
        // Create Object
        
    }

    // example4 method
    public static void example4() {
        // Create Object
        
    }

}

Feline.java

// class header
public class Feline {
    // private instance variable
    

    // constructor
    

    // constructor
    

    // methods
    

}

Tiger.java

// class header
public class Tiger extends Feline {

    // methods



}

Cat.java

// class header
public class Cat extends Feline {

     // constructors
     


     // methods
     

}

Zoo.java

import java.util.*;

// Base class representing a generic Animal (IS-A relationship)
class Animal {
    // Method to make a generic animal sound
    public void makeSound() {
        System.out.println("Animals make sounds");
    }
}

// Subclass representing a Bear (IS-A Animal)
class Bear extends Animal {
    // Method specific to Bear
    public void growl() {
        System.out.println("Bears like to growl");
    }
}

// Subclass representing a Lion (IS-A Animal)
class Lion extends Animal {
    // Method specific to Lion
    public void roar() {
        System.out.println("Lions like to roar");
    }
}

// Subclass representing an Elephant (IS-A Animal)
class Elephant extends Animal {
    // Method specific to Elephant
    public void forage() {
        System.out.println("Elephants like to forage");
    }
}

// Zoo class HAS-A relationship with a list of animals
class Zoo {
    // ArrayList to store Animal objects
    

    // Constructor to initialize the list of animals
    

    // Add an animal to the zoo
    

    // Method to display the sounds of all animals in the zoo
    
            // Check if the animal is a Bear
            
            
            // Check if the animal is a Lion
            
            // Check if the animal is an Elephant
            
            
}

Professions.java

import java.util.*;

// Superclass


// Subclass: 


// Subclass: 


// Subclass: 


// Subclass: 


// Main class
public class Professions {
    public static void main(String[] args) {
        // create an ArrayList of Professions
        

        // Creating instances of each profession
        

        // Adding objects to the ArrayList
        

        // Loop through ArrayList of Professions using instanceof and calling methods
        
        
    }
}

MainCharacter.java

class CharacterBase {
    void action() {
        System.out.println("Ready to fight! ");
    }
}

class Warrior extends CharacterBase {
    public Warrior() {
        System.out.println("Equipped with sword! ");
        super.action();
    }
}

class Knight extends Warrior {
    public Knight() {
        System.out.println("For the kingdom!");
    }
}

public class MainCharacter {
    public static void main(String[] args) {
        Knight k = new Knight();
    }
}

SquareDemo.java

// A class called Rectangle that has two instance variables, length and width,
// a constructor that initializes them, and a method called draw that uses nested loops
// to draw a length x width rectangle of stars.
class Rectangle {
    private int length;
    private int width;

    // Constructor that initializes length and width
    public Rectangle(int l, int w) {
        length = l;
        width = w;
    }

    // Method to draw a rectangle of stars based on length and width
    public void draw() {
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < width; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
        System.out.println();
    }

    // Add an area method to compute the area of the rectangle


}

// Make the class Square below inherit from Rectangle

    // Add a Square constructor with 1 argument for a side
    // that calls Rectangle's constructor with 2 arguments using super
    

// Add another subclass called LongRectangle which inherits from Rectangle
// but has the additional condition that the length is always 2 x the width



public class SquareDemo {
    public static void main(String[] args) {
        // Create a Rectangle object and draw it
        Rectangle r = new Rectangle(3, 5);
        // System.out.println("Area of rectangle: " + r.area());
        r.draw();

        // Uncomment the objects to test the squares
        // Square s1 = new Square(1);
        // System.out.println("Area of square with side 1: " + s1.area());
        // s1.draw();

        // Square s2 = new Square(3);
        // System.out.println("Area of square with side 3: " + s2.area());
        // s2.draw();
        // System.out.println();

        // Uncomment the objects to test the rectangle
        // LongRectangle lr = new LongRectangle(4);
        // System.out.println("Area of long rectangle with length 4: " + lr.area());
        // lr.draw();
    }
}

Last updated

Was this helpful?