HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Carpet Calculator Program Final Edit

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
editprogramcarpetfinalcalculator

Problem

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()
{

Solution

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.

Code Snippets

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

Context

StackExchange Code Review Q#86893, answer score: 4

Revisions (0)

No revisions yet.