I have an assignment below:

1. Create a class called Furniture with the following attributes:
furnitureType – a String
modelNumber – a String
price – double
quantitySold – int
totalCharge - double.

Provide the following:
1. A non-default constructor for Furniture
2. Settors and gettors for price and furnitureType (don’t worry about settors and gettors for the others.
3. A method called calculateCharge() that calculates the total charge as quantitySold times price.
4. A toString() method that displays all the attributes and the total charge.

Furniture has two children: HandMade and MachineMade.

HandMade has an additional attribute, process, which refers to the type of process used in making the furniture. Note that process is of type String.

MachineMade has two additional attributes: one for manufacturer (String ) and one for country (also a String).

HandMade adds an extra 10% to the total charge if furnitureType is “wood” and the process is “wash”.

MachineMade gives a discount of 15% if the total charge is over $1500.

Write a Java application that includes the following:

1. Write the nondefault constructor for HandMade.
2. Write the nondefault constructor for MachineMade.
3. Write the calculateCharge for HandMade.
4. Write the calculateCharge for MachineMade.
5. toString() method for both HandMade and MachineMade.
6. Create a test class called FurnitureTest. Declare an array, furnitureArray, that can hold five instances of Furniture (either HandMade furniture or MachineMade furniture).
7. Insert in the 4th cell of the array the HandMade piece of furniture with process “paint”, type “plastic”, price $175.50, model E2302, quantity sold 7.
8. Insert in the 1st cell of the array the MachineMade piece of furniture with type “laminate”, model “KX225”, price $249.95, quantity sold 12, country “USA”, and manufacturer “Bassett”.
9. Populate the remaining cells furnitureArray with some Furniture objects of your choice and write the code necessary to polymorphically calculate and print/display each instance’s information to the system prompt or to a message dialog.

Here's what I have so far:

Furniture.java

public class Furniture
{
	private String furnitureType;
	private String modelNumber;
	private double price;
	private int quantitySold;
	private double totalCharge;

	public Furniture()
	{
	
	}

	public void setFurnitureType(String furnitureType)
	{
		this.furnitureType = furnitureType;
	}

	public String getFurnitureType()
	{
		return furnitureType;
	}

	public void setPrice(double price)
	{
		this.price = price;
	}
	
	public double getPrice()
	{
		return price;
	}

	public String toString()
	{
		return "Furniture Type: " + furnitureType +
			"\nModel Number: " + modelNumber +
			"\nPrice: " + price +
			"\nQuantity Sold: " + quantitySold + 
			"\nTotal Charge: " +  totalCharge;
	}
}

HandMade.java

public class HandMade extends Furniture
{
	private Sting process;

	public HandMade()
	{
	
	}

	public double calculateCharge()
	{
		if(process == wash) && (furnitureType == wood)
		   return quantitySold * price + (price * 0.10);
		else
		   return quantitySold * price;
	}

	public String toString()
	{
		return "Furniture Type: " + furnitureType +
			"\nModel Number: " + modelNumber +
			"\nPrice: " + price +
			"\nQuantity Sold: " + quantitySold + 
			"\nTotal Charge: " +  totalCharge;
	}
}
}

FurnitureTest.java

public class FurnitureTest
{
	public static void main(String[] args)
	{

	
		Furniture[] furnitureArray = new Furniture[5];


	}
}

How can I populate the array and assign the test information to furniture type, model, etc?

Also, i'm not sure if my code is complete, am I missing anything?

Thanks guys!!

Recommended Answers

All 10 Replies

Use

furniture[the_array_number] = new Furniture();

to initialize the array item....

i got that part but how do I do this..

7. Insert in the 4th cell of the array the HandMade piece of furniture with process “paint”, type “plastic”, price $175.50, model E2302, quantity sold 7.

8. Insert in the 1st cell of the array the MachineMade piece of furniture with type “laminate”, model “KX225”, price $249.95, quantity sold 12, country “USA”, and manufacturer “Bassett”.


Can someone help me finish this program? I feel like I am very close..

say you want to set the price of the Furniture object held on 4th cell of your furniture array.

you invoke the setPrice method of that object, that is:

furniture[3].setPrice(price)//that's it. note, though, that furniture[3] is 4th cell, since first is furniture[0]

is this what you need help with? I can't see a setType method anywhere, so I took an example setting the price.

P.S. you could also do this in your constructor method for Furniture class, but in that case you wouldn't need your set-methods. since you have the methods, I assume you are expected to do this not using your constructor method.

Thanks for the repliy bibiki. I think i get where you are going.. the problem says:

6. Create a test class called FurnitureTest. Declare an array, furnitureArray, that can hold five instances of Furniture (either HandMade furniture or MachineMade furniture).

7. Insert in the 4th cell of the array the HandMade piece of furniture with process “paint”, type “plastic”, price $175.50, model E2302, quantity sold 7.

Im trying to figure out how to make these objects go into the array and display with my toString method.

It's a problem from the polymorphism chapter, right now I only have created the HandMade class which is a child of Furniture..

alright,
when you declared your array, you said that it will hold Furniture objects. and then, you should assign to each cell on your array an actual object. since your HandMade and MachineMade classes both extend your Furniture class, you are safe in assigning to furniture[3] a HandMade (or MachineMade) object. so you are safe coding as follows:

furniture[3] = new HandMade();

//after you have done the above, you proceed with:

//invoke the method that sets the process for your furniture[3] object
//invoke the method that sets the type for your furniture[3] object
//invoke the method that sets the price for your furniture[3] object
//invoke the method that sets the model for your furniture[3] object
//invoke the method that sets the quantity for your furniture[3] object

//and finally, invoke the toString method for your furniture[3] object
furniture[3].toString();

I hope this helps

However my instructor noted that he wants Settors and gettors for price and furnitureType only

you have that. your settors are your set-methods (setPrice) and your gettors are your get-methods (getPrice) for Furniture type, but your HandMade and MachineMade are Furniture types as well, because they extend Furniture type... don't get discouraged, you have the solution above, it's easy. take a look at your code and replies!

so, your Furniture class's settors and gettors also 'belong' to your HandMade and MachineMade classes.

Thanks I think i'm getting closer, when I try and run this, I get an error saying the varaible is not found but I thought it would pass through parameter?

public class FurnitureTest extends Furniture
{
	public static void main(String[] args)
	{
	
		Furniture[] furnitureArray = new Furniture[5];

		furnitureArray[3] = new HandMade();
		furnitureArray[3].setPrice(101.50);
		furnitureArray[3].setFurnitureType(plastic);
		furnitureArray[3].toString();
		

	}
}

The instructor wants all the objects to be passed through parameters i am assuming, but how can i pass the rest like quantity sold and model without gettors and settors?

ok, your line 5 on your HandMade class is your constructor method for HandMade objects. however, to effectively do what you want to, you'd need to use the 'super' java keyword. just go to the chapter on your book on polymorphism, find info on using 'super' keyword, and I'm sure you'll figure it out. you might also need to read a little about using constructor methods, but it should not be difficult. It's 2 in the morning where I am so I need to sleep. good night.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.