Question
I'm trying to get this program how my teacher wants it, but I keep getting errors. This is what I got and the directions that
I'm trying to get this program how my teacher wants it, but I keep getting errors. This is what I got and the directions that she handed it out for it are below the program
public class Pet { private String name; private int age; //default constructor public Pet() { name = ""; age = 0; }//end default constructor //values constructor public Pet(String petName, int petAge) { name = petName; age = petAge; }//end values constructor //override toString method public String toString() { return ("Name = " + name + ",age = " + age); }//end toString //set method for both attributes public void setNameAge(String petName, int petAge) { name = petName; age = petAge; }//end setNameAge
public String getName() { return name; }//end getName public int getAge() { return age; }//end getAge }//end class Pet
__________________________________________________________________________________________________________________________________________
public class Dog { //declare additional attributes private int weight; private String breed; //default constructor public Dog() { super();//this gets the name and the age from the person weight = 0; breed = ""; }//default constructor //values constructor public Dog(String name, int age, int petWeight, String petBreed) { super(name, age); weight = petWeight; breed = petBreed; }//end values constructor public String toString() { return (super.toString() + " weight = " + weight + ", breed = " + breed); }//end toString
public int getWeight() { return weight; }//getWeight public String getBreed() { return breed; }//end getBreed public void setNameAgeWeightBreed(String petName, int petAge, int petWeight, String petBreed) { setName(petName, petAge); weight = petWeight; breed = petBreed; }//setNameAgeWeightBreed public int calcAge() { return 7 * getAge(); }//end calc }//end class Dog
___________________________________________________________________________________________________________________________________
import java.util.*;
public class Client { public static void main(String[] args) { System.out.println("***************Pet Class************* "); //instantiate pet1 and pet2 Pet pet1 = new Pet(); Pet pet2 = new Pet("Spot", 5); //print pet1 and pet2 System.out.println("PetOne: " + pet1.toString()); System.out.println("PetTwo: " + pet2.toString()); //use set method to give pet1 new values pet1.setNameAge("Max", 8); //print pet1 with get methods System.out.println("Pet Name = " + pet1.getName() + " Age = " + pet1.getAge()); System.out.println("***************Dog Class*************** "); //instantiate dog1 and dog2 Dog dog1 = new Dog(); Dog dog2 = new Dog("Lassie ", 3, 76, "Collie "); //method to calculate the age in dog years for dog2 System.out.println("The age in dog years is " + dog2.calcAge()); //print attributes for dog1 and dog2 System.out.println("Dog 1 information: " + dog1.toString()); System.out.println("Dog 2 information: " + dog2.toString()); //set methods to set new attributes for dog1 dog1.setNameAgeWeightBreed("Ginger ", 13, 35, "Beagle"); //Call the method to calculate the age in dog years for dog1 System.out.println("The age in dog years is " + dog1.calcAge()); //get methods to get the attributes for dog1 and print them System.out.println("Dog 1 information:"); System.out.println("Name = " + dog1.getName() + " Age = " + dog1.getAge() + " Breed = " + dog1.getBreed() + " Weight = " + dog1.getWeight()); }//end main }//end class
_________________________________________________________________________________________________________________________________
Java programming 5th edition chapter 10 Inheritance programs
Part 1
Create a class called Pet that has 2 instance variables, one for the pet's name and one for the pet's age (int type). It should have a default constructor and a values constructore. Also include a set method that sets both attributes, a get method for each attribute, and a method that redefines toString() to print the attributes.
Part 2
Create a Dog class that extends the Pet class above. It adds instance variables for the dog's breed, weight (int type), and age in dog years (this will be a calculated attribute). It should have a default constructor and a values constructor (age in dog years will be 0). Include one set method that sets the name, pet age, breed, and weight attributes, get methods that get each of the 3 new attributes, and a method that redefines toString() to print all attributes. Also, include a method to calculate the age in dog years by multiplying the pet age by 7. Make sure your methods don't repeat the code already written in the Pet class.
Part 3
Create a client program that uses both of the classes created above. Make it do the following in this order:
1. Instantiate an object of the Pet class called pet1 with the default constructor.
2. Instantiate an object of the Pet class called pet2 with the values constructor. Use Spot for the pet's name and 5 for the pet's age.
3. Use the Pet class print method to print pet1 and pet2.
4. Call the set method to reset the name and age for pet1. Use Max for the pet's name and 8 for the pet's age.
5. Use teh get methods to get the attributes for pet1 and print tehm in the client (not with the print method).
6. Instantiate an object of the Dog class called dog1 with the default constructor.
7. Instantiate an object of the Dog class called dog2 with the values constructor. Use Lassie for the pet's name, 3 for the pet's age, collie for the dog's breed, and 76 for the dog's weight.
8. Call the method to calculate the age in dog years for dog2.
9. Use the Dog class print method to print attributes for dog1 and dog2.
10. Use the set method to set new attributes for dog1. Use Ginger for the pet's name, 13 for the pet's age, beagle for the dog's breed, and 35 for the dog's weight.
11. Call the method to calculate the age in dog years for dog1.
12. Use the get methods to get the attributes for dog1 and print them in the client (not with the print method).
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started