Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java This lab will require you to create a small class hierarchy based on products in a furniture store. The parent class is FurnitureProduct which

Java

This lab will require you to create a small class hierarchy based on products in a furniture store. The parent class is FurnitureProduct which has the two subclasses, Table and Chair. These three classes will be in a package called furniturewarehouseitems. A test program is provided.

Concepts Covered

Creating packages Creating reusable classes Inheritance

Create a new project called Lab5Inheritance. In this project create the package furniturewarehouseitems. This is where the FurnitureProduct, Table, and Chair classes will go.

The documentation for these classes

furniturewarehouseitems Class Chair

java.lang.Object furniturewarehouseitems.FurnitureProduct furniturewarehouseitems.Chair

public class Chair extends FurnitureProduct

Class for a generic table product. Inherits basic product data from FurnitureProduct.

Field Summary Fields Modifier and Type Field and Description private boolean cushioned Denotes whether or not the chair has cushions. Constructor Summary Constructors Constructor and Description Chair(java.lang.String productNameIn, int productNumberIn, boolean cushionedIn) Chair constructor, takes all information required of a chair furniture product. Method Summary All MethodsInstance MethodsConcrete Methods Modifier and Type Method and Description java.lang.String toString() Used to get String representation of the Chair. Methods inherited from class furniturewarehouseitems.FurnitureProduct equals, setProductNumber Methods inherited from class java.lang.Object clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

Field Detail cushioned

private boolean cushioned

Denotes whether or not the chair has cushions. Constructor Detail Chair

public Chair(java.lang.String productNameIn, int productNumberIn, boolean cushionedIn)

Chair constructor, takes all information required of a chair furniture product.

Parameters: productNameIn - Name of the table. productNumberIn - Product number of the table. cushionedIn - True if chair has cushions, false if it does not.

Method Detail toString

public java.lang.String toString()

Used to get String representation of the Chair. See example output for format.

Overrides: toString in class FurnitureProduct Returns: String giving the Chairs's name, number, and if it has cushions.

furniturewarehouseitems Class FurnitureProduct

java.lang.Object furniturewarehouseitems.FurnitureProduct

Direct Known Subclasses: Chair, Table

public class FurnitureProduct extends java.lang.Object

The FurnitureProduct class is the base class for all products sold in a furniture store. It contains the two things that all furniture products have, a name and product number.

Field Summary Fields Modifier and Type Field and Description private static int MAXIMUM_PRODUCT_NUMBER The largest allowed value for a product number, set to 99999. private static int MINIMUM_PRODUCT_NUMBER The smallest allowed value for a product number, set to 10000. private java.lang.String productName The name of the product. private int productNumber The number of the product. Constructor Summary Constructors Constructor and Description FurnitureProduct(java.lang.String productNameIn, int productNumberIn) FurnitureProduct constructor used to create a new furniture product. Method Summary All MethodsInstance MethodsConcrete Methods Modifier and Type Method and Description boolean equals(java.lang.Object otherProduct) Determines if one FurnitureProduct is equal to another based solely on product number. void setProductNumber(int productNumberIn) Sets the product number of the product to the value passed if it is in range of the minimum and maximum, otherwise it does nothing. java.lang.String toString() Used to get String representation of the FurnitureProduct. Methods inherited from class java.lang.Object clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

Field Detail MINIMUM_PRODUCT_NUMBER

private static final int MINIMUM_PRODUCT_NUMBER

The smallest allowed value for a product number, set to 10000.

See Also: Constant Field Values

MAXIMUM_PRODUCT_NUMBER

private static final int MAXIMUM_PRODUCT_NUMBER

The largest allowed value for a product number, set to 99999.

See Also: Constant Field Values

productName

private java.lang.String productName

The name of the product. productNumber

private int productNumber

The number of the product. Constructor Detail FurnitureProduct

public FurnitureProduct(java.lang.String productNameIn, int productNumberIn)

FurnitureProduct constructor used to create a new furniture product. Takes product name and product number. Product number must be set using the setProductNumber method.

Parameters: productNameIn - Name of the new product. productNumberIn - Product number of the new product.

Method Detail setProductNumber

public void setProductNumber(int productNumberIn)

Sets the product number of the product to the value passed if it is in range of the minimum and maximum, otherwise it does nothing.

Parameters: productNumberIn - Desired product number.

equals

public boolean equals(java.lang.Object otherProduct)

Determines if one FurnitureProduct is equal to another based solely on product number.

Overrides: equals in class java.lang.Object Parameters: otherProduct - Reference to the product to compare for equality. Returns: True if products are equal, false if they are not equal.

toString

public java.lang.String toString()

Used to get String representation of the FurnitureProduct. See example output for format.

Overrides: toString in class java.lang.Object Returns: String giving product name and number.

furniturewarehouseitems Class Table

java.lang.Object furniturewarehouseitems.FurnitureProduct furniturewarehouseitems.Table

public class Table extends FurnitureProduct

Class for a generic table product. Inherits basic product data from FurnitureProduct.

Field Summary Fields Modifier and Type Field and Description private int seats Count of possible place settings at the table. Constructor Summary Constructors Constructor and Description Table(java.lang.String productNameIn, int productNumberIn, int seatsIn) Table constructor, takes all information required of a table furniture product. Method Summary All MethodsInstance MethodsConcrete Methods Modifier and Type Method and Description java.lang.String toString() Used to get String representation of the Table. Methods inherited from class furniturewarehouseitems.FurnitureProduct equals, setProductNumber Methods inherited from class java.lang.Object clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

Field Detail seats

private int seats

Count of possible place settings at the table. Constructor Detail Table

public Table(java.lang.String productNameIn, int productNumberIn, int seatsIn)

Table constructor, takes all information required of a table furniture product.

Parameters: productNameIn - Name of the table. productNumberIn - Product number of the table. seatsIn - Number of seats at the table.

Method Detail toString

public java.lang.String toString()

Used to get String representation of the Table. See example output for format.

Overrides: toString in class FurnitureProduct Returns: String giving the Table's name, number, and seats.

Given:

import furniturewarehouseitems.Table; import furniturewarehouseitems.Chair;

/** * Test class for FurnitureProduct subclasses, Table and Chair. Contains Magic Numbers for testing. */ public class FurnitureProductTest {

/** * Main method which creates instance of Table and array of Chairs and performs tests. * Allowed Checkstyle errors for Magic Numbers used for testing. * @param args Command line arguments (Unused) */ public static void main(String[] args) { Table diningTable = new Table("Bear's Dining Table", 10000, 4); Chair[] diningChairs = new Chair[4]; diningChairs[0] = new Chair("Too Hard Chair", 99999, false); diningChairs[1] = new Chair("Too Soft Chair", 9999, true); diningChairs[2] = new Chair("Just Right Chair", 100000, true); diningChairs[3] = new Chair("Baby's High Chair", 66666, false); printDiningSet(diningTable, diningChairs); System.out.println(" UH-OH! There should be incorrect product numbers! "); diningChairs[1].setProductNumber(77777); diningChairs[2].setProductNumber(88888); System.out.println("Product Numbers Fixed! "); printDiningSet(diningTable, diningChairs); } /** * Prints the contents of a dining set included Table and Chairs. * @param diningTable Table of the dining set. * @param diningChairs Array of Chairs of the dining set. */ public static void printDiningSet(Table diningTable, Chair[] diningChairs) { System.out.println("***Your Dining Set Includes***"); System.out.println(diningTable); for (int i = 0; i < diningChairs.length; i++) { System.out.println(diningChairs[i]); } }

}

The test program will run a number of tests on your classes. The exact expected output is shown below.

EXPECTED OUTPUT

***Your Dining Set Includes*** Product Name: Bear's Dining Table, Product Number: 10000, Number of seats: 4 Product Name: Too Hard Chair, Product Number: 99999, No cushions Product Name: Too Soft Chair, Product Number: 0, Has cushions Product Name: Just Right Chair, Product Number: 0, Has cushions Product Name: Baby's High Chair, Product Number: 66666, No cushions

UH-OH! There should be incorrect product numbers!

Product Numbers Fixed!

***Your Dining Set Includes*** Product Name: Bear's Dining Table, Product Number: 10000, Number of seats: 4 Product Name: Too Hard Chair, Product Number: 99999, No cushions Product Name: Too Soft Chair, Product Number: 77777, Has cushions Product Name: Just Right Chair, Product Number: 88888, Has cushions Product Name: Baby's High Chair, Product Number: 66666, No cushions

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

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions

Question

Analyze data in the order-to-cash process.

Answered: 1 week ago