Carthage Carpet Company Lab 04 – Due by Thursday, June 12, 2018 at 5:00 pm The C
ID: 3896413 • Letter: C
Question
Carthage Carpet Company Lab 04 – Due by Thursday, June 12, 2018 at 5:00 pm
The Carthage Carpet Company has asked you to write a program to calculate the total cost of carpet/flooring for rectangular rooms or hallways. You will get user input for the room dimensions and the price per square foot of the carpet/flooring. To calculate total cost, multiply length & width of the room to get the area, and then multiply the area by the price per square foot of carpet/flooring to get total price. For example, a 12’ x 10’ room has an area of 120 square feet. Carpet/flooring with a price of $8 per square foot, would be a total cost of $960 (12 x 10 x 8 = 960). The minimum (default) purchase at the company is for a 10’ x 10’ room, charging $1.50 square foot. One customer, Corbin Corp, has a standing monthly order to install $15.99 carpet/flooring for a 9.75’ x 12.5’ room.
Create the following in your program:
A class named Measurements that has the following data fields with appropriate modifiers & data types:
? length of the room
? width of the room
? price of the carpet/flooring per square foot
The Measurements class should also contain:
? no-arg constructor (set default values: length 10.0, width 10.0, price $1.50 minimum as noted above)
? overloaded constructor that accepts length, width, & price as arguments
? all setters & getters
? value-returning method that calculates the area of the room (length x width)
? value-returning method that calculates the cost of the carpet/flooring (area x price)
? toString method to display results
The driver class containing the main method should:
? get user input for length & width of the room and price per square foot of the carpet/flooring
? create an object using the no-arg constructor & display output as noted for minimum purchase
? create an object using the overloaded constructor & display output of the user input
? create an object using the overloaded constructor & display output as noted for Corbin Corp
Required validation & additional formatting: all user input must be validated (length & width no less than 10, price no less than $1.50). Length, width, and area should be formatted to three decimal places. Price and total cost of the carpet/flooring must be formatted to currency.
Points may be deducted if submitted incorrectly.
Required project name: LastnameFirstname_04
Recommended name for package: objects.classes
Recommended name for class: CarpetFlooringDemo
Required name for class: Measurements
See next page for a sample run of the program.
Sample output. Your program should display something similar. You should create your own input to test for several different cases. User input is green text.
Length: -1
Length must be at least 10.0. Please try again: 0
Length must be at least 10.0. Please try again: 5
Length must be at least 10.0. Please try again: 9.9
Length must be at least 10.0. Please try again: 35.5
Width: -1
Width must be at least 10.0. Please try again: 5
Width must be at least 10.0. Please try again: 9.9
Width must be at least 10.0. Please try again: 125.25
Price per square foot: -1
Price must be at least $1.50. Please try again: 0
Price must be at least $1.50. Please try again: 1.49
Price must be at least $1.50. Please try again: 10.99
Room 1 (default):
Length: 10.000
Width: 10.000
Total Area: 100.000
Price per sq ft: $1.50
Total Cost: $150.00
Room 2 (user input):
Length: 35.500
Width: 125.250
Total Area: 4,446.375
Price per sq ft: $10.99
Total Cost: $48,865.66
Corbin Corp (scheduled purchase):
Length: 9.750
Width: 12.500
Total Area: 121.875
Price per sq ft: $15.99
Total Cost: $1,948.78
Explanation / Answer
Solution:
Measurements.java
package objects.classes;
import java.math.RoundingMode;
import java.text.DecimalFormat;
public class Measurements {
DecimalFormat df = new DecimalFormat("0.000");
//Declaring fields/members of Measurements class
float length;
float width;
float price;
//Default Constructor or No-arguments constructor
public Measurements() {
length = 10.0f;
width = 10.0f;
price = 1.50f;
}
//Overloading constructor with parameters or parameterized constructor
public Measurements(float length, float width, float price) {
this.length = length;
this.width = width;
this.price = price;
}
//Calculating area by multiplying length and width
public float area() {
return length*width;
}
//Calculating cost of carpet/flooring by multipying price with area
public float carpetCost() {
return (area()*price);
}
//Overriding toString method to display customise information
@Override
public String toString() {
df.setRoundingMode(RoundingMode.CEILING);
return "Length: " + df.format(length) + " Width: " + df.format(width) +" Total Area: "+ df.format(area())+ " Price per sq ft: $" + price+" Total Cost: $"+ carpetCost();
}
}
CarpetFlooringDemo.java
package objects.classes;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.concurrent.SynchronousQueue;
//Driver class to test our main class
public class CarpetFlooringDemo {
static float length;
static float width;
static float price;
public static void main(String[] args) {
//Taking inputs from the user for length,width and price
lengthInput();
widthInput();
priceInput();
//Creating default measurements object
Measurements defaultMeasurements = new Measurements();
//Creating User input measurements object
Measurements userInputMeasurements = new Measurements(length,width,price);
//Creating scheduled purchase measurements object
Measurements scheduledPurchaseMeasurements = new Measurements(9.750f,12.500f,15.99f);
//Displaying the Cost information
System.out.println("Room1 (default)");
System.out.println(defaultMeasurements);
System.out.println("Room2 (user input)");
System.out.println(userInputMeasurements);
System.out.println("Room3 (scheduled purchase)");
System.out.println(scheduledPurchaseMeasurements);
}
//Taking valid length from the user and displaying error message for wrong input and asking for the input again
public static void lengthInput() {
while(true) {
Scanner scan = new Scanner(System.in);
System.out.println("Length:");
try {
length = scan.nextFloat();
if(length<10.0) {
System.out.println("Length must be at least 10.0. Please try again: ");
continue;
}else {
break;
}
}catch(InputMismatchException ime) {
System.out.println("Invalid input. It must be of type float.");
}
}
}
//Taking valid width from the user and displaying error message for wrong input and asking for the input again
public static void widthInput() {
while(true) {
Scanner scan = new Scanner(System.in);
System.out.println("Width:");
try {
width = scan.nextFloat();
if(width<10.0) {
System.out.println("Width must be at least 10.0. Please try again: ");
continue;
}else break;
}catch(InputMismatchException ime){
System.out.println("Invalid input. It must be of type float.");
}
}
}
//Taking valid price from the user and displaying error message for wrong input and asking for the input again
public static void priceInput() {
while(true) {
Scanner scan = new Scanner(System.in);
System.out.println("Price per square foot:");
try {
price = scan.nextFloat();
if(price<1.50) {
System.out.println("Price must be at least $1.50. Please try again: ");
continue;
}else break;
}catch(InputMismatchException ime){
System.out.println("Invalid input. It must be of type float.");
}
}
}
}
Sample Run:
Length:
subrat
Invalid input. It must be of type float.
Length:
-1
Length must be at least 10.0. Please try again:
Length:
0
Length must be at least 10.0. Please try again:
Length:
5
Length must be at least 10.0. Please try again:
Length:
9.9
Length must be at least 10.0. Please try again:
Length:
35.5
Width:
-1
Width must be at least 10.0. Please try again:
Width:
5
Width must be at least 10.0. Please try again:
Width:
9.9
Width must be at least 10.0. Please try again:
Width:
125.5
Price per square foot:
-1
Price must be at least $1.50. Please try again:
Price per square foot:
0
Price must be at least $1.50. Please try again:
Price per square foot:
1.49
Price must be at least $1.50. Please try again:
Price per square foot:
10.99
Room1 (default)
Length: 10.000
Width: 10.000
Total Area: 100.000
Price per sq ft: $1.5
Total Cost: $150.0
Room2 (user input)
Length: 35.500
Width: 125.500
Total Area: 4455.250
Price per sq ft: $10.99
Total Cost: $48963.195
Room3 (scheduled purchase)
Length: 9.750
Width: 12.500
Total Area: 121.875
Price per sq ft: $15.99
Total Cost: $1948.7812
Note: Before giving negative feedback please do disscuss what went wrong in comment section.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.