In: Computer Science
Starting Out with Java (6th Edition)
chapter 8, 3PC
Requirement: Each of the RoomCarpet and RoomDimension classes should have a toString method, an equals method, a copy method, and a copy constructor. The RoomCarpet class should have mutators and accessors for both of its fields.
The Code is:
import java.text.DecimalFormat;
public class Carpet {
private Dimension rmSize;
private pricesqft;
//constructor
public Carpet()
{
this.rmSize = new Dimension();
this.pricePerSqFt = 0.00;
}
public Carpet(Dimension rmSize, pricesqft)
{
this.rmSize = new
Dimension(rmSize.getRmLength(),rmSize.getRmWidth());
this.pricePerSqFt = pricePerSqFt;
}
public Dimension getRmSize()
{
return new
Dimension(rmSize.getRmLength(),rmSize.getRmWidth());
}
public double getPricePerSqFt()
{
return this.pricePerSqFt;
}
public void setRmSize(Dimension rmSize)
{
this.rmSize = new Dimension(rmSize.getRmLength(),
rmSize.getRmWidth());
}
public void setPricePerSqFt(pricesqft)
{
this.pricePerSqFt = pricePerSqFt;
}
public double rmTotalCost()
{
return rmSize.getAreaRoom() * pricePerSqFt;
}
public String toString()
{
DecimalFormat dollar = new DecimalFormat("$#,##0.00");
String str = this.rmSize.toString() + " Price per sq. ft : "
+dollar.format(pricePerSqFt) + " Price to carpet Room: " +
dollar.format(rmTotalCost()) + '\n';
return str;
}
public boolean equals(Carpet object2)
{
boolean status;
if
((this.equals(object2)==true)&&(this.pricePerSqFt==object2.pricePerSqFt))
status = true;
else
status = false;
return status;
}
}