Thursday, May 14, 2009

Dog

  1. Create a new class called Dog that is derived from the Pet class given in Listing 6.1 of Chapter 6 on pages 347 - 349. The new class has the additional attributes of breed (type String) and boosterShot (type boolean), which is true if the pet has had its booster shot, and false if not. Be sure your classes have a reasonable complement of constructors and accessor methods.

You will need all three Java programs for execution: Pet.java, Dog.java, DogDemo.java. .

1. Add two instance variables: String breed and boolean boosterShot; (10)

2. Your derived class name is Dog, therefore your file name is Dog.java.

3. You need to define three constructors: (30)

· Without any parameters, codes given below:

public Dog()

{

super();

breed = "None";

boosterShot = false; // Default: presume no shot

· With five parameters name, age, weight, breed, and boosterShot;

· With four parameters name, age, weight, and breed.

4. Give a new definition of writeOutput() (30)

· This is an overridden method.

· The original output from the base class (don’t forget the super keyword).

· Output breed

· Output the dog has (or has not) had a booster shot.

5. Write three mutator methods: (30)

· To reset name, age, weight, breed, and boosterShot status. The instance variables in the base class can be accessed through base class’s set method. Again don’t forget the super keyword here.

· To reset the breed only.

· To reset boosterShot value.

6. Write two accessor methods: (20)

· To get breed info.

· To get info on boosterShot status.

7. You need to document both programs follow my previous suggestions. (10)

· Your code should be written in a way that is easy to read.

· Add comments on what software/IDE you used in developing your program.

· Add compilation instructions. E.g., javac Dog.java. and javac DogDemo.java

· Add executing instructions. E.g., java DogDemo

· Programmer’s name.

· Preconditions and postconditions.

  1. Write a test (demo or driver) program (file name: DogDemo.java) that reads in five pets of type Dog from the keyboard and at the end of the input the program prints out the name and breed of all dogs that are over two years old and have not had their booster shots. Note that your writeOutput() methods in the Pet class and Dog class determine the output format.

· You will use an array to hold all dogs info (check your StudentRecord class).

· Declare the array size to be 5.

· Use a looping method to enter info for 5 dogs.

· Be sure to check your index during your input loop so you don’t get an array index out of bound exception during run time.

· After each loop, echo all info to the screen as shown here:

Enter first dog’s name:

Freddie

Enter dog’s age in years:

5

Enter dog’s weight in pounds:

10.5

Enter breed:

Dachshund

Has the dog had a booster shot within 2 years?

Enter y (or Y) for yes and anything else for no.

N

You have entered the following dog info:

Name: Freddie

Age: 5 years

Weight: 10.5 pounds

Bread: Dachshund

Has NOT had a booster shot.

Enter second dog’s name:

………and so forth……

· At the end of the program, print all the dogs’ information to the screen in ascending order by their ages.

· Sample screen output listed below.

Name Age Weight Breed Boostershot

. Freddie 2 10.5 Dachshund false

yy 8 20.0 B2 true

zz 10 15.0 B4 true

aa 12 10.0 B5 true

dd 16 30.0 B2 true

· A sorting method that sorts an array of type Dog for your reference:

public static void insertionSort(int numberDogs, Dog a[] )

{

int in, out;

Dog temp = null;

for(out=1; out // out is dividing line

{

temp = a[out]; // remove marked person

in = out; //index of the end of sorted region

while(in>0 && a[in-1].getAge() > temp.getAge() ) { // until smaller one found

a[in] = a[in-1]; // shift item to the right

--in; // go left one position

}

a[in] = temp; // insert marked item

} // end for

} // end insertionSort()

Note: the numberDogs is the number of element of the Dog array have actually filled with data. The Dog array a[] can be a size of 5 if you initially created 5 elements with the following Java statement:

Dog a[] = new Dog[5];

The length of the a[] array is 5, and all 5 elements at this point are all null. If you try to sort this array, you will get a null pointer exception! You have to use the keyword ‘new’ to instantiate each element of the Dog array. For example the 1st element of the a[] array:

a[i] = new Dog(…);







/**
Class for basic pet data: name, age, and weight.
*/
public class Pet
{
private String name;
private int age; //in years
private double weight;//in pounds

public Pet(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 setPet(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 Pet(String initialName)
{
name = initialName;
age = 0;
weight = 0;
}

public void setName(String newName)
{
name = newName; //age and weight are unchanged.
}

public Pet(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;
//name and weight are unchanged.
}

public Pet(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; //name and age are unchanged.
}

public Pet( )
{
name = "No name yet.";
age = 0;
weight = 0;
}

public String getName( )
{
return name;
}

public int getAge( )
{
return age;
}

public double getWeight( )
{
return weight;
}

public void writeOutput( )
{
System.out.println("Name: " + name);
System.out.println("Age: " + age + " years");
System.out.println("Weight: " + weight + " pounds");
}
}

No comments:

Post a Comment