Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

create a project named Pets that has these three Java files: Pets.java (an initial version is supplied below) Person.java Dog.java The Person and Dog classes

image text in transcribedcreate a project named Pets that has these three Java files:

  • Pets.java (an initial version is supplied below)
  • Person.java
  • Dog.java

The Person and Dog classes should implement features shown in the UML class diagram and do the error checking described in the "Error Checking" section of the Program 2 description. Pets' main method should use asserts to thoroughly test all of the features of the Person and Dog classes.

Requirements

Create a project named Pets that includes the classes shown in the following UML class diagram.

In addition to writing the classes shown in the UML class diagram, also test the classes thoroughly with code written your Pets' main method.

image text in transcribed

Below is a sample main method that should run without throwing any assertion errors:

package pets; public class Pets { public static void main(String[] args) { // Person paul = new Person("Paul", 99.00); // Person ming = new Person("Ming", 1000.00); // // System.out.println(paul); Dog barkie = new Dog("Barkie", "poodle", 55.00); Dog show = new Dog("Daisy", "shepherd", 200.90); System.out.println(barkie); // System.out.println(""); // System.out.println("Ming before purchase:"); // System.out.println(ming); // ming.buy(barkie); // System.out.println("Ming after purchase:"); // ming.buy(show); // System.out.println(ming); // // System.out.println(""); // System.out.println("Ming is going to sell her dog to Susan:"); // System.out.println("Before the sale, Susan is..."); // Person susan = new Person("Susan", 300.00); // // System.out.println(susan); // // if (!susan.hasADog() && susan.canAfford(ming.getDog().getCost()) && ming.hasADog()) // susan.buy(ming.sell()); // // System.out.println("After the sale, Susan and Ming:"); // System.out.println(susan); // System.out.println(ming); // // assert paul.hasADog() == false; // assert paul.canAfford(5000.00) == false; // assert paul.canAfford(10.00) == true; // assert paul.sell() == null; // // Dog duke = new Dog("Duke", "great dane", 335.50); // Person prakash = new Person("Prakash", 3000.00); // // assert susan.buy(duke) == false; // cannot buy becase already has a dog // assert paul.buy(duke) == false; // cannot buy because cannot affort // assert prakash.buy(duke) == true; // // assert prakash.toString().equals("Prakash has $2,664.50 and a dog Duke, a great dane ($335.50)"); // assert paul.toString().equals("Paul has $99.00 and no dog"); // assert barkie.toString().equals("Barkie, a poodle ($55.00)"); System.out.println("DONE!"); } } 

The code above should generate this output:

Paul has $99.00 and no dog Barkie, a poodle ($55.00) Ming before purchase: Ming has $1,000.00 and no dog Ming after purchase: Ming has $945.00 and a dog Barkie, a poodle ($55.00) Ming is going to sell her dog to Susan: Before the sale, Susan is... Susan has $300.00 and no dog After the sale, Susan and Ming: Susan has $245.00 and a dog Barkie, a poodle ($55.00) Ming has $1,000.00 and no dog DONE! 

The code above should not throw an assertion exception when run with your Person and Dog classes. Make sure that you are running under a virtual machine configuration that has assertions enabled.

Error Checking

  • Dog constructor - only need error checking on the cost of the dog. Dog has to cost at least $1. If the value requested is less than $1, then set the cost to $1. Similarly, setCost cannot set the cost below $1.
  • The Dog toString method should return a String with exactly this format: Barkie, a poodle ($55.00) The format should has the same spacing, upper and lowercase letters, etc.
  • A Person is always constructed without a dog. A person's dollars must be greater than or equal to zero. If a request is made for a person with less than zero dollars, then set their dollars to zero.
  • The getDog method only returns a reference to a person's dog (it does not remove the dog from the person)
  • The sell method returns the a reference to the just-sold dog if the sale is successful. The sale is unsuccessful (and returns null) if the person has no dog to sell. On a successful sale the dog is removed from the person, the person's dollar amount goes up by the sale amount of the dog.
  • The buy method returns true if the buy is successful. The buy is unsuccessful if the person already has a dog (a person can only own one dog at a time), or if the person does not have enough money to buy the dog. The boolean false value is returned on an unsuccessful buy.
  • The Person toString method returns one of two possible strings, of the exact format shown here:
    • Susan has $300.00 and no dog
    • Susan has $245.00 and a dog Barkie, a poodle ($55.00)
Pets Person Dog +main +MIN DOLLARS -name: String -dollars: double +MIN COST -name: String -breed: String -cost: double +Person(String, double) +getName(): String +hasADog(): boolean +canAfford(double): boolean +getDog(): Dog +buy(Dog): boolean +sell(): Dog +toString(): String +Dog(String, String, double) +getName(): String +setName(String): void +getBreed(): String +getCost(): double +setCost(double): void +toString(): String

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

Students also viewed these Databases questions