Difference between revisions of "Calculating Area"
From CompSciWiki
Line 1: | Line 1: | ||
{{1010PrAD|ProblemName=Calculate Area | {{1010PrAD|ProblemName=Calculate Area | ||
− | |Problem= It's time to replace the carpet in your living room, but how much will it cost? The carpet you want costs $ | + | |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. Write a program that accepts the length and width (as integers) of a room. Calculate the total cost of carpeting by multiplying the square footage by the price per square foot, plus installation. Your program will print the total square footage and the total cost. |
|SideSection= | |SideSection= | ||
Line 12: | Line 12: | ||
|Solution=Calculate Area and Cost | |Solution=Calculate Area and Cost | ||
|SolutionCode=<pre> | |SolutionCode=<pre> | ||
− | + | import javax.swing.JOptionPane; | |
− | import javax.swing. | + | |
public class CalculateArea | public class CalculateArea | ||
{ | { | ||
− | + | public static void main(String[] args) | |
− | + | { | |
− | + | final int COST_PSF = 8; // Cost of $5 per square foot | |
+ | final int INSTALL_FEE = 500; // $500 installation fee | ||
− | + | String input; | |
− | + | int length; | |
− | + | int width; | |
− | + | int area; | |
− | + | int totalCost; | |
− | + | input = JOptionPane.showInputDialog("Input length:"); | |
− | + | length = Integer.parseInt(input); | |
− | + | input = JOptionPane.showInputDialog("Input width:"); | |
− | + | width = Integer.parseInt(input); | |
− | + | area = length * width; | |
− | + | totalCost = (area * COST_PSF) + INSTALL_FEE; | |
− | + | System.out.println("Total area: " + area + " square feet"); | |
− | + | System.out.println("Total cost: $" + totalCost); | |
− | + | } | |
} | } | ||
</pre> | </pre> | ||
}} | }} |
Revision as of 19:30, 1 April 2010
Back to the Program-A-Day homepage
ProblemIt'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. Write a program that accepts the length and width (as integers) of a room. Calculate the total cost of carpeting by multiplying the square footage by the price per square foot, plus installation. Your program will print the total square footage and the total cost. |
SideSectionTitle
An image or By Students section | |
---|---|---|
SolutionCalculate Area and Cost | ||
Code |