Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. selected_dog = new Labrador;
  2. selected_dog = new Labrador;
  3. selected_dog = new Dog.Labrador;
  4.  
  5. import java.util.Scanner;
  6.  
  7. public class DogsTest
  8. {
  9. private static Scanner input = new Scanner(System.in);
  10.  
  11. public static void main(String[] args)
  12. {
  13. final String YES = "y";
  14.  
  15. String answer;
  16. Dog my_dog; // Step 1
  17.  
  18. do
  19. {
  20. // ------------------------------------------------------------
  21. // The compiler cannot know at compile time what type my_dog is
  22. // so it is determined at runtime every time the loop iterates
  23. // ------------------------------------------------------------
  24. my_dog = getDog();
  25. System.out.println(my_dog.getName() + " says " + my_dog.speak());
  26.  
  27. System.out.print("Try again? ");
  28. answer = input.next();
  29. } while (answer.equalsIgnoreCase(YES));
  30. }
  31.  
  32. public static Dog getDog() // Step 2
  33. {
  34. int choice;
  35. Dog selected_dog; // Step 3
  36. String name,
  37. color;
  38.  
  39. do
  40. {
  41. // ----------------------------------
  42. // A null reference indicates that an
  43. // invalid menu choice was entered
  44. // ----------------------------------
  45. selected_dog = null;
  46. System.out.print("Choose a Breed (1. Labrador 2. Yorkshire): ");
  47. choice = input.nextInt();
  48.  
  49. switch (choice)
  50. {
  51. case 1: System.out.print("Enter dog's name: ");
  52. name = input.next();
  53. System.out.print("Enter dog's color: ");
  54. color = input.next();
  55. selected_dog = Labrador; // Step 4
  56. break;
  57. case 2: System.out.print("Enter dog's name: ");
  58. name = input.next();
  59. selected_dog = ______; // Step 5
  60. break;
  61. default: System.out.println("Invalid choice");
  62. break;
  63. }
  64. } while (selected_dog == null);
  65. return __________________; // Step 6
  66. }
  67. }
  68.  
  69. public class Dog
  70. {
  71. protected String name;
  72.  
  73. public Dog(String name)
  74. {
  75. this.name = name;
  76. }
  77.  
  78. public String getName()
  79. {
  80. return name;
  81. }
  82.  
  83. public String speak()
  84. {
  85. return "Woof";
  86. }
  87. }
  88.  
  89. public class Labrador extends Dog
  90. {
  91. private String color;
  92. private static int breed_weight = 75;
  93.  
  94. public Labrador(String name, String color)
  95. {
  96. this.color = color;
  97. }
  98.  
  99. // =========================================
  100. // Big bark -- overrides speak method in Dog
  101. // =========================================
  102. public String speak()
  103. {
  104. return "WOOF";
  105. }
  106.  
  107. public static int avgBreedWeight()
  108. {
  109. return breed_weight;
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement