*** This question is not as long as it looks! I just pasted in the question that
ID: 3662949 • Letter: #
Question
*** This question is not as long as it looks! I just pasted in the question that the code is for! It's only a short question, really! :) ***
First - I am very new with java.
I am looking to see what could be changed in the code below where I have it marked to change the ouput.
When the program is ran, it shows the enter length and width, but when it comes to the total, it shows the code instead of showing the calculated dimensions and total cost.
Basically, what I want it to say instead on that line is:
"The dimensions of the room is +the calculated dimensions here+ , which will cost +total cost here+."
Also, does this need to be cleaned up? I am so lost in the class, it's 1 day a week from 9:30am-10:45am. The class is incredibly fast paced and I am just having a terrible time keeping up. (This isn't a mini-semester class, this is just a regular class.)
Thank you to anyone who answers this! I greatly appreciate it!
Here is the question for the code below:
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
floor that is 12 feet long and 10 feet wide is 120 square feet. To cover that floor with carpet
that costs $8 per square foot would cost $960. (12 3 10 3 8 5 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. The RoomDimension class should have a method that
returns the area of the room. (The area of the room is the room’s length multiplied by the
room’s width.)
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.
Figure 8-21 is a UML diagram that shows possible class designs and the relationships
among the classes. Once you have written these classes, use them in an application that
asks the user to enter the dimensions of a room and the price per square foot of the desired
carpeting. The application should display the total cost of the carpet.
import java.util.Scanner;
public class CarpetCalc
{
public class RoomDimens
{
private double length;
private double width;
public RoomDimens(double length, double width) {
super();
this.length = length;
this.width = width;
}
// end private double
public double getLength()
{
return length;
}
public double getWidth()
{
return width;
}
public double getArea()
{
return length * width;
}
@Override
public String toString() {
return "RoomDimension [length=" + length + ", width=" + width + "]";
}
}
public class RoomCarpet
{
private RoomDimens roomDimensions;
private double costOfCarpet;
public RoomCarpet(RoomDimens roomDimensions, double costOfCarpet)
{
super();
this.roomDimensions = roomDimensions;
this.costOfCarpet = costOfCarpet;
}
public double getTotalCost()
{
return costOfCarpet * roomDimensions.getArea();
}
@Override
public String toString()
{
// ********* I THINK THIS IS WHAT NEEDS TO BE CHANGED *********
return "RoomCarpet [roomDimensions=" + roomDimensions
+ ", costOfCarpet=" + costOfCarpet + ", " + "total cost="
+ getTotalCost() + "]";
// ********* I THINK THIS IS WHAT NEEDS TO BE CHANGED *********
}
}
public static void main(String[] args) {
final double CARPET_PRICE_PER_SQFT = 8.0;
Scanner keyboard = new Scanner(System.in);
System.out.println("This program will display the carpet cost of a room. *This program calculates in feet.* ");
System.out.print("Please enter the length of room: ");
double length = keyboard.nextDouble();
System.out.print("Please enter the width of room: ");
double width = keyboard.nextDouble();
keyboard.close();
CarpetCalc calculatorProgram = new CarpetCalc();
RoomDimens dimensions = calculatorProgram.new RoomDimens(length,
width);
RoomCarpet roomCarpet = calculatorProgram.new RoomCarpet(dimensions,
CARPET_PRICE_PER_SQFT);
System.out.println(roomCarpet);
}
}
Explanation / Answer
The format of toString to be changed according to your needs. Its not displaying code, its just displaying what you returning in toString methods. I changed them in both classes see them.
In java the practice will be defining classes in its own file if you want them publicly avialable. Even if they are private no need to define one class in another. You can do like below.
import java.util.Scanner;
public class CarpetCalc {
public static void main(String[] args) {
final double CARPET_PRICE_PER_SQFT = 8.0;
Scanner keyboard = new Scanner(System.in);
System.out.println("This program will display the carpet cost of a room. *This program calculates in feet.* ");
System.out.print("Please enter the length of room: ");
double length = keyboard.nextDouble();
System.out.print("Please enter the width of room: ");
double width = keyboard.nextDouble();
keyboard.close();
RoomDimens dimensions = new RoomDimens(length,
width);
RoomCarpet roomCarpet = new RoomCarpet(dimensions,
CARPET_PRICE_PER_SQFT);
System.out.println(roomCarpet);
}
}
class RoomDimens
{
private double length;
private double width;
public RoomDimens(double length, double width) {
super();
this.length = length;
this.width = width;
}
// end private double
public double getLength()
{
return length;
}
public double getWidth()
{
return width;
}
public double getArea()
{
return length * width;
}
@Override
public String toString() {
// return "RoomDimension [length=" + length + ", width=" + width + "]";
//changing output here
return " length=" + length + ", width=" + width+ ", Area = "+getArea();
}
}
class RoomCarpet
{
private RoomDimens roomDimensions;
private double costOfCarpet;
public RoomCarpet(RoomDimens roomDimensions, double costOfCarpet)
{
super();
this.roomDimensions = roomDimensions;
this.costOfCarpet = costOfCarpet;
}
public double getTotalCost()
{
return costOfCarpet * roomDimensions.getArea();
}
@Override
public String toString()
{
// ********* I THINK THIS IS WHAT NEEDS TO BE CHANGED *********
/* return "RoomCarpet [roomDimensions=" + roomDimensions
+ ", costOfCarpet=" + costOfCarpet + ", " + "total cost="
+ getTotalCost() + "]";
*/
// ********* I THINK THIS IS WHAT NEEDS TO BE CHANGED *********
return "The dimensions of the room is "+roomDimensions+", which will cost " +getTotalCost();
}
}
----output---
This program will display the carpet cost of a room.
*This program calculates in feet.*
Please enter the length of room: 10
Please enter the width of room: 20
The dimensions of the room is length=10.0, width=20.0, Area = 200.0, which will cost 1600.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.