|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
I can't get this thing to run. It is an assignment that I'm working on and this is what the instructions say. and here is what I have.
INSTRUCTIONS: Create a new class called Cat that is devifed from PetRecord class The new class has the additional attributes of: breed - type String declawed - type boolean - true for has no claws, false for has claws Be sure our classes have a reasonable complement of constructors and accessor methods. 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) Write a driver 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 You are required to turn in 3 files. PetRecord.java Cat.java DriverProgram.java - you can name this as you like. It will use the other 2 class files. -------------------------------------------------- Here is what I have (However I can't figure out how to start a driver program. And my other ones won't work) CAT CLASS---- Code:
public class Cat101 extends PetRecord101
{
private String breed;
private boolean deClawed;
public void catinfo(String name, int age, double weight,
String initialBreed, boolean initialDeClawed);
{
breed = initialBreed;
deClawed = initialDeClawed;
}
public void set(boolean newDeClawed)
{
deClawed = newDeClawed;
}
public void setBreed(String newBreed)
{
breed = newBreed;
}
public String getBreed()
{
return breed;
}
public boolean getDeClawed()
{
return deClawed;
}
public void print()
{
System.out.println("name: " + getName() + " age: " + getAge() + " weight: " + getWeight() +
" breed: " + getBreed() + " deClawed: " + getDeClawed());
}
}
PETRECORD-=---------- Code:
import java.util.*;
import java.text.DecimalFormat;
public class PetRecord101
{
private String name;
private int age;//in years
private double weight;//in pounds
public static void main(String[] args)
{
Cat101 firstCat = new Cat101();
Cat101 secondCat = new Cat101();
Cat101 thirdCat = new Cat101();
firstCat101.catinfo("Sora", "3", "7.6");
secondCat101.catinfo("Riku", "1", "10.3");
thirdCat101.catinfo("Daisy", "5", "8.2");
}
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 PetRecord101(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 PetRecord101(String initialName)
{
name = initialName;
age = 0;
weight = 0;
}
public void set(String newName)
{
name = newName; //age and weight are unchanged.
}
public PetRecord101(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 PetRecord101(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 PetRecord101()
{
name = "No name yet.";
age = 0;
weight = 0;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public double getWeight()
{
return weight;
}
}
--------------------------------------------- I can't get it to run or compile. I don't understand. ANd how do I do the driver program? Thanks for the help in advance Last edited by MadCowDzz : February 1st, 2006 at 08:28 AM. Reason: added [code] tags |
|
#2
|
||||
|
||||
|
There are a couple of things going wrong. I'm going to tell you a couple I've spotted (but not all) to get you underway:
First of: remove the semi-colon ( after the signature of the catinfo method.In the main method (PetRecord101 class) you declare objects with the name firstCat, secondCat, etc. but you try to reference them using firstCat101, secondCat101,.. choose one. I don't understand the 101 you put behind all your classes btw, I'd suggest to remove all of them. Another thing in the main is that you are calling catinfo with 3 arguments where the real catinfo method has 5 arguments (catinfo should also be turned into a constructor to be clean..). To compile you need to do something like: "javac Cat.java PetRecord.java" If you move the main method from PetRecord to a third file, then that 3rd file is the 'driver' which 'starts/constructs' the other two. Hope this will get you to stop pulling your hair. If there are more questions after this don't hesitate to ask. Good luck Last edited by Icon : February 1st, 2006 at 01:39 AM. |
|
#3
|
|||
|
|||
|
Java Driver Program with Inheritance
Icon are you still around?... I was playing with your suggestions on this post and came close to making it work (i think).
Look forward to hearing from you. Quote:
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > HELP!!! I'm Pulling my hair |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|