Source Code
Session8.java
import java.util.*;
public class Session8 {
public static void main(String[] args) {
String myName = ""; // add your name
System.out.println("Session 8" + " - " + 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 "E5":
System.out.println("Example 5");
example5();
break;
case "E6":
System.out.println("Example 6");
example6();
break;
case "E7":
System.out.println("Example 7");
example7();
break;
case "Q":
System.out.println("Quitting..!");
break;
default:
System.out.println("Invalid Choice");
}
}
// 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
}
// example5 method
public static void example5() {
// Create a Greeter
// Create a MeanGreeter
}
// example6 method
public static void example6() {
// Create a Greeter
// Create a MeanGreeter
}
// example7 method
public static void example7() {
}
}
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 {
// Array 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
}
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();
}
}
Greeter.java
public class Greeter {
// greet method no parameters
// greet method with one parameter
}
MeanGreeter.java
public class MeanGreeter extends Greeter {
@Override // Compiler will verify this actually overrides a method
// greet method with no parameters
// greet method with one parameter
}
Last updated