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:
Defining Classes I
Exercise:
Programming Projects
Question:10 | ISBN:9780132830317 | Edition: 5

Question

Your vet’s office is using the Pet class defined in Display 4.15 and would like to include a way to calculate the dosage amount for drugs that are commonly administered for dogs and cats. Make the following modifications to the class:

  • Add an instance variable that indicates if the type of the pet is a dog or a cat.

  • Modify the constructor and the set method to include the type of pet (i.e., dog or cat).

  • Add a method named that returns as a double the dosage in ml for the sedative acepromazine.

  • Add a method named that returns as a double the dosage in ml for the pain killer carprofen.

The dosage calculation is

is in pounds.

  • For acepromazine, use mg per , and mg per for dogs and for cats.

  • For carprofen, use mg per , and mg per for dogs and for cats.

Modify the main method in Display 4.16 to include tests of the new methods.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

//Pet.java
public class Pet
{
	private String name;
	private int age;
	private double weight;
	private boolean isPet;

	public Pet(String startName) {
		name = startName;
		age = 0;
		weight = 0;
		isPet = true;
	}

	public Pet(double startWeight) {
		name = "No name still";
		age = 0;
		if (startWeight < 0) {
			System.out.println("Error: Negative weight");
			System.exit(0);
		} else
			weight = startWeight;
		isPet = true;
	}

	

	public Pet(String startName, int ageBegins, double startWeight, boolean type) {
		name = startName;
		if (ageBegins < 0 || startWeight < 0) {
			System.out.println("Erro contains Negative age or weight");
			System.exit(0);
		} else {
			age = ageBegins;
			weight = startWeight;
		}
		isPet = type;
	}

	public void set(String newName, int newAge, double newWeight, boolean type) {
		name = newName;
		if (newAge < 0 || newWeight < 0) {
			System.out.println("Error: Negative age or 	weight");
			System.exit(0);
		} else {
			age = newAge;
			weight = newWeight;
			isPet = type;
		}
	}

	public double acepromazine()
	{
		if (isPet)
			return (weight / 2.2) * (0.03 / 10);
		else
			return (weight / 2.2) * (0.002 / 10);
	}

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

			else
				return (weight / 2.2) *
					   (0.25 / 12);
		}
}

//Modify Display 4.16 from the textbook. 
import java.util.*;

public class PetDemo {

	public static void main(String args[]) {

		Pet p = new Pet("Daniel Plainview");
		System.out.println("No records on your pet name ");
		Scanner keyboard = new Scanner(System.in);
		System.out.println("Enter the pet's name:");
		String name = keyboard.nextLine();
		System.out.println("Enter the pet's age:");
		int age = keyboard.nextInt();
		System.out.println("Enter the pet's weight:");
		double weight = keyboard.nextDouble();
		keyboard.nextLine();
		System.out.println("Is the pet a dog? (y/n):");
		boolean isDog;
		String choice = keyboard.nextLine();
		if (choice.equalsIgnoreCase("y"))
			isDog = true;
		else
			isDog = false;
		p.set(name, age, weight, isDog);

		System.out.println("Records as follows:");
		System.out.println(p);
		System.out.println("Acepromazine needed: " + p.acepromazine() + " ml");
		System.out.println("Carprofen needed: " + p.carprofen() + " ml");
	}
}

Result output:

No records on your pet name 
Enter the pet's name:sweety
Enter the pet's age:5
Enter the pet's weight:12
Is the pet a dog? (y/n):y
Records as follows:
Acepromazine needed: 0.01636363636363636 ml
Carprofen needed: 0.22727272727272724 ml

 

0 0

Discussions

Post the discussion to improve the above solution.