beginner java program help
hi i recently started a java class and received my 1st assignment. Its alot harder than i woulda thought and im kinda stuck and need abit of help.
if im doing this right ive declared my variable and made them all user imputs but im confused on how to set up the calculations.help would be greatly appreciated, heres the criteria:
also anything relating to area was me trying random things that didnt work.
Write a program that computes the cost of painting and installing carpet in a room. Assume that the room has one door, two windows, and one book-shelf. Your program must do the following:
a. Prompts the user to enter, in feet, the length, width, and height of a room. Read the dimensions of the room.
b. Prompts the user to enter the widths and heights, in feet, of the door, each window, and the bookshelf. Read these quantities.
c. Prompts the user to enter the cost, per square foot, of painting the walls. Read these quantities.
d. Prompts the user to enter the cost, per square foot, of installing carpet. Read these quantities.
e. Outputs the cost of painting the walls and installing the carpet.
Code :
:import java.util.Scanner;
public class Assignment1 {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
int Height;
int Width;
int Length;
int DLength;
int DWidth;
int W1Length;
int W1Width;
int W2Length;
int W2Width;
int BLength;
int BWidth;
int FloorCost;
int WallCost;
System.out.println("ROOM Height");
Height = in.nextInt();
System.out.println("ROOM Width");
Width = in.nextInt();
System.out.println("ROOM Length");
Length = in.nextInt();
int Area = Length * Width; //area of the floor
int Tarea = Length * Width * Height * 4; //area of the 4 walls
System.out.println("DOOR Height");
DLength = in.nextInt();
System.out.println("DOOR Width");
DWidth = in.nextInt();
int Area1 = DLength * DWidth; //area of the door
System.out.println("WINDOW1 Height");
W1Length = in.nextInt();
System.out.println("WINDOW1 Width");
W1Width = in.nextInt();
int Area2 = W1Length * W1Width; //area of the 1st window
System.out.println("WINDOW2 Height");
W2Length = in.nextInt();
System.out.println("WINDOW2 Width");
W2Width = in.nextInt();
int Area3 = W2Length * W2Width; //area of the 2nd window
System.out.println("BOOKSHELF Height");
BLength = in.nextInt();
System.out.println("BOOKSHELF Width");
BWidth = in.nextInt();
int Area4 = BLength * BWidth; //area of the bookshelf
System.out.println("FloorCost per sqaurefeet");
FloorCost = in.nextInt();
System.out.println("WallCost per sqaurefeet");
WallCost = in.nextInt();
int TotalAREA = Area1 + Area2 + Area3 + Area4; //total area of everything minus the floor and walls
System.out.println("The Cost of Painting the walls" + (Tarea - TotalAREA) * WallCost);
System.out.println("The Cost of install carpet is " + Area * FloorCost);
}
}
Re: beginner java program help
Quote:
Originally Posted by
Peeboelmeebo
... how to set up the calculations....
The flow for this program is strictly top-to-bottom. It cannot calculate area until it has the dimensions. It can't go back and plug in dimensional data into previous arithmetic expressions.
Similarly, it can't calculate cost for floor covering until it knows the area of the floor and the price per square foot of carpet. It can't calculate the cost for wall covering until it knows the area that has to be covered and the price per square foot of wall covering.
So, here's the deal:
- Prompt user for room dimensions and get length, width, height
- Calculate area of floor. (area = length*width)
- Calculate gross area of the four walls. I'll leave that up to you.
- Prompt user for door dimensions: dLength, dWidth.
- Calculate area of door.
- Prompt user for Window number 1 dimensions: w1Length, w1Height.
- Calculate area of Window number 1.
- Etc.
See how it goes? First get dimensions from user. Then use them to calculate whatever...
Anyhow...
After all of the above calculations, you know the floor area. The wall area that has to be covered is equal to the gross area minus the area of the door minus the area of Window number 1, etc...
Once you know the floor area and the wall area that has to be covered, use price information that you get from the user to calculate the cost of each.
Cheers!
Z
Re: beginner java program help
Re: beginner java program help
thank you both for the advice, i was able to successfully run the program with everything in the directions . Also can the if/then statement can be used on multiple things without having to retype it?
Re: beginner java program help
Quote:
Originally Posted by
Peeboelmeebo
...can the if/then statement can be used on multiple things without having to retype it?
Can you explain what you mean? Maybe give an example in pseudo-code or human language?
Cheers!
Z
Re: beginner java program help
Quote:
Originally Posted by
Zaphod_b
Can you explain what you mean? Maybe give an example in pseudo-code or human language?
Cheers!
Z
The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
that explains it, and i suppose also answers my question.