Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- selected_dog = new Labrador;
- selected_dog = new Labrador;
- selected_dog = new Dog.Labrador;
- import java.util.Scanner;
- public class DogsTest
- {
- private static Scanner input = new Scanner(System.in);
- public static void main(String[] args)
- {
- final String YES = "y";
- String answer;
- Dog my_dog; // Step 1
- do
- {
- // ------------------------------------------------------------
- // The compiler cannot know at compile time what type my_dog is
- // so it is determined at runtime every time the loop iterates
- // ------------------------------------------------------------
- my_dog = getDog();
- System.out.println(my_dog.getName() + " says " + my_dog.speak());
- System.out.print("Try again? ");
- answer = input.next();
- } while (answer.equalsIgnoreCase(YES));
- }
- public static Dog getDog() // Step 2
- {
- int choice;
- Dog selected_dog; // Step 3
- String name,
- color;
- do
- {
- // ----------------------------------
- // A null reference indicates that an
- // invalid menu choice was entered
- // ----------------------------------
- selected_dog = null;
- System.out.print("Choose a Breed (1. Labrador 2. Yorkshire): ");
- choice = input.nextInt();
- switch (choice)
- {
- case 1: System.out.print("Enter dog's name: ");
- name = input.next();
- System.out.print("Enter dog's color: ");
- color = input.next();
- selected_dog = Labrador; // Step 4
- break;
- case 2: System.out.print("Enter dog's name: ");
- name = input.next();
- selected_dog = ______; // Step 5
- break;
- default: System.out.println("Invalid choice");
- break;
- }
- } while (selected_dog == null);
- return __________________; // Step 6
- }
- }
- public class Dog
- {
- protected String name;
- public Dog(String name)
- {
- this.name = name;
- }
- public String getName()
- {
- return name;
- }
- public String speak()
- {
- return "Woof";
- }
- }
- public class Labrador extends Dog
- {
- private String color;
- private static int breed_weight = 75;
- public Labrador(String name, String color)
- {
- this.color = color;
- }
- // =========================================
- // Big bark -- overrides speak method in Dog
- // =========================================
- public String speak()
- {
- return "WOOF";
- }
- public static int avgBreedWeight()
- {
- return breed_weight;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement