Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class PetStatistics
  3. {
  4. public static void main(String[] args)
  5. {
  6. Scanner keyboard = new Scanner(System.in);
  7. String petName = "";
  8. int petAge = 0;
  9. double petWeight = 0;
  10.  
  11. Pet pet1 = null, pet2 = null, pet3 = null;
  12. double largest = 0, smallest = 0;
  13. int youngest = 0, oldest = 0;
  14. double aveWeight = 0, aveAge = 0;
  15.  
  16. System.out.print("Enter name of Pet 1:");
  17. petName=keyboard.nextLine();
  18. System.out.print("Enter age of Pet 1:");
  19. petAge=keyboard.nextInt();
  20. System.out.print("Enter weight of Pet 1:");
  21. petWeight=keyboard.nextDouble();
  22. pet1=new Pet(petName,petAge,petWeight);
  23.  
  24.  
  25. keyboard.nextLine();
  26. System.out.print("Enter name of Pet#2:");
  27. petName=keyboard.nextLine();
  28. System.out.print("Enter age of Pet#2:");
  29. petAge=keyboard.nextInt();
  30. System.out.print("Enter weight of Pet#2:");
  31. petWeight=keyboard.nextDouble();
  32. pet2=new Pet(petName,petAge,petWeight);
  33.  
  34. keyboard.nextLine();
  35. System.out.print("Enter name of Pet#3:");
  36. petName=keyboard.nextLine();
  37. System.out.print("Enter age of Pet#3:");
  38. petAge=keyboard.nextInt();
  39. System.out.print("Enter weight of Pet#3:");
  40. petWeight=keyboard.nextDouble();
  41. pet3=new Pet(petName,petAge,petWeight);
  42.  
  43. System.out.println("====================================");
  44. System.out.println();
  45.  
  46. largest=pet1.getWeight();
  47. if(largest<pet2.getWeight())
  48. {
  49. largest=pet2.getWeight();
  50.  
  51. }
  52. if(largest<pet3.getWeight())
  53. {
  54. largest=pet3.getWeight();
  55. }
  56. System.out.println("The largest pet(s) (" + largest + " pounds):");
  57.  
  58. if(largest==pet1.getWeight())
  59. {
  60. System.out.println(pet1.getName());
  61. }
  62. if(largest==pet2.getWeight())
  63. {
  64. System.out.println(pet2.getName());
  65. }
  66. if(largest==pet3.getWeight())
  67. {
  68. System.out.println(pet3.getName());
  69. }
  70. System.out.println();
  71.  
  72. smallest=pet1.getWeight();
  73. if(smallest>pet2.getWeight())
  74. {
  75. smallest=pet2.getWeight();
  76. }
  77. if(smallest>pet3.getWeight())
  78.  
  79. {
  80. smallest=pet3.getWeight();
  81. }
  82. System.out.println("The smallest pets (" + smallest + " pounds):");
  83. if(smallest==pet1.getWeight())
  84. {
  85. System.out.println(pet1.getName());
  86. }
  87. if(smallest==pet2.getWeight())
  88. {
  89. System.out.println(pet2.getName());
  90. }
  91. if(smallest==pet3.getWeight())
  92. {
  93. System.out.println(pet3.getName());
  94. }
  95. System.out.println();
  96.  
  97.  
  98. oldest=pet1.getAge();
  99. if(oldest<pet2.getAge())
  100. {
  101. oldest=pet2.getAge();
  102. }
  103. if(oldest<pet3.getAge())
  104. {
  105. oldest=pet3.getAge();
  106. }
  107. System.out.println("The oldest pet(s) (" + oldest + " years):");
  108.  
  109. if(oldest==pet1.getAge())
  110. {
  111. System.out.println(pet1.getName());
  112. }
  113. if(oldest==pet2.getAge())
  114. {
  115. System.out.println(pet2.getName());
  116. }
  117. if(oldest==pet3.getAge())
  118. {
  119. System.out.println(pet3.getName());
  120. }
  121. System.out.println();
  122. youngest=pet1.getAge();
  123. if(youngest>pet2.getAge())
  124.  
  125. {
  126. youngest=pet2.getAge();
  127. }
  128. if(youngest>pet3.getAge())
  129. {
  130. youngest=pet3.getAge();
  131. }
  132. System.out.println("The youngest pet(s) (" + youngest + " years):");
  133.  
  134. if(youngest==pet1.getAge())
  135. {
  136. System.out.println(pet1.getName());
  137. }
  138. if(youngest==pet2.getAge())
  139. {
  140. System.out.println(pet2.getName());
  141. }
  142. if(youngest==pet3.getAge())
  143. {
  144. System.out.println(pet3.getName());
  145. }
  146. System.out.println();
  147.  
  148. aveWeight=(pet1.getWeight()+pet2.getWeight()+pet3.getWeight())/3.0;
  149. System.out.print("Average weight (pounds) = " + aveWeight);
  150. System.out.println();
  151.  
  152. aveAge=(pet1.getAge()+pet2.getAge()+pet3.getAge())/3.0;
  153. System.out.print("Average age (years) = " + aveAge);
  154. System.out.println();
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement