# Carpet Calculator Program Final Edit

I asked a previous question of this same program. I would like more opinions before I submit this assignment since I edited the program after receiving feedback last time.

public class Room_Dimension
{
//Data fields
private double length;
private double width;

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

//Copy constructor
public Room_Dimension(Room_Dimension rd)
{
this.length = rd.getLength();
this.width  = rd.getWidth();
}

//Accessors
public double getLength()
{
return length;
}

public double getWidth()
{
return width;
}

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

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

//Function to calculate the area
public double getArea()
{
return length * width;
}

//Function to display information
public String toString()
{
return "Dimensions of room: (length = " + length + ", width = " + width + ")";
}

}

public class Room_Carpet
{
//Data fields
private Room_Dimension rd;
private double carpetCost;

//Constructor
public Room_Carpet(Room_Dimension rd, double carpetCost)
{
super();
this.rd = rd;
this.carpetCost = carpetCost;
}

//Copy constructor
public Room_Carpet(Room_Carpet rc)
{
this.rd             = rc.rd;
this.carpetCost     = rc.getCarpetCost();
}

//Function to determine total cost of carpet that will fill area
public double getTotalCost()
{
return carpetCost * rd.getArea();
}

//Function to get size
public Room_Dimension getSize()
{
return rd;
}

//Function to get carpet cost
public double getCarpetCost()
{
return carpetCost;
}

//Function to display information
public String toString()
{
return "" + rd + ", Carpet cost per square feet = $" + carpetCost + ", " + "Total cost =$" + getTotalCost() + "";
}

}

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 dimension = new Room_Dimension(length, width);
Room_Carpet roomCarpet = new Room_Carpet(dimension, carpetCost);

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


Your code is near perfection, if you want to "get OCD" about it you can skip all the empty lines, comments on self-explanatory methods and fields and open code blocks on the same line, for example:

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


and all the rest, could be written as:

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


to save space, so that the class is visible within the screen with no need of scrolling. Additionally, search for program layout and class interface best practices, to get insight on the subject.

• Thank you simplicis veritatis. The comments are so obvious but I leave them in there since my professor is very nitpicky about these things. – Duck Apr 14 '15 at 20:07
• Its really easy to read and it looks great with or without the comments :) – Ziezi Apr 14 '15 at 20:12

I would mostly agree with simplicis veritatis but would take it a fair amount further.

Yes, your code is neat, but, you are not following many of the standard Java common code conventions. I know that 'local rules' override common guidelines, but I feel I should point them all out anyway, in case you don't have an excuse.... ;-)

1. Java class names do not have an underscore in them. They use CamelCase. Your class Room_Dimension should be RoomDimension.
2. Java convention puts the open-brace at the end-of-line, not the start-of-next-line. Start-of-next-line is a C#, C, and other convention. So, code like:

//Copy constructor
public Room_Carpet(Room_Carpet rc)
{
this.rd             = rc.rd;
this.carpetCost     = rc.getCarpetCost();
}


//Copy constructor
public Room_Carpet(Room_Carpet rc) {
this.rd             = rc.rd;
this.carpetCost     = rc.getCarpetCost();
}

3. Your Room_Dimension should be an immutable class (you should not be able to change it). As a result, the width and length should be final, and there should be no 'setters' on it. How often do the dimensions of a room change? Your current class has an interesting spin-off: your Room_Carpet copy-constructor takes a copy of the reference to the Room_Dimension class... now, because it is a shallow copy, if someone changes the room dimension they change the details on both copies.

4. You should use printf when you can. Your lines of code like:

return "Dimensions of room: (length = " + length + ", width = " + width + ")";


return String.format("Dimensions of room: (length = %f, width = %f)", length, width);