Question

Project 4: Pet Store Objectives: Create multiple classes in an inheritance hierarchy Use an abstract class...

Project 4:

Pet Store Objectives:

Create multiple classes in an inheritance hierarchy Use an abstract class Use polymorphism Use an ArrayList Assignment

For this project, you get to write a simplified pet store inventory program in Java. To complete this, you will need to have the following classes:

Main class

PetStore class

Pet class

Bird class

Reptile class

Snake class

Turtle class

Main class: This class is a driver class for the PetStore class. It is not part of the tests, but it provides a guide for you to anticipate what some of the tests will require. Running main() should produce the following output (exactly): Happy Pets, Inc. currently has 4 pets Pet Store Inventory (4 pets): Pet #1 Species: reticulated python Cost: $123,456.78 Reptile (Needs an air temperature of 80.0 F) Snake (48.7 inches long) Pet #2 Species: speckled cape tortoise Cost: $98.76 Reptile (Needs an air temperature of 88.0 F) Turtle (Shell diameter: 2.1 inches) Pet #3 Species: hyacinth macaw Cost: $249.99 Bird (Wingspan: 48.0 inches) Pet #4 Species: amazon parrot Cost: $321.09 Bird (Wingspan: 15.5 inches) Pet Store Inventory (2 pets): Pet #1 Species: speckled cape tortoise Cost: $98.76 Reptile (Needs an air temperature of 88.0 F) Turtle (Shell diameter: 2.1 inches) Pet #2 Species: amazon parrot Cost: $321.09 Bird (Wingspan: 15.5 inches)

PetStore class: The PetStore class is a simple inventory system to keep track of Pets. This class must use an ArrayList to store references to Pet objects. (Note, one of the tests is that you used an ArrayList in this class.) It needs to provide at least the following methods: Constructor addPet() that takes a Pet as a parameter and adds that pet to the ArrayList sellPet() that takes a Pet as a parameter and removes that pet from the ArrayList. It needs to return true if found and false otherwise. getInventoryCount() that returns the current number of pets in the inventory Additionally, have a polymorphic method call in this class and label it with a comment and the exact phrase, "polymorphic method".

Pet class: This is a base class for all pets. The class needs to be an abstract class. It needs to provide at least the following methods: Constructor that takes a String (for the species) and a double (for the cost [in dollars]) as parameters setSpecies() that takes a String as a parameter getSpecies() that returns a String setCost() that takes a double as a parameter getCost() that returns a double

Bird class: This is a concrete class for storing information about a bird (that is a pet). It needs to provide at least the following methods: Constructor that takes a String (for the species), a double (for the cost [in dollars]), and a double (for the wingspan [in inches]) as parameters setWingspan() that takes a double as a parameter getWingspan() that returns a double toString() that returns a String in the following format: Species: Cost: $ Bird (Wingspan: inches)

Reptile class: This is another abstract class. It stores information about a reptile (that is a pet). Reptiles are cold blooded, so, we need to keep track of their required air temperature. This class needs to provide at least the following methods: Constructor that takes a String (for the species), a double (for the cost [in dollars]), and a double (for the air temperature [in degree Fahrenheit]) as parameters setAirTemperature() that takes a double as a parameter getAirTemperature() that returns a double

Snake class: This is another concrete class for storing information about a snake (which is a reptile). It needs to provide at least the following methods: Constructor that takes a String (for the species), a double (for the cost [in dollars]), a double (for the air temperature [in degree Fahrenheit]), and a double (for the length [in inches]) as parameters setLength() that takes a double as a parameter getLength() that returns a double toString() that returns a String in the following format: Species: Cost: $ Reptile (Needs an air temperature of F) Snake ( inches long)

Turtle class: This is another concrete class for storing information about a turtle (which is a reptile). It needs to provide at least the following methods: Constructor that takes a String (for the species), a double (for the cost [in dollars]) as parameters, a double (for the air temperature [in degree Fahrenheit]), and a double (for the shell's diameter [in inches]) setShellDiameter() that takes a double as a parameter, which is the diameter of the turtle's shell (in inches) getShellDiameter() that returns a double toString() that returns a String in the following format: Species: Cost: $ Reptile (Needs an air temperature of F) Turtle (Shell diameter: inches) Finally, include a completed rubric as a comment in your Main.java file.

Main.java

public class Main{
public static void main( String[] args ){
// Create some pet objects
Snake longestPython = new Snake( "reticulated python", 123456.78, 80.0, 48.6667 );
Turtle smallestTurtle = new Turtle( "speckled cape tortoise", 98.76, 88.0, 2.1 );
Bird macaw = new Bird( "hyacinth macaw", 249.99, 48.0 );
Bird polly = new Bird( "amazon parrot", 321.09, 15.545);

// create a pet store
PetStore happyPetsInc = new PetStore();

// add the pets to the pet store
happyPetsInc.addPet( longestPython );
happyPetsInc.addPet( smallestTurtle );
happyPetsInc.addPet( macaw );
happyPetsInc.addPet( polly );

// display information about the pet store
System.out.println("Happy Pets, Inc. currently has " + happyPetsInc.getInventoryCount() + " pets");
System.out.println( happyPetsInc.toString() );

// remove some pets
happyPetsInc.sellPet( macaw );
happyPetsInc.sellPet( longestPython );
  
// display (updated) information about the pet store
System.out.println( happyPetsInc.toString() );
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Pet class

Pet.java file


public abstract class Pet
{
private String species;
private double cost;

public Pet(String species, double cost) {
this.species = species;
this.cost = cost;
}

public String getSpecies() {
return species;
}

public void setSpecies(String species) {
this.species = species;
}

public double getCost() {
return cost;
}

public void setCost(double cost) {
this.cost = cost;
}
}

Bird class

Bird.java


import java.text.NumberFormat;
import java.util.Locale;


public class Bird extends Pet
{
private double wingspan;

public Bird( String species, double cost,double wingspan) {
super(species, cost);
this.wingspan = wingspan;
}

public double getWingspan() {
return wingspan;
}

public void setWingspan(double wingspan) {
this.wingspan = wingspan;
}

@Override
public String toString()
{
String str;
Locale locale = new Locale("en", "US");
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(locale);
str="Species: "+super.getSpecies();
str+="\nCost: "+currencyFormatter.format(super.getCost());
str+="\nBird (Wingspan: "+String.format("%.1f",wingspan) +")";
return str;
}
}

reptile class

reptile.java

public abstract class reptile extends Pet
{
private double airTemperature;

public reptile(double airTemperature, String species, double cost) {
super(species, cost);
this.airTemperature = airTemperature;
}

public double getAirTemperature() {
return airTemperature;
}

public void setAirTemperature(double airTemperature) {
this.airTemperature = airTemperature;
}
  
  
  
}

Snake class

Snake.java


import java.text.NumberFormat;
import java.util.Locale;

public class Snake extends reptile
{
private double length;

public Snake(String species,double cost, double airTemperature,double length)
{
super(airTemperature, species, cost);
this.length = length;
}

public double getLength() {
return length;
}

public void setLength(double length) {
this.length = length;
}

@Override
public String toString() {
String str;
Locale locale = new Locale("en", "US");
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(locale);
str="Species: "+super.getSpecies();
str+="\nCost: "+currencyFormatter.format(super.getCost());
str+=" Reptile (Needs an air temperature of "+String.format("%.1f",super.getAirTemperature())+" F";
str+="\nSnake ("+String.format("%.1f",length)+" inches long)";
return str;
}
  
  
}


Tutrtle class

Tutrtle.java


import java.text.NumberFormat;
import java.util.Locale;

public class Turtle extends reptile
{
private double shellDiameter;

public Turtle(String species, double cost, double airTemperature,double shellDiameter)
{
super(airTemperature, species, cost);
this.shellDiameter = shellDiameter;
}

public double getShellDiameter() {
return shellDiameter;
}

public void setShellDiameter(double shellDiameter) {
this.shellDiameter = shellDiameter;
}

@Override
public String toString() {
String str;
Locale locale = new Locale("en", "US");
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(locale);
str="Species: "+super.getSpecies();
str+="\nCost: "+currencyFormatter.format(super.getCost());
str+=" Reptile (Needs an air temperature of "+String.format("%.1f",super.getAirTemperature())+" F";
str+="\nTurtle (Shell diameter: "+String.format("%.1f",shellDiameter)+" inches)";
return str;
}
  
  
}

PetStore class

PetStore.java
import java.util.ArrayList;
public class PetStore
{
private ArrayList<Pet> inventory;

public PetStore()
{
inventory=new ArrayList<>();
}

public void addPet(Pet p)
{
inventory.add(p);
}
public boolean sellPet(Pet p)
{
if(inventory.contains(p))
{
inventory.remove(p);
return true;
}
return false;
}
public int getInventoryCount()
{
return inventory.size();
}

@Override
public String toString()
{
String str="";
for(int i=0;i<inventory.size();i++)
{
str+="Pet #"+(i+1)+"\n";
str+=inventory.get(i)+"\n\n";
}
return str;
}
}

Main class

Main,java

public class Main{
public static void main( String[] args ){
// Create some pet objects
Snake longestPython = new Snake( "reticulated python", 123456.78, 80.0, 48.6667 );
Turtle smallestTurtle = new Turtle( "speckled cape tortoise", 98.76, 88.0, 2.1 );
Bird macaw = new Bird( "hyacinth macaw", 249.99, 48.0 );
Bird polly = new Bird( "amazon parrot", 321.09, 15.545);

// create a pet store
PetStore happyPetsInc = new PetStore();

// add the pets to the pet store
happyPetsInc.addPet( longestPython );
happyPetsInc.addPet( smallestTurtle );
happyPetsInc.addPet( macaw );
happyPetsInc.addPet( polly );

Pet class

Pet.java file


public abstract class Pet
{
private String species;
private double cost;

public Pet(String species, double cost) {
this.species = species;
this.cost = cost;
}

public String getSpecies() {
return species;
}

public void setSpecies(String species) {
this.species = species;
}

public double getCost() {
return cost;
}

public void setCost(double cost) {
this.cost = cost;
}
}

Bird class

Bird.java


import java.text.NumberFormat;
import java.util.Locale;


public class Bird extends Pet
{
private double wingspan;

public Bird( String species, double cost,double wingspan) {
super(species, cost);
this.wingspan = wingspan;
}

public double getWingspan() {
return wingspan;
}

public void setWingspan(double wingspan) {
this.wingspan = wingspan;
}

@Override
public String toString()
{
String str;
Locale locale = new Locale("en", "US");
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(locale);
str="Species: "+super.getSpecies();
str+="\nCost: "+currencyFormatter.format(super.getCost());
str+="\nBird (Wingspan: "+String.format("%.1f",wingspan) +")";
return str;
}
}

reptile class

reptile.java

public abstract class reptile extends Pet
{
private double airTemperature;

public reptile(double airTemperature, String species, double cost) {
super(species, cost);
this.airTemperature = airTemperature;
}

public double getAirTemperature() {
return airTemperature;
}

public void setAirTemperature(double airTemperature) {
this.airTemperature = airTemperature;
}
  
  
  
}

Snake class

Snake.java


import java.text.NumberFormat;
import java.util.Locale;

public class Snake extends reptile
{
private double length;

public Snake(String species,double cost, double airTemperature,double length)
{
super(airTemperature, species, cost);
this.length = length;
}

public double getLength() {
return length;
}

public void setLength(double length) {
this.length = length;
}

@Override
public String toString() {
String str;
Locale locale = new Locale("en", "US");
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(locale);
str="Species: "+super.getSpecies();
str+="\nCost: "+currencyFormatter.format(super.getCost());
str+=" Reptile (Needs an air temperature of "+String.format("%.1f",super.getAirTemperature())+" F";
str+="\nSnake ("+String.format("%.1f",length)+" inches long)";
return str;
}
  
  
}


Tutrtle class

Tutrtle.java


import java.text.NumberFormat;
import java.util.Locale;

public class Turtle extends reptile
{
private double shellDiameter;

public Turtle(String species, double cost, double airTemperature,double shellDiameter)
{
super(airTemperature, species, cost);
this.shellDiameter = shellDiameter;
}

public double getShellDiameter() {
return shellDiameter;
}

public void setShellDiameter(double shellDiameter) {
this.shellDiameter = shellDiameter;
}

@Override
public String toString() {
String str;a
Locale locale = new Locale("en", "US");
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(locale);
str="Species: "+super.getSpecies();
str+="\nCost: "+currencyFormatter.format(super.getCost());
str+=" Reptile (Needs an air temperature of "+String.format("%.1f",super.getAirTemperature())+" F";
str+="\nTurtle (Shell diameter: "+String.format("%.1f",shellDiameter)+" inches)";
return str;
}
  
  
}

PetStore class

PetStore.java
import java.util.ArrayList;
public class PetStore
{
private ArrayList<Pet> inventory;

public PetStore()
{
inventory=new ArrayList<>();
}

public void addPet(Pet p)
{
inventory.add(p);
}
public boolean sellPet(Pet p)
{
if(inventory.contains(p))
{
inventory.remove(p);
return true;
}
return false;
}
public int getInventoryCount()
{
return inventory.size();
}

@Override
public String toString()
{
String str="";
for(int i=0;i<inventory.size();i++)
{
str+="Pet #"+(i+1)+"\n";
str+=inventory.get(i)+"\n\n";
}
return str;
}
}

Main class

Main,java

public class Main{
public static void main( String[] args ){
// Create some pet objects
Snake longestPython = new Snake( "reticulated python", 123456.78, 80.0, 48.6667 );
Turtle smallestTurtle = new Turtle( "speckled cape tortoise", 98.76, 88.0, 2.1 );
Bird macaw = new Bird( "hyacinth macaw", 249.99, 48.0 );
Bird polly = new Bird( "amazon parrot", 321.09, 15.545);

// create a pet store
PetStore happyPetsInc = new PetStore();

// add the pets to the pet store
happyPetsInc.addPet( longestPython );
happyPetsInc.addPet( smallestTurtle );
happyPetsInc.addPet( macaw );
happyPetsInc.addPet( polly );

// display information about the pet store
System.out.println("Happy Pets, Inc. currently has " + happyPetsInc.getInventoryCount() + " pets");
System.out.println( happyPetsInc.toString() );

// remove some pets
happyPetsInc.sellPet( macaw );
happyPetsInc.sellPet( longestPython );
  
// display (updated) information about the pet store
System.out.println( happyPetsInc.toString() );
}
}

// remove some pets
happyPetsInc.sellPet( macaw );
happyPetsInc.sellPet( longestPython );
  
// display (updated) information about the pet store
System.out.println( happyPetsInc.toString() );
}
}

Add a comment
Know the answer?
Add Answer to:
Project 4: Pet Store Objectives: Create multiple classes in an inheritance hierarchy Use an abstract class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The purpose of this is to use inheritance, polymorphism, object comparison, sorting, reading binary files, and...

    The purpose of this is to use inheritance, polymorphism, object comparison, sorting, reading binary files, and writing binary files. In this application you will modify a previous project. The previous project created a hierarchy of classes modeling a company that produces and sells parts. Some of the parts were purchased and resold. These were modeled by the PurchasedPart class. Some of the parts were manufactured and sold. These were modeled by the ManufacturedPart class. In this you will add a...

  • Create a new project in BlueJ and name it LastName-lab8-appstore, e.g., Smith-lab8-appstore. If your App class from Lab 4 is fully functional, copy it into the project. (You can drag a file from File...

    Create a new project in BlueJ and name it LastName-lab8-appstore, e.g., Smith-lab8-appstore. If your App class from Lab 4 is fully functional, copy it into the project. (You can drag a file from File Explorer onto the BlueJ project window.) Otherwise, use the instructor's App class: Create a new class using the "New Class..." button and name it App. Open the App class to edit the source code. Select and delete all the source code so that the file is...

  • Should be in C# Create an inheritance hierarchy that a bank might use to represent customers’...

    Should be in C# Create an inheritance hierarchy that a bank might use to represent customers’ bank accounts. All customers at this back can deposit (i.e. credit) money into their accounts and withdraw (i.e. debit) money from their accounts. More specific types of accounts also exist. Savings accounts, for instance, earn interest on the money they hold. Checking accounts, on the other hand, charge a fee per transaction. Create base class Account and derived classes SavingsAccount and CheckingAccount that inherit...

  • Please use Java only. Write the class TopResult, which keeps track of the best (highest numbered ...

    Please use Java only. Write the class TopResult, which keeps track of the best (highest numbered or highest ordered) result it has seen so far. The class will be a generic type, so the type of results it sees depends on how it is declared. TopResult Task: There are a number of situations in which we want to keep track of the highest value we've seen so far - a highest score, a most recent entry, a name which is...

  • Please use Java only. Write the class TopResult, which keeps track of the best (highest numbered ...

    Please use Java only. Write the class TopResult, which keeps track of the best (highest numbered or highest ordered) result it has seen so far. The class will be a generic type, so the type of results it sees depends on how it is declared. TopResult Task: There are a number of situations in which we want to keep track of the highest value we've seen so far - a highest score, a most recent entry, a name which is...

  • Additional code needed: PartA: BurgerOrder class (1 point) Objective: Create a new class that represents an...

    Additional code needed: PartA: BurgerOrder class (1 point) Objective: Create a new class that represents an order at a fast-food burger joint. This class will be used in Part B, when we work with a list of orders. As vou work through this part and Part B, draw a UML diagram of each class in using the UML drawing tool 1) Create a new Lab5TestProject project in Netbeans, right-click on the lab5testproject package and select New>Java Class 2) Call your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT