Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with the ouput for the smallest and the youngest please. The output fo

ID: 3538498 • Letter: N

Question

Need help with the ouput for the smallest and the youngest please. The output for the oldest and largest works fine, thanks.


// var declaration
    private static String petName;
    private static int petAge;
    private static int youngAge;
    private static double smallWeight;
    private static double largeWeight;
    private static int ageSum;
    private static double weightSum;
    private static double ageAverage;
    private static double weightAverage;
    private static double petWeight;
    private static String smallestPet;
    private static String largestPet;
    private static String oldestPet;
    private static String youngestPet;
    private static int oldAge;

   // private static Scanner input;
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        Pet pet = new Pet (petName,petAge,petWeight);
        //input = new Scanner(System.in);
     
       
      
        //initialize the values
        smallestPet = pet.getName();
        largestPet = pet.getName();
        oldestPet = pet.getName();
        weightSum = pet.getWeight();
        oldAge = pet.getAge();
        youngAge = pet.getAge();
        smallWeight = pet.getWeight();
        largeWeight = pet.getWeight();
        youngestPet = pet.getName();
        ageSum = pet.getAge();
       
       
         petName = " No Name yet";
         petAge = 0;
         petWeight = 0;
     
        for(int i = 1; i<=3; i++)
        {
         //Scanner strIn=new Scanner(System.in);
        // Scanner intIn=new Scanner(System.in);

        System.out.println(" enter name of pet " + i);
        petName = input.next();
        System.out.println(" enter age of pet " + i);
        petAge = input.nextInt();
        System.out.println(" enter weight of pet " + i);
        petWeight = input.nextDouble();
         
     
            //object for 2nd pet class
            pet = new Pet(petName, petAge, petWeight);
          
             pet.getName();
             pet.getAge();
             pet.getWeight();
            //calls getDetails
            getDetails(pet);
        }
      
        // call getAverageAge
        getAverageAge();
      
        //call getAverageWeight
        getAverageWeight();
     
        //call display
        displayOutcome();
    }
       
        private static void getDetails(Pet pet)
    {
        // determin young
        if(pet.getAge() < youngAge)
        {
            youngestPet = pet.getName();
            youngAge = pet.getAge();
        }  
        else if(pet.getAge() > oldAge)
        {
            oldestPet = pet.getName();
            oldAge = pet.getAge();
        }
      
        //determins small and largest weight
        if(pet.getWeight() < smallWeight)
        {
            smallestPet = pet.getName();
            smallWeight = pet.getWeight();
        }  
        else if(pet.getWeight() > largeWeight)
        {
            largestPet = pet.getName();
            largeWeight = pet.getWeight();
        }
        // sum of ages
        ageSum += pet.getAge();
      
        //sum of weights
        weightSum += pet.getWeight();
    }
  
    //calculates average age
    private static void getAverageAge()
    {
        ageAverage = ((double)ageSum) / 5;
    }
    private static void getAverageWeight()
    {
        weightAverage = ((double)weightSum) / 5;
    }
  
    //displays outcome
    private static void displayOutcome()
    {
        System.out.println("Name of the smallest pet: " + smallestPet);
        System.out.println("Name of the largest pet: " + largestPet);
        System.out.println("Name of the oldest pet: " + oldestPet);
        System.out.println("Name of the youngest pet: " + youngestPet);
        System.out.println("Average weight of the five pets: " + weightAverage);
        System.out.println("Average age of the five pets: " + ageAverage);
    }
}



public class Pet {
    private String name;
    private int age; //in years
    private double weight;//in pounds
    public Pet() {
        name = "No name yet.";
        age = 0;
        weight = 0;
    }
    public Pet(String initialName, int initialAge,
            double initialWeight) {
        name = initialName;
        if ((initialAge < 0) || (initialWeight < 0)) {
            System.out.println("Error: Negative age or weight.");
            System.exit(0);
        } else {
            age = initialAge;
            weight = initialWeight;
        }
    }
    public void setPet(String newName, int newAge,
            double newWeight) {
        name = newName;
        if ((newAge < 0) || (newWeight < 0)) {
            System.out.println("Error: Negative age or weight.");
            System.exit(0);
        } else {
            age = newAge;
            weight = newWeight;
        }
    }
    public Pet(String initialName) {
        name = initialName;
        age = 0;
        weight = 0;
    }
    public void setName(String newName) {
        name = newName; //age and weight are unchanged.
    }
    public Pet(int initialAge) {
        name = "No name yet.";
        weight = 0;
        if (initialAge < 0) {
            System.out.println("Error: Negative age.");
            System.exit(0);
        } else
            age = initialAge;
    }
    public void setAge(int newAge) {
        if (newAge < 0) {
            System.out.println("Error: Negative age.");
            System.exit(0);
        } else
            age = newAge;
//name and weight are unchanged.
    }
    public Pet(double initialWeight) {
        name = "No name yet";
        age = 0;
        if (initialWeight < 0) {
            System.out.println("Error: Negative weight.");
            System.exit(0);
        } else
            weight = initialWeight;
    }
    public void setWeight(double newWeight) {
        if (newWeight < 0) {
            System.out.println("Error: Negative weight.");
            System.exit(0);
        } else
            weight = newWeight; //name and age are unchanged.
    }
    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
    public double getWeight() {
        return weight;
    }
    public void writeOutput() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age + " years");
        System.out.println("Weight: " + weight + " pounds");
    }
}

Explanation / Answer

you have initialized variables youngAge and smallWeight to zero.So at the time of comparision 0 is lowest and no new value can be assigned.So to correct thismake these two cahnges.


1.Before call of getDetails() in for loop put these lines

if(youngAge == 0)                                                                                                                                               
                   youngAge=pet.getAge();                                                                                                              
               
                if(smallWeight == 0)                                                                                                                      
                   smallWeight=pet.getWeight();     

2.in the   getDetils function in comparision operator for youngAge and smallWeight also replace '<' with '<=                                                                                                                                      

put if(pet.getAge() <= youngAge)

and if(pet.getWeight() <= smallWeight).


                                                                               

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote