?
Solved

Need help on my Driver Class for PetRcord and Cat Class

Posted on 2005-04-18
3
Medium Priority
?
595 Views
Last Modified: 2012-08-13
My PetRecord and Cat Class are compiling, but they all are not running. I am getting errors when compiling the driver program.

Cat class
import java.util.*;

public class Cat

{
     public String breed,initialBreed;
     public boolean declawed;
     public Cat()
     {
         super();
         breed = "None";
         declawed = false;
     }

public Cat(String initialName, int initialAge, double initialWeight, String

initialBreed, boolean declawed)
{
    super(initialName, initialAge, initialWeight);
    this.initialBreed = initialBreed;
    this.declawed = declawed;
}

public void reset(String nextName, int nextAge, double nextWeight, String nextBreed,

boolean nextDeclawed)
{
    super.set(nextName, nextAge , nextWeight);
    breed = initialBreed;
    declawed = nextDeclawed;
}
public String getBreed()
{
    return breed;
}

public void setBreed(String nextBreed)
{
   breed = nextBreed;
}

public boolean getDeclawed()
{
    return declawed;
}

public void setDeclawed(boolean nextDeclawed)
{
    declawed = nextDeclawed;
}
public void Function()
{
   if((age>3) && (declawed == false))
   {
         System.out.println("Here are the names and ages");
         System.out.println("of all the cats you entered over 3 years old");
         System.out.println("and that have not been declawed");
         System.out.println("Name: "+name);
         System.out.println("Age: "+age);
   }

}

}

PetRecord:

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

public void writeOutput()
{
System.out.println("Name: " + name);
System.out.println("Age: " + age + " years");
System.out.println("Weight: " + weight + " pounds");
}
/** Creates a new instance of Class */
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;
}
}

Driver:

import java.util.Scanner;
Scanner keyboard;
keyboard = new Scanner(System.in);
int numberOfCats = 3;
Cat infoCats = new Cat[numberOfCats];

public class CatDriver
{

     public static void main(String[] args)
          {

          /*The following will be output to the screen which describes what
          input is wanted from the user and what the final output consists of*/

          System.out.println("This program will will require you to input");
          System.out.println("certain information for 3 different cats.\n");

          System.out.println("It will then provide you with a summary only");
          System.out.println("of the cats who are over 3 years old and that");
          System.out.println("have not been declawed\n");

          //Information for first cat

for(int i = 0; i < numberOfCats; i++) {
             System.out.println("Please enter the name of your cat number " + i + ":");
            String correctName = keyboard.nextInt();
             System.out.println("Please enter the age of your cat number " + i + ":");
             int correctAge = keyboard.nextInt();
             System.out.println("Please enter the weight of your cat number " + i + ":");
             double correctWeight = keyboard.nextInt();
             System.out.println("Please enter the breed of your cat number " + i + ":");
             String correctBreed = keyboard.nextInt();
             System.out.println("Is this cat declawed? Please enter T for true or F for false");
             boolean catDeclawed = keyboard.nextInt();

             System.out.println(""); //Places a blank line between cats for neatness
             infoCats[i] = new Cat(catName,catAge, catWeight,catBreed,catDeclawed);
     }


        System.out.println("The name and age of all cats with claws");
        System.out.println("that are over 3 years old are as follows:");

     boolean noCatsWithClaws = true;
     for(int i = 0; i < numberOfCats; i++)
          if(infoCats[i].getAge()>3 && infoCats[i].getDeclawed()) {
                  System.out.println(""); //Places a blank line between cats for neatness
               infoCats[i].writeOutput();
               noCatsWithClaws = false;
          }
     }
     if(noCatsWithClaws) {
           System.out.println("You have entered no cats that fit the output criteria");
     }
    }

     PLEASE HELP! THANKS!      



0
Comment
Question by:chilax4u
  • 2
3 Comments
 
LVL 92

Accepted Solution

by:
objects earned 1900 total points
ID: 13804082
you need to move those variable declarations inside your main:

import java.util.Scanner;

public class CatDriver
{

     public static void main(String[] args)
          {

Scanner keyboard;
keyboard = new Scanner(System.in);
int numberOfCats = 3;
Cat infoCats = new Cat[numberOfCats];

          /*The following will be output to the screen which describes what
          input is wanted from the user and what the final output consists of*/

          System.out.println("This program will will require you to input");
          System.out.println("certain information for 3 different cats.\n");

          System.out.println("It will then provide you with a summary only");
          System.out.println("of the cats who are over 3 years old and that");
          System.out.println("have not been declawed\n");

          //Information for first cat

for(int i = 0; i < numberOfCats; i++) {
             System.out.println("Please enter the name of your cat number " + i + ":");
            String correctName = keyboard.nextInt();
             System.out.println("Please enter the age of your cat number " + i + ":");
             int correctAge = keyboard.nextInt();
             System.out.println("Please enter the weight of your cat number " + i + ":");
             double correctWeight = keyboard.nextInt();
             System.out.println("Please enter the breed of your cat number " + i + ":");
             String correctBreed = keyboard.nextInt();
             System.out.println("Is this cat declawed? Please enter T for true or F for false");
             boolean catDeclawed = keyboard.nextInt();

             System.out.println(""); //Places a blank line between cats for neatness
             infoCats[i] = new Cat(catName,catAge, catWeight,catBreed,catDeclawed);
     }


        System.out.println("The name and age of all cats with claws");
        System.out.println("that are over 3 years old are as follows:");

     boolean noCatsWithClaws = true;
     for(int i = 0; i < numberOfCats; i++) {
          if(infoCats[i].getAge()>3 && infoCats[i].getDeclawed()) {
                  System.out.println(""); //Places a blank line between cats for neatness
               infoCats[i].writeOutput();
               noCatsWithClaws = false;
          }
     }
     if(noCatsWithClaws) {
           System.out.println("You have entered no cats that fit the output criteria");
     }
    }
}
0
 
LVL 9

Assisted Solution

by:cyberdevil67
cyberdevil67 earned 100 total points
ID: 13804083
Hi chilax4u,

Whats the error message, the code you supplied some methods are closing the smooth brackets off () for examplie

public Cat(String initialName, int initialAge, double initialWeight, String

Is that a paste problem or you haven't closed it

Cheers!
0
 
LVL 92

Expert Comment

by:objects
ID: 13804096
>   infoCats[i] = new Cat(catName,catAge, catWeight,catBreed,catDeclawed);


that should also be:

  infoCats[i] = new Cat(correctName,correctAge, correctWeight,correctBreed,catDeclawed);
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question