Back to the Program-A-Day homepage
Problem
It's time to replace the carpet in your living room, but how much will it cost? The carpet you want costs $8 per square foot plus an installation fee of $500. Your roommate has agreed to pay half, and you have a coupon for $50 off the total cost. Write a program that accepts the length and width (as integers) of a room. Calculate the total cost (as a double) of carpeting by multiplying the square footage by the price per square foot, plus installation. Your program will print total square footage, total cost, and your cost.
|
|
SideSectionTitle
Taken from http://www.flickr.com/photos/daniello/565304023/
An image or By Students section
|
Solution
Calculate Area and Cost
|
Code
Solution Code
import javax.swing.JOptionPane;
public class CalculateArea
{
public static void main(String[] args)
{
String input;
int costPSF = 8; // Cost of $8 per square foot
int installFee = 500; // $500 installation fee
int coupon = 50;
int length;
int width;
int area;
double totalCost;
input = JOptionPane.showInputDialog("Input length:");
length = Integer.parseInt(input);
input = JOptionPane.showInputDialog("Input width:");
width = Integer.parseInt(input);
area = length * width;
totalCost = (area * costPSF) + installFee;
System.out.println("Total area: " + area + " square feet");
System.out.println("Total cost: $" + totalCost);
System.out.println("Your cost: $" + ((totalCost / 2) - coupon));
}
}
|
Back to the Program-A-Day homepage