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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

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

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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. #3
    Junior Member
    Join Date
    Aug 2009
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

Similar Threads

  1. help with simple java program
    By parvez07 in forum Java Theory & Questions
    Replies: 4
    Last Post: August 25th, 2009, 07:19 AM
  2. Java program Square root
    By Hey in forum Java Theory & Questions
    Replies: 5
    Last Post: August 16th, 2009, 01:14 AM
  3. Java Program Help
    By javakid93 in forum Java Theory & Questions
    Replies: 6
    Last Post: July 27th, 2009, 11:03 AM
  4. Someone please help me write a simple program!!!
    By ocean123 in forum Loops & Control Statements
    Replies: 3
    Last Post: June 14th, 2009, 09:46 PM
  5. Java program to write game
    By GrosslyMisinformed in forum Paid Java Projects
    Replies: 3
    Last Post: January 27th, 2009, 03:33 PM

Tags for this Thread