Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hey, in Java, I tried doing these next couple steps in my program but keep getting errors. I think my syntax is wrong and need

Hey, in Java, I tried doing these next couple steps in my program but keep getting errors. I think my syntax is wrong and need help. I have a HomePets and Pets class and a Homepets Driver. My Pets class I figured out but its the HomePets class that I need help with.

What I have to do:

1. Implement two methods totalPrice and totalWeight where it returns the total price and total weight of the home pets respectively.

2. Write statements that prints the two methods mentioned in the previous step.

3. Implement getter bethods for dog and cat instance variables.

4, In the Driver, add statements that add 10 the price of myPet's dog and subtract 5 from the price of myPet's cat and then print the total price again. (The object created in Driver.)

5. In Driver, print myPets again to make sure that the prices have changed.

my HomePets class:

public class HomePets { private String ownerName; private Pet dog; private Pet cat; public HomePets(String homeName, Pet homeDog, Pet homeCat) { this.ownerName = homeName; this.dog = homeDog; this.cat = homeCat; } public String toString() { String output = " "; output += this.ownerName; output += this.dog; output += this.cat; return output; } public totalPrice() { } public totalWeight() { }

My Pet class:

public class Pet { private String name; private double price; private int weight; //constructor public Pet(String petName, double petPrice, int petWeight) { this.name = petName; this.price = petPrice; this.weight = petWeight; } public String toString() { String output = " "; output += this.name + " \t"; output += this.price + " dollars \t"; output += this.weight + " pounds \t"; return output; } public double getPrice() { return price; } public int getWeight() { return weight; }

public void changePrice (double amount) { if (amount < 0) this.price = getPrice() + amount; else this.price = getPrice() + amount; } }

my Driver:

public class HomePetsDriver { public static void main (String [] args) { Pet myDog = new Pet("Carter", 1000 , 30); Pet myCat = new Pet("Hunter", 500 , 50); HomePets myPets = new HomePets("Long: ", myDog, myCat); //myPets object System.out.println(); //testing toString System.out.println(myDog.toString()); System.out.println(myCat.toString() +" "); System.out.println(myPets.toString()); //myPets toString System.out.println(); //using changePrice myDog.changePrice(20.00); myCat.changePrice(-10.00); System.out.println(myDog); System.out.println(myCat); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Demystified

Authors: Andrew Oppel

1st Edition

0072253649, 9780072253641

More Books

Students also viewed these Databases questions

Question

How are members held accountable for serving in the assigned roles?

Answered: 1 week ago

Question

Have roles been defined and assigned?

Answered: 1 week ago