Help some one please


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Help some one please

Hybrid View

  1. #1
    Join Date
    Aug 2005
    Posts
    9

    Help some one please

    I have to Create a new class called Cat that is devifed from PetRecord class

    using breed - type String
    declawed - type boolean - true for has no claws, false for has claws

    I need to have have a reasonable complement of constructors and accessor methods.

    for example:
    public void setBreed(String inBreed) used to set the breed
    public void set(Boolean inClaws) used to set claws or not
    (You must overload the set method to set deClawed value)

    I have to write a program that reads in 3 pets of type Cat and prints out the name and age of all cats with claws and over 3 years old.

    The following information should be read in:
    Name
    Age
    Weight
    Breed
    DeClawed

    I am required to turn in 3 files.
    PetRecord.java
    Cat.java
    petRecordStart

    I am having problems trying to get it to work. This is what I have for my three programs. Can anybody help me.


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


    /*This outputs the name, age and weight of the cats.*/

    public void writeOutput( )
    {
    System.out.println("Name: " + name);
    System.out.println("Age: " + age + " years");
    System.out.println("Weight: " + 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 set(String newName)
    {
    name = newName; /*age and weight are unchanged.*/
    }

    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 set(int newAge)
    {
    if (newAge < 0)
    {
    System.out.println("Error: Negative age.");
    System.exit(0);
    }
    else
    age = newAge;
    /*name and weight are unchanged.*/
    }

    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 set(double newWeight)
    {
    if (newWeight < 0)
    {
    System.out.println("Error: Negative weight.");
    System.exit(0);
    }
    else
    weight = newWeight; /*name and age are unchanged.*/
    }

    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;
    } /*end of methode petrecord*/
    } /*end of class petrecord*/


    public class Cat extends PetRecord

    {
    String CatsName;
    Bolean declawed;

    public void setBread(String newBreed)

    {
    Bread = new Breed;

    }

    public String getBreed()

    {
    return Breed;

    }

    } /*End of Cat class*/


    import java.util.*;

    public class petRecordStart
    {
    public static void main (String[] args)
    {
    cat myCat1 = new cat1();

    Scanner input = new Scanner(System.in);

    System.out.println("Enter a cats name:");
    String name = input.next();

    System.out.println("Enter cats age:");
    int age = input.nextInt();

    System.out.println("Enter cats weight:");
    double weight = input.nextDouble();

    System.out.println("Enter cats breed:");
    String breed = input.next();

    System.out.println("Is your cat declawed? True or False");
    boolean declawed = input.nextBoolean();

    myCat1.set(name, age, weight);
    myCat1.setBreed(breed);

    }

    }


    I really don't know what I am doing wrong. Can some one help please.

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    When you say that you have having trouble making it work, what does the compiler tell you, when you compile the program, if anything? What does the jre tell you when you try to execute the program, if anything?

    Your method main is not doing anything other than creating an instance of the Cat class - you are not asking it to output anything.


    If it appears that nothing is happening, you need to "make something happen" that you can see - some debugging statements to test your classes, test your methods.

  3. #3
    Join Date
    Aug 2005
    Posts
    9
    So how do I do that?

  4. #4
    Join Date
    Jul 2005
    Location
    the Netherlands
    Posts
    128
    Quote Originally Posted by jose
    Code:
    1  public class Cat extends PetRecord
    2  {
    3    String CatsName;
    4    Bolean declawed;
    5
    6    public void setBread(String newBreed)
    7    {
    8      Bread = new Breed;
    9    }
    10
    11   public String getBreed()
    12   {
    13     return Breed;
    14   }
    15 } /*End of Cat class*/
    What is a "Bolean"? (line 4)
    What is a "Bread"? (line 8)
    What is a "Breed"? (line 8, 13)

    This is more how it's supposed to look like:
    Code:
    public class Cat extends PetRecord
    {
        private boolean declawed;
        private String breed;
    
        public Cat() {
            super();
        }
    
        public void setBread(String newBreed) {
            breed = newBreed;
        }
    
        public String getBreed() {
            return breed;
        }
        
        // Rest of your methods (all methods from PetRecord are inhereted!!!).
    }

  5. #5
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    So how do I do that?
    If you are asking about the output -


    Code:
    myCat1.writeOutput();

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center