Results 1 to 2 of 2

Thread: PetRecord and

  1. #1
    gnng is offline Member
    Join Date
    Mar 2011
    Location
    San Diego, CA
    Posts
    34
    Rep Power
    0

    Default PetRecord and

    We are give this first program and told to write a second program to do the following:
    Write a driver program with a main method to read in data for five Pets and display the following data: name of smallest pet, name of largest pet, name of oldest pet, name of youngest pet, average weight of the five pets, and average age of the five pets. Also, test to see if any of the five pets are the same. Be sure to include an example of this test when you PrintScreen your results.

    Here is the supplied code
    Java Code:
    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;
        }
    }
    Here is my start for the driver program. I instantly run into compiling issue "cannot find symbol method getWeight" - I don't understand this as I used the exact format for name and age, both which compile fine. I am thinking I am totally going in the wrong direction. Any help is appreciated.

    Gary

    Java Code:
    import java.util.*;
    
    public class PetRecordTest
    {
    public static void main(String[] args)
    {
     // Declare a variable that will be used to
          // reference an InventoryItem object.
          Dog dogOne;
    
          // The getData method will return a reference
          // to an InventoryItem object.
          dogOne = getData();
    
          // Display the object's data.
          System.out.println("Here is the data you entered:");
          System.out.println("Name: "
                             + dogOne.getName()
                             + "  Age: " + dogOne.getAge()
                             + " Weight: " + dogOne.getWeight());
    }
    
     public static Dog getData()
       {
          String name;   // To hold the dog name
          int age;     // To hold the age
          double weight;  // to hold the weight
    
          // Create a Scanner object for keyboard input.
          Scanner keyboard = new Scanner(System.in);
    
          // Get the dog name
          System.out.print("Enter the dog name: ");
          name = keyboard.nextLine();
    
          // Get the age
          System.out.print("Enter age: ");
          age = keyboard.nextInt();
    
           // Get the weight
          System.out.print("Enter weight: ");
          weight = keyboard.nextDouble();
    
          // Create an InventoryItem object and return
          // a reference to it.
          return new Dog(name, age, weight);
       }
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    What is a Dog class?

    Can you post your exact error message?

    I don't think that your instructor wants you to do what you're doing, including your current static method. Rather, perhaps he wants you to create an array of PetRecord objects and then using a Scanner and a for loop, enter information into each PetRecord object and place in the array. Then you can use other for loops to check the items in the array to see which is oldest, heaviest, etc...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •