> For the complete documentation index, see [llms.txt](https://www.computersciencea.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.computersciencea.com/older/session-20.md).

# Session 20

## Source Code

```java
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

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

    // constructor
    

    // constructor
    

    // methods
    

}
```

## Tiger.java

```java
// class header
public class Tiger extends Feline {

    // methods



}
```

## Cat.java

```java
// class header
public class Cat extends Feline {

     // constructors
     


     // methods
     

}
```

## Zoo.java

```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

```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

```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

```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();
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.computersciencea.com/older/session-20.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
