Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Simple java program to calculate wall covering of a room

1. ## Simple java program to calculate wall covering of a room

hi there,

i have been doin java for a few months now. and i have been trying to solve small and simple programs first, so that i understand java right from the beggining.
im currently solving a very simple program which is calculating the wall cover estimate of a room. i would appretiate it alot if some could help me with this simple program as i have been trying to solve the errors with the calculations and the whole structure of the program.

this is the work which i have done so far.
thanks alot

```// calculating area of wall covering of a room

import javax.swing.JOptionPane;

public class Room
{
// First Method, to find Area of room to be covered
public static void areaOfRoom(double lenght, double height, double width)
{
double area = 2 * (lenght * height) + 2 * (width * height);

if (lenght<0)
JOptionPane.showMessageDialog(null, "Lenght Can not be negative");
if (height<0)
JOptionPane.showMessageDialog(null, "Height Can not be negative");
if (width<0)
JOptionPane.showMessageDialog(null, "Width Can not be negative");
if (lenght>0)
if (height>0)
if (Width>0)
JOptionPane.showMessageDialog(null, "The Area of Rectangle is " + area);
}

// Main Method to input numbers from the user
public static void main(String[]args)
{
String input = JOptionPane.showInputDialog("Please Type in 1 to find the Area of Room");
int number = Integer.parseInt(input);
int count=0;
double l,h + w,h;

// This Section runs the areaOfRectangle Method
if (number==1)
{
input = JOptionPane.showInputDialog("Please Type in the Lenght of the Room");
l = Integer.parseInt(input);
input = JOptionPane.showInputDialog("Please Type in the Height of the Room");
h = Integer.parseInt(input);
input = JOptionPane.showInputDialog("Please Type in the Width of the Room");
W = Integer.parseInt(input);
areaOfRectangle 2 * (l, h) + 2 * (W, h);

else if (number<0) // Terminates the program
{
}
}
}```

2. ## Re: Simple java program to calculate wall covering of a room

You misspelled length however, you were consistent, so that doesn't matter. You also have the wrong surface are formula:

Area = 2*height*length+2*width*length+2*height*width

double l,h + w,h;
Remember, using the keyword double doesn't allow you to do any operations, it just says you want to use a variable with that name as a double. It should be:
`double l, w, h;`

areaOfRectangle 2 * (l, h) + 2 * (W, h);
You're passing arguments incorrectly to the function areaOfRectangle. All your arguments should be inside the parenthesis following areaOfRectangle. You're allowed to perform any operations you want inside provided that it returns the type the function signiture demands, however in this case you just need to pass the height, width, and length unmodified.
`areaOfRectangle(l,h,w);`

Also, I'm not sure why you have the check for input... if you're trying to loop, you need a while loop:
```int number = 0;
while (number == 1)
{
input = JOptionPane.showInputDialog("Please Type in the Lenght of the Room");
l = Integer.parseInt(input);
input = JOptionPane.showInputDialog("Please Type in the Height of the Room");
h = Integer.parseInt(input);
input = JOptionPane.showInputDialog("Please Type in the Width of the Room");
w = Integer.parseInt(input);
String input = JOptionPane.showInputDialog("Please Type in 1 to find the area of another room");
number = Integer.parseInt(input);
}```

It looks like the count variable is unused. You can just get rid of it.

You're also testing for negative dimensions incorrectly... use if else statements.

```// First Method, to find Area of room to be covered
public static void areaOfRoom(double length, double height, double width)
{
double area = 2*height*length+2*width*length+2*height*width;

if (lenght<0)
JOptionPane.showMessageDialog(null, "Lenght Can not be negative");
else if (height<0)
JOptionPane.showMessageDialog(null, "Height Can not be negative");
else if (width<0)
JOptionPane.showMessageDialog(null, "Width Can not be negative");
else
JOptionPane.showMessageDialog(null, "The Area of Rectangle is " + area);
}```

hope that helps

3. ## The Following User Says Thank You to helloworld922 For This Useful Post:

parvez07 (August 22nd, 2009)

4. ## Re: Simple java program to calculate wall covering of a room

thanks alot for helping me with my problems with the program.
when compiling the program i come up with errors and i have been trying figure out what the problem. im using textpad to write the code in.
i have tried the way suggested but am finding it hard to sort it out.
thanks alot again.

// calculating area of wall covering of a room

import javax.swing.JOptionPane;

// First Method, to find Area of room to be covered
public static void areaOfRoom(double length, double height, double width)
{
double area = 2*height*length+2*width*length+2*height*width;
{
if (lenght<0)
JOptionPane.showMessageDialog(null, "Lenght Can not be negative");
else if (height<0)
JOptionPane.showMessageDialog(null, "Height Can not be negative");
else if (width<0)
JOptionPane.showMessageDialog(null, "Width Can not be negative");
else
JOptionPane.showMessageDialog(null, "The Area of Rectangle is " + area);
}
}

public static void main(String[]args)
{
String input = JOptionPane.showInputDialog("Please Type in 1 to find the Area of Room");
int Number = Integer.parseInt(input);
int count=0;
double l, w, h;

// This Section runs the areaOfRectangle Method
int number = 0;
while (number == 1)
{
input = JOptionPane.showInputDialog("Please Type in the Lenght of the Room");
l = Integer.parseInt(input);
input = JOptionPane.showInputDialog("Please Type in the Height of the Room");
h = Integer.parseInt(input);
input = JOptionPane.showInputDialog("Please Type in the Width of the Room");
w = Integer.parseInt(input);
String input = JOptionPane.showInputDialog("Please Type in 1 to find the area of another room");
number = Integer.parseInt(input);
}

else if (number<0) // Terminates the program
{
}

}

5. ## Re: Simple java program to calculate wall covering of a room

String input = JOptionPane.showInputDialog("Please Type in 1 to find the area of another room");
oops, that may have been my bad. don't re-declare input. It should be:
`input = JOptionPane.showInputDialog("Please Type in 1 to find the area of another room");`

and you've got an extra set of brackets in the areaOfRoom method. I don't know if it'll give you a compiler error, but it's not necessary.

```public static void areaOfRoom(double length, double height, double width)
{
double area = 2*height*length+2*width*length+2*height*width;

if (lenght<0)
JOptionPane.showMessageDialog(null, "Lenght Can not be negative");
else if (height<0)
JOptionPane.showMessageDialog(null, "Height Can not be negative");
else if (width<0)
JOptionPane.showMessageDialog(null, "Width Can not be negative");
else
JOptionPane.showMessageDialog(null, "The Area of Rectangle is " + area);
}```

else if (number<0) // Terminates the program
{
}
This last bit of code can be removed. There's no if statement now, it's a while loop.