Hello, How do you solve programming challenge #3 in chapter 8 in Starting out wi
ID: 642985 • Letter: H
Question
Hello,
How do you solve programming challenge #3 in chapter 8 in Starting out with java: from control structures through objects 5th edition by Tony Gaddis?
This is the program:
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 feel long and 10 feet wide is 120 square feet. To convert that floor with carpet that costs $8 per square foot would cost $960. (12x10x8=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 lenght 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.
Thanks!
Explanation / Answer
carpetcalculator.java
import java.util.Scanner;
/**
This program demonstrates a solution to the
Carpet Calculator programming challenge.
*/
public class CarpetCalculator
{
public static void main(String[] args)
{
final double CARPET_PRICE = 8.0; // Price per square foot
double length; // To input room length
double width; // To input room width
RoomDimension dimensions; // To hold room dimensions
RoomCarpet room; // To determine cost
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Display intro.
System.out.println("This program will display price to " +
"carpet a room. You must input the " +
"room's dimensions in feet.");
// Get the length of the room.
System.out.print("Enter the length first: ");
length = keyboard.nextDouble();
// Get the width of the room.
System.out.print("Now enter the width: ");
width = keyboard.nextDouble();
// Create RoomDimension and RoomCarpet objects.
dimensions = new RoomDimension(length, width);
room = new RoomCarpet(dimensions, CARPET_PRICE);
// Display the dimensions and cost.
System.out.println(room);
}
}
RoomCarpet.java
import java.text.DecimalFormat;
/**
The RoomCarpet class stores data about a room's carpet
for the Carpet Calculator programming challenge.
*/
public class RoomCarpet
{
private RoomDimension size; // The size of the room
private double carpetCost; // Carpet cost per square foot
/**
Constructor
@param dim The room's dimensions.
@param cost The carpet's cost per square foot.
*/
public RoomCarpet(RoomDimension dim, double cost)
{
// Make size reference a copy of the object referenced
// by the dim argument.
size = new RoomDimension(dim.getLength(), dim.getWidth());
// Assign the cost.
carpetCost = cost;
}
/**
getTotalCost method
@return The total cost of the carpet.
*/
public double getTotalCost()
{
// Return the total cost.
return carpetCost * size.getArea();
}
/**
toString method
@return A string reporting the room's dimensions and
the total cost of the carpet.
*/
public String toString()
{
// Create a DecimalFormat object to format output.
DecimalFormat dollar = new DecimalFormat("#,##0.00");
// Create a String with the object state.
String str = "Room dimensions: " +
size + " Carpet cost: $" +
dollar.format(getTotalCost());
// Return the String
return str;
}
}
RoomDimension.java
/**
The RoomDimension class stores data about a room
for the Carpet Calculator programming challenge.
*/
public class RoomDimension
{
private double length; // Room length
private double width; // Room width
/**
Constructor
@param len The room's length.
@param w The room's width.
*/
public RoomDimension(double len, double w)
{
length = len;
width = w;
}
/**
getLength method
@return The room's length.
*/
public double getLength()
{
return length;
}
/**
getWidth method
@return The room's width.
*/
public double getWidth()
{
return width;
}
/**
getArea method
@return The room's area.
*/
public double getArea()
{
return length * width;
}
/**
toString method
@return A string reporting the room's length,
width, and area.
*/
public String toString()
{
String str = "Length: " + length +
" Width: " + width +
" Area: " + getArea();
return str;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.