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

Code :

// 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
{
}
}
}

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

You misspelled length :D 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

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:

Quote:

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.

Also, I'm not sure why you have the check for input... if you're trying to loop, you need a while loop:

Code :

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.

Code :

// 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 :)

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

{

}

}

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

Quote:

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:

Code :

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.

Code :

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);
}

Quote:

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.