# Carpet Calculator Program

This program is supposed to have 3 parts to it. 2 classes and 1 test program which I have created. My code for the program runs and works fine. However, I based my program off a different UML diagram. I had to go back and add more modules based on the UML diagram that my professor gave the class. My program still runs fine although I am skeptical about the addition of those methods which I feel I do not need in my program but I have to include them. I want to know if I created them properly. The instructions only covered the basic modules. For the extra modules the professor wanted, there wasn't a clear reason why they were needed and what they were supposed to do which threw me off. Usually, the modules return something or are there to hold something. The ones I am unsure about are the ones marked with a yellow lines next to them.

The Westfield Carpet Company has asked you to write an application that calculates the price of carpeting for rectangular rooms. To calculate the price, you multiply the area of the floor (width times length) by the price per square foot of carpet. For example, the area of the floor that is 12 feet long and 10 feet wide is 120 square foot. To cover that floor with carpet that costs $8 per square foot would cost$960.

First you should create a class named RoomDimension that has two fields: one for the length of the room and one for the width of the room. The RoomDimension class should have a method that returns the area of the room.

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 calculates and returns the cost of the carpet.

Once you have the two classes, write a test program to ask the user to enter the dimensions of a room and the price per square foot of the desired carpeting. Then display the total cost of the carpet.

public class Room_Dimension
{

private double length;
private double width;

public Room_Dimension(double length, double width)
{
this.length = length;
this.width = width;
}

public double getLength()
{
return length;
}

public double getWidth()
{
return width;
}

public Room_Dimension(Room_Dimension roomDimensions)
{

}

public double getArea()
{
return length * width;
}

public String toString()
{
return "Dimensions of room: (length = " + length + ", width = " + width + ")";
}

}

public class Room_Carpet
{
private Room_Dimension roomDimensions;
private double carpetCost;

public Room_Carpet(Room_Dimension roomDimensions, double carpetCost)
{
this.roomDimensions = roomDimensions;
this.carpetCost = carpetCost;
}

public Room_Carpet(Room_Carpet roomCarpet)
{

}

public double getTotalCost()
{
return carpetCost * roomDimensions.getArea();
}

public Room_Dimension getSize()
{
return roomDimensions;
}

public double getCarpetCost()
{
return carpetCost;
}

public String toString()
{
return "" + roomDimensions + ", Carpet cost per square feet = $" + carpetCost + ", " + "Total cost =$" + getTotalCost() + "";
}

}

import java.util.Scanner;
public class Carpet_Calculator
{
public static void main(String[] args)
{
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);

System.out.println("This program will display the carpet cost of a room.");

// Get the length of the room.
System.out.print("Enter the length of room: ");
double length = keyboard.nextDouble();

// Get the width of the room.
System.out.print("Enter the width of room: ");
double width = keyboard.nextDouble();

// Get the cost of price per square feet of the desired carpeting.
System.out.print("Enter the price per square feet for the desired carpeting: ");
double carpetCost = keyboard.nextDouble();

//Close keyboard
keyboard.close();

// Create Room_Dimension and Room_Carpet objects.
Room_Dimension dimensions = new Room_Dimension(length, width);
Room_Carpet roomCarpet = new Room_Carpet(dimensions, carpetCost);

// Print the object calling the toString
System.out.println(roomCarpet);
}
}

Room_Dimension Class

Overall, I think that this class is laid out pretty well. There are a couple small things that I would question though;

public Room_Dimension(Room_Dimension roomDimensions)
{

}

If this method doesn't actually do anything, it shouldn't be in the code. I would say that having copy constructors can be very helpful... If the method is properly implemented.

The point of a copy constructor is that you can pass an object, which is already instantiated, to another object of the same type to instantiate it. The benefit to this is that you don't have to worry about passing a bunch of values to the object manually. You literally create the copy of the object with the values that it contains.

public Room_Dimension(Room_Dimension roomDimensions)
{
this.length = roomDimensions.getLength();
this.width  = roomDimensions.getWidth();
}

public double getLength()
{
return length;
}

public double getWidth()
{
return width;
}

You have accessors (the getLength() and getWidth() methods). These methods allow us to retrieve data from our private members of the class instances safely, without revealing how they are implemented. This is known as encapsulation, a very important topic on programming to understand. This is where having a properly implemented copy constructor can show its' worth, due to the fact that you can now create copies easily without exposing references/implementations of the member variables.

Typically, accessors are paired with mutators, which conversely, allow us to alter the members of a class instance in a predictable and controlled way. As pointed out by cbjobar in the comments, the use of mutators changes a class instance from immutable to mutable; the data values can now be modified after instantiation.

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

public void setWidth(double width)
{
this.width = width;
}

Room_Carpet Class
Once again, the class is designed pretty well, but the copy constructor is not implemented.

public Room_Carpet(Room_Carpet roomCarpet)
{
this.roomDimensions = roomCarpet.roomDimensions;
this.carpetCost     = roomCarpet.getCarpetCost();
}

Another copy constructor that just takes up space without providing any added functionality or benefit to your program. I would remove it if you don't use it (unless it is required)

Finally, the reason that your professor added the extra methods was to force you to protect your data. This is a normal practice in Java, but you really don't see accessors/mutators as much in a language like C++, for instance.

• It is probably worth noting that adding mutators to an object can change it from immutable to mutable. This can have implications ranging from possibly breaking encapsulation to thread safety to memory demands. For example, having mutable RoomDimensions means that a copy should be returned from RoomCarpet#getSize so that the internal object can't be changed from the outside. Having to make so many copies would give a copy constructor a useful purpose, too. – cbojar Apr 11 '15 at 2:08
• Good point! I will update my post with this – Evan Bechtol Apr 11 '15 at 2:12
• @cbojar Just updated my post, thanks for that! – Evan Bechtol Apr 11 '15 at 4:14
• Looks great! :) – cbojar Apr 11 '15 at 4:20
• Thanks Evan! I just didn't want to have empty constructors lying around, but then I was worried if I didn't include them, it would have resulted in point deduction. – user21563966 Apr 12 '15 at 20:51