Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Sammy Samkough
- // The Pet class is fully coded
- // The class itself must be declared abstract
- // since it contains an abstract method
- public abstract class Pet
- {
- private String myName;
- public Pet(String name)
- {
- myName = name;
- }
- public String getName()
- {
- return myName;
- }
- public abstract String speak();
- }
- -------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // Class Hierarchy:
- // ______
- // | |
- // | Pet |
- // |_____|
- // / \
- // / \
- // __/___ ___\___
- // | | | |
- // | Cat | | Dog |
- // |_____| |______|
- // |
- // ______|______
- // | |
- // | LoudDog |
- // |____________|
- public class Dog extends Pet
- {
- public Dog(String name)
- {
- super(name);
- }
- public String speak()
- {
- String str = "dog-sound";
- return str;
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // Part A - Write a complete class declaration for the class Cat
- // including implementations of its constructor and method(s)
- // the Cat method speak returns "meow" when it is invoked.
- // Class Hierarchy:
- // ______
- // | |
- // | Pet |
- // |_____|
- // / \
- // / \
- // __/___ ___\___
- // | | | |
- // | Cat | | Dog |
- // |_____| |______|
- // |
- // ______|______
- // | |
- // | LoudDog |
- // |____________|
- public class Cat extends Pet
- {
- public Cat(String name)
- {
- super(name);
- }
- public String speak()
- {
- String str = "meow";
- return str;
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // Part B - Write a complete class declaration for the class LoudDog
- // including implementations of its constructor and method(s)
- // if the String dog-sound is returned by the Dog method speak,
- // then the LoudDog method speak returns a String containing
- // dog-sound repeated two times.
- // Class Hierarchy:
- // ______
- // | |
- // | Pet |
- // |_____|
- // / \
- // / \
- // __/___ ___\___
- // | | | |
- // | Cat | | Dog |
- // |_____| |______|
- // |
- // ______|______
- // | |
- // | LoudDog |
- // |____________|
- public class LoudDog extends Dog
- {
- public LoudDog(String name)
- {
- super(name);
- }
- public String speak()
- {
- String str = super.speak() + super.speak();
- return str;
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // Pet
- // Spec: Create methods about Pet.
- import java.util.ArrayList;
- public class Kennel
- {
- private ArrayList <Pet> petList;
- public Kennel()
- {
- petList = new ArrayList<Pet>();
- }
- // Part C
- // Write method allSpeak. For each Pet in the kennel, allSpeak
- // prints a line w/ the name of the Pet followed by
- // the result of a call to its speak method.
- public void allSpeak()
- {
- for (Pet p : petList)
- {
- System.out.println("Name: " + p.getName() + "\nVoice: " + p.speak());
- }
- }
- // added this method to add Pets to petList
- public void addPet(Pet fred)
- {
- petList.add(fred);
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- // Sammy Samkough
- // PetDriver
- // Spec: Print out the pets.
- public class PetDriver
- {
- public static void main(String args[])
- {
- Cat mark = new Cat("Mark");
- Dog zach = new Dog("Zach");
- LoudDog harry = new LoudDog("Harry");
- System.out.println("My cat named " + mark.getName() + " makes this sound: " + mark.speak());
- System.out.println("My dog named " + zach.getName() + " makes this sound: " + zach.speak());
- System.out.println("My loud dog named " + harry.getName() + " makes this sound: " + harry.speak());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement