Carpet Calculator Problem using aggregation
Hi this is what i am supposed to do:
RoomDimension: First, you should create a class named RoomDimension that has two fields: one for the length of the room, and one for the width. The RoomDimension class should have a method that returns the area of the room.
RoomCarpet : Next you should create a RoomCarpet class that has a RoomDimension object as a field. It should also have a field for the cost of the carpet per square foot. The RoomCarpet class should have a method that returns the total cost of the carpet. Use the UML diagram in figure 9-21 (pg. 599) as a guide to creating these classes.
RoomPriceDemo : Finally, you will create a RoomPriceDemo application program that asks the user to enter:
• the dimensions of a room
• and the price per square foot of the desired carpet
Your program should then calculate and display the total cost of the carpet.
here is my code:
HTML Code:
package Program_5;
public class RoomDimensions {
private int roomLength=0;
private int roomWidth=0;
public void setRoomLength(int roomlength) {
this.roomLength = roomlength;
}
public void setRoomWidth(int roomwidth) {
this.roomWidth = roomwidth;
}
public RoomDimensions() {
}
public int getRoomArea() {
return roomLength*roomWidth;
}
}
Other class
HTML Code:
package Program_5;
public class RoomCarpet{
private RoomDimensions thisRoomDimension;
final int carpetUnitCost=8;
public void setRoomDimension(RoomDimensions thisroomdimension) {
this.thisRoomDimension = thisroomdimension;
}
public int getTotalAMount()
{
return thisRoomDimension.getRoomArea()* carpetUnitCost;
}
}
Here is my demo:
HTML Code:
package Program_5;
import java.util.Scanner;
public class RoomPriceDemo {
public static void main(String[] args) {
int roomLength;
int roomWidth;
//Create Scanner object
Scanner keyboard= new Scanner(System.in);
System.out.println("Enter The Length Of The Room");
roomLength= keyboard.nextInt();
System.out.println("Enter The Width Of The Room");
roomWidth = keyboard.nextInt();
RoomDimensions roomd = new RoomDimensions();
roomd.setRoomLength(roomLength);
roomd.setRoomWidth(roomWidth);
//instantiating carpet class
RoomCarpet room1 = new RoomCarpet();
//function call to set dimensions
room1.setRoomDimension(roomd);
//displaying data
System.out.println("Room dimensions: ");
System.out.println("Length: " + roomLength + " Width: " + roomWidth +
"Area: " + room1.getRoomArea());
System.out.println("Carpet cost:$" + room1.getTotalAMount());
}
}
my problem is my room1.getRoomArea()); print statement is not working. everything else seems correct but when i try and run it it gives me an error at the room1.getRoomArea()): method in the System.out.println is underlined red and that is the only problem i have and cant figure out how to fix it
Re: Carpet Calculator Problem using aggregation
What is the actual error that the code is giving you?
Quote:
Originally Posted by gpelefty90
method in the System.out.println is underlined red
Not very useful..
As a note, you should have a constructors in your RoomDimension class that allows you to begin with giving values to the length and width, as shown below.
Code Java:
public RoomDimensions(int length, int width)
{
roomLength = length;
roomWidth = width;
}
That way for creating a RoomDimensions, you can just
Code Java:
RoomDimensions aRoomDimension = new RoomDimension(length, width);
removing the unnecessary method calls. Also, it would be better if your RoomDimensions class was not pluralized..
You need to be precise about your errors! Generally questions get general answers. Also, highlight your code with Java tags not HTML.
Recommended Links:
Java Style Guide: Naming Conventions
Java Style Guide: Commenting
Be Precise