SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Inheritance
Exercise:
Programming Projects
Question:8 | ISBN:9780132830317 | Edition: 5

Question

Programming Project 4.10 required adding an instance variable to the Pet class defined in Display 4.15 to indicate if the pet is a dog or cat. A better organization is to define Pet as a superclass of the Dog and Cat classes. This organization eliminates the need for an instance variable to indicate the type of the pet. Do or redo Programming Project 4.10 with inheritance. The acepromazine() and carprofen() methods should be defined in the Pet class to simply return 0. Override both methods in the Dog and Cat classes to calculate the correct dosage. Write a main method with appropriate tests to exercise the changes.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

PET CLASS

public class Pet {
  protected String name;  
  protected int age;
  protected double weight;
  
  Pet(){};
  Pet(String name,int age,double weight){
      this.age=age;
      this.weight=weight;
      this.name=name;
  }
  
  
  
  public double acepromazine()
	{
		return 0;
	}

	public double carprofen()
		{
			return 0;
		}
  
}
CAT CLASS

public class Cat extends Pet {
    
    Cat(){};
    Cat(String name,int age, double  weight){
        super(name,age,weight);
    }
    
    public double acepromazine()
	{
		
			return (weight / 2.2) * (0.03 / 10);
		
			
	}

	public double carprofen()
		{
			
				return (weight / 2.2) *  (0.5 / 12);

			
		}
        
        public String toString()
        {
       return ("\nThe Pet Cat Named: "+name+"\nWeight:"+weight+"\nAge:"+age+"\nAcepromazine:"+acepromazine()
               +"\nCarprofen:"+carprofen());     
        }
    
    
}
DOG CLASS

public class Dog extends Pet {
    
    Dog(){};
    Dog(String name,int age, double weight){
        super(name,age,weight);
    }
    
    public double acepromazine()
	{
		
			return (weight / 2.2) * (0.002 / 10);
		
			
	}

	public double carprofen()
		{
			
				return (weight / 2.2) *
					   (0.25 / 12);

			
		}
        
        public String toString()
        {
       return ("\nThe Pet Dog Named: "+name+"\nWeight:"+weight+"\nAge:"+age+"\nAcepromazine:"+acepromazine()
               +"\nCarprofen:"+carprofen());     
        }
    
    
}


public class TestMethod {


    public static void main(String[] args) {
        Cat cat1= new Cat("Tom",18,22.5);
        Cat cat2= new Cat("Jazzy",21,33.5);
        Dog dog1= new Dog("Hachi",8,16.4);
        Dog dog2= new Dog("Jon",7,12.5);
        
        System.out.println(cat1.toString());
        System.out.println(cat2.toString());
        System.out.println(dog1.toString());
        System.out.println(dog2.toString());
    }
    
}

OUTPUT

The Pet Cat Named: Tom
Weight:22.5
Age:18
Acepromazine:0.03068181818181818
Carprofen:0.4261363636363636

The Pet Cat Named: Jazzy
Weight:33.5
Age:21
Acepromazine:0.04568181818181818
Carprofen:0.6344696969696969

The Pet Dog Named: Hachi
Weight:16.4
Age:8
Acepromazine:0.0014909090909090907
Carprofen:0.15530303030303028

The Pet Dog Named: Jon
Weight:12.5
Age:7
Acepromazine:0.0011363636363636363
Carprofen:0.11837121212121211

2 1

Discussions

Nadir Ali Khan

public class Cat extends Pet {

.......

........

public double acepromazine()

{

return (weight / 2.2) * (0.002 / 10); //Correct For Cat Class

}

*************************************************************************************************************************

public class Dog extends Pet {

..........

...........

public double acepromazine()

{

return (weight / 2.2) * (0.03/ 10); //Correct For Dog Class

}

Post the discussion to improve the above solution.