im having hard time to get the output of the the ff. data: name of smallest pet,
name of largest pet, name of the oldest pet, name of the youngest pet, average weight of five pets and average age of the five pets?

this is what i did

import java.util.Scanner;

public class PetRecordTESTER
{
    public static void main(String [] args)

    {
        int age,age2, age3, age4, age5;
        String name, name2, name3, name4, name5;
        double weight, weight2, weight3, weight4, weight5, average;

        PetRecord she = new PetRecord();
        PetRecord she2 = new PetRecord();
        PetRecord she3 = new PetRecord();
        PetRecord she4 = new PetRecord();
        PetRecord she5 = new PetRecord();

        Scanner keyboard = new Scanner(System.in);

        //dog1
        System.out.println("Enter your first pet name:");
        she.setName(keyboard.nextLine());
        System.out.println("Enter you pet age:");
        she.setAge(keyboard.nextInt());
        System.out.println("Enter you pet weight:");
        she.setWeight(keyboard.nextDouble());

        keyboard.nextLine();
        //dog2
        System.out.println("Enter your second pet name:");
        she2.setName(keyboard.nextLine());
        System.out.println("Enter your pet age:");
        she2.setAge(keyboard.nextInt());
        System.out.println("Enter your pet weigh:");
        she2.setWeight(keyboard.nextDouble());


        keyboard.nextLine();
        //dog3
        System.out.println("Enter your third pet name:");
        she3.setName(keyboard.nextLine());
        System.out.println("Enter your pet age:");
        she3.setAge(keyboard.nextInt());
        System.out.println("Enter your pet weight:");
        she3.setWeight(keyboard.nextDouble());

        she3.getAge();
        she3.getWeight();
        keyboard.nextLine();
        //dog4
        System.out.println("Enter your fourth pet name:");
        she4.setName(keyboard.nextLine());
        System.out.println("Enter your pet age:");
        she4.setAge(keyboard.nextInt());
        System.out.println("Enter your pet weight:");
        she4.setWeight(keyboard.nextDouble());

        keyboard.nextLine();
        //dog5
        System.out.println("Enter your fifth pet name:");
        she5.setName(keyboard.nextLine());
        System.out.println("Enter your pet age:");
        she5.setAge(keyboard.nextInt());
        System.out.println("Enter your pet weight:");
        she5.setWeight(keyboard.nextDouble());

        keyboard.nextLine();
        System.out.println("#################################################");
        System.out.println("           Pet one");
        System.out.println(""+ she.toString() + she.getAge() + she.getWeight());
        System.out.println("");
        System.out.println("           Pet two");
        System.out.println(""+ she2.toString() + she2.getAge() + she2.getWeight());
        System.out.println("");
        System.out.println("           Pet three");
        System.out.println(""+ she3.toString() + she3.getAge() + she3.getWeight());
        System.out.println("");
        System.out.println("           Pet four");
        System.out.println(""+ she4.toString() + she4.getAge() + she4.getWeight());
        System.out.println("");
        System.out.println("           Pet five");
        System.out.println(""+ she5.toString() + she5.getAge() + she5.getWeight());
        System.out.println("");
        System.out.println("");
        System.out.println("#################################################");

        System.out.println("Smallest pet:");                    ?
        System.out.println("Largest pet:");                                                     System.out.println("Name of Oldest pet:");
        System.out.println("Name of the youngest pet:");
        System.out.println("Average weight of five pets:");
        System.out.println("Average age of the five pets");
        }
}

Recommended Answers

All 3 Replies

Work it out on paper first. Suppose you have five index cards, each one has the information about the pets, and you have a piece of scratch paper. You can look at one card at a time, but you can look at them as many times as you like. On your scratch paper, you can make a place to write down the name of a card that you want to keep in mind, but you can only remember one card there at a time. You have another section of your scratch paper where you can write down your "output", but once you've written something down there, you can't erase it. This is enough to do all but the average weight and age (we'll get to that).

What steps do you take to put the names of the largest, the oldest, and the youngest pets in the output section, in that order? Remember, for this exercise you have no memory, you can only use what's written down.

i already have my class here
my problem is how to compute the average and etc.

public class PetRecord
{
    private String name;
    private int age;//in years
    private double weight;//in pounds

    public String toString( )
    {
        return ("Name: " + name + " Age: " + age + " years"
                       + "\nWeight: " + weight + " pounds");
    }

    public PetRecord(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 set(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 PetRecord(String initialName)
    {
        name = initialName;
        age = 0;
        weight = 0;
    } 

    public void setName(String newName)
    {
        name = newName; 
    }

    public PetRecord(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;
    }

    public PetRecord(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;
    }

    public PetRecord( )
    {
        name = "No name yet.";
        age = 0;
        weight = 0; 
    }

    public String getName( )
    {
        return name;
    }

    public int getAge( )
    {
        return age;
    }

    public double getWeight( )
    {
   return weight;
    }
}

Right, you have the class but you need the logic for figuring out which is the oldest of the pets, and so on.
How would you do that if the data were on index cards, and you were working under the restrictions I described above?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.