Question: Using the class Pet in the Sakai Week 8 => Source Code folder, write a program to read data for three Pets, create three Pet

Using the class Pet in the Sakai Week 8 => Source Code folder, write a program to read data for three Pets, create three Pet objects, and display the following information, using the Pet instance methods (check the Pet source code to see them):

The names of the smallest and largest Pets (by weight).

The names of the youngest and oldest Pets.

The average weight of the three Pets.

The average age of the three Pets.

**Hints: You can keep track of the smallest/largest and youngest/oldest Pets as you are reading them in once the first one has been initialized you can assume it is the smallest/largest and youngest/oldest to start, and then compare later Pets against those. If more than one Pet is the smallest or largest or youngest or oldest, print the names of all of them together with their related statistic values.

You can also accumulate their weights and ages as they are being read in, starting from 0 for both. Be sure to calculate the average age as a double, not by using integer division.**

IN JAVA:

public class PetStatistics { public static void main(String[] args) { Scanner170 keyboard = new Scanner170(System.in); // some variables for your program ... String petName = ""; int petAge = 0; double petWeight = 0; Pet pet1 = null, pet2 = null, pet3 = null; double largest = 0, smallest = 0; // weights int youngest = 0, oldest = 0; // ages double aveWeight = 0, aveAge = 0; // write statements to prompt for the name, age, // and weight of each Pet in turn, and create Pet // objects from each of those 3 Pets using // reference variables pet1, pet2, and pet3 // be sure to use keyboard.next() to read in the // name of each Pet to avoid problems with the // use of nextInt() and nextDouble() /* your code goes here */

System.out.println("===================================="); System.out.println();

// now find and print the name(s) of the largest Pet(s): // first find the largest weight, then print the name(s) // of any Pet(s) that has/have that same weight /* your code to find the largest weight goes here */

System.out.println("The largest pet(s) (" + largest + " pounds):"); /* your code to print just the name(s) of the largest Pet(s) goes here */ System.out.println();

// find the weight of the smallest Pet(s) /* your code goes here */

System.out.println("The smallest pet(s) (" + smallest + " pounds):"); /* your code to print just the name(s) of the smallest Pet(s) goes here */ System.out.println();

/* your code to find the age of the oldest Pet(s) goes here */

System.out.println("The oldest pet(s) (" + oldest + " years):"); /* your code to print just the name(s) of the oldest Pet(s) goes here */ System.out.println();

/* your code to find the age of the youngest Pet(s) goes here */

System.out.println("The youngest pet(s) (" + youngest + " years):"); /* your code to print just the name(s) of the oldest Pet(s) goes here */ System.out.println();

/* your code to calculate the average weight of the 3 Pets goes here */ System.out.print("Average weight (pounds) = " + aveWeight); System.out.println();

/* your code to calculate the average age of the 3 Pets goes here */ /* be careful of integer division when calculating the average! */ System.out.print("Average age (years) = " + aveAge); System.out.println(); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!