The Artima Developer Community
Sponsored Link •

Design Forum
Review Java program & help fix errors

1 reply on 1 page. Most recent reply: Feb 19, 2003 1:40 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Tina Smith

Posts: 2
Nickname: dessa
Registered: Feb, 2003

Review Java program & help fix errors Posted: Feb 8, 2003 11:11 PM
Reply to this message Reply
Advertisement
Requirements:
This is what I need to add to the program:
--Create a new class called Dog, derived from petRecord class. With additional attributes (breed)type string and (boosterShot)type boolean, true if the pet has had it's boostershot, false if not.
---write a driver program to test all methods, and write program that reads in five pets of type Dog and print the name and breed of all dogs that are over two years old and have had their booster shots.
//////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// ///////


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");
}

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;


{
PetRecord usersPet = new PetRecord("Stitch");
System.out.println("My records on your pet are inaccurate.");
System.out.println("Here is what they currently say:");
usersPet.writeOutput();

System.out.println("Please enter correct pet name:");
String correctName = SavitchIn.readLine();
System.out.println("Please enter corect pet age:");
int correctAge = SavitchIn.readLineInt();
System.out.println("Please enter correct pet weight:");
double correctWeight = SavitchIn.readLineDouble();
usersPet.set(correctName, correctAge, correctWeight);
System.out.println("My updated records now say:");
usersPet.writeOutput();

System.out.println("Correct pet name is Stitch:");
System.out.println("Correct pet age is 8:");

System.out.println("Correct pet weight is 40lbs:");

System.out.println("My updated records now say:");


}
}


public class Dog extends PetRecord
{

private int dogBreed;

public Dog()
{
super();
dogBreed = terrier;
}

public Dog(String breed, int boosterShot)
{
super(Initialbreed);
booster = initialboosterShot;
}

public void reset(String newName, int newbreed)
{

setName(newName);
boosterShot = newboosterShot;
}

public int getBreed()
{

return breed;
}

public void setboosterShot(int newboosterShot)
{
boosterShot = newboosterShot;
}

public void writeOutput()
{
System.out.println("breedName: " + getbreedName());
System.out.println("boosterShot: " + boosterShot);

}
public boolean equals(breed)

{
return (this.sameName(otherbreed)

&& (this.boosterShot== otherboosterShot.newBreed));
}

}


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Review Java program & help fix errors Posted: Feb 19, 2003 1:40 PM
Reply to this message Reply
> Requirements:
> This is what I need to add to the program:
> --Create a new class called Dog, derived from petRecord
> class. With additional attributes (breed)type string and
> (boosterShot)type boolean, true if the pet has had it's
> boostershot, false if not.
> ---write a driver program to test all methods, and write
> program that reads in five pets of type Dog and print the
> name and breed of all dogs that are over two years old and
> have had their booster shots.

Okay, so do it. And let us know what specific questions you have.

You can use the Artima SuiteRunner or JUnit for the "driver program to test all methods" instead of writing your own, by the way.

> <snipp>several gazillion gigabytes of left-justified code</snip>

Please use the [ java ] and [ /java ] tags, especially when posting a ton of code. See the gray Formatting Your Post box to the right when you are posting for instructions.

Flat View: This topic has 1 reply on 1 page
Topic: OOD and OOP Previous Topic   Next Topic Topic: Design mysql client

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use