Results 1 to 12 of 12
  1. #1
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default error code on my program

    hi i am trying to Write a program to calculate and output the surface area and volume of a cube.

    this is my code

    import java.util.*;

    class Area {

    public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Please Enter the Height of the Cuboid:");
    float h = sc.nextInt();
    System.out.println("Please Enter the Width of the Cuboid:");
    float w = sc.nextInt();
    System.out.println("Please Enter the Length of the Cuboid:");
    float l = sc.nextInt();


    new area().DisplayVolume(w,h,l);
    new area().DisplaySurfaceArea(w,h,l);

    }

    private float GetCuboidVolume(float width, float height,float length);
    {
    return width*height*length;
    }

    private float GetCuboidSurfaceArea(float width, float height,float length);
    {
    return (2*width*length)+(2*length*height)+(2*he);
    }

    private void DisplayVolume(float width, float height,float length);
    {
    System.out.println("The Volume is: "+GetCuboidVolume(width,height,length));
    }
    private void DisplaySurfaceArea(float width, float height,float length);
    {
    System.out.println("The Surface Area is: "+GetCuboidSurfaceArea(width,height,length);
    }
    }

    the error code i am getting is

    H:\FPT>javac Area.java
    Area.java:36: error: ')' expected
    System.out.println("The Surface Area is: "+GetCuboidSurfaceArea(width,height,len
    gth);

    ^
    1 error

    many thanks Andy..

  2. #2
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: error code on my program

    You need to capitalize A in new area()

  3. #3
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: error code on my program

    Also don't add semicolons to the end of your class methods:

    Java Code:
    private float GetCuboidVolume(float width, float height,float length);
    {
    return width*height*length;
    }
    Change to

    Java Code:
    private float GetCuboidVolume(float width, float height,float length)
    {
    return width*height*length;
    }
    I also noticed that your equation is wrong for Surface Area: Replace with this

    Java Code:
    private float GetCuboidSurfaceArea(float width, float height,float length)
    {
    return (2*width*length)+(2*length*height)+(2*height*width);
    }


    Here is the working code:

    Java Code:
    import java.util.*;
    
    class Area {
    
    public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Please Enter the Height of the Cuboid:");
    float h = sc.nextInt();
    System.out.println("Please Enter the Width of the Cuboid:");
    float w = sc.nextInt();
    System.out.println("Please Enter the Length of the Cuboid:");
    float l = sc.nextInt();
    
    
    new Area().DisplayVolume(w,h,l);
    new Area().DisplaySurfaceArea(w,h,l);
    
    }
    
    private float GetCuboidVolume(float width, float height,float length)
    {
    return width*height*length;
    }
    
    private float GetCuboidSurfaceArea(float width, float height,float length)
    {
    return (2*width*length)+(2*length*height)+(2*height*width);
    }
    
    private void DisplayVolume(float width, float height,float length)
    {
    System.out.println("The Volume is: "+GetCuboidVolume(width,height,length));
    }
    private void DisplaySurfaceArea(float width, float height,float length)
    {
    System.out.println("The Surface Area is: "+GetCuboidSurfaceArea(width,height,length));
    }
    }
    Last edited by kraigballa; 10-11-2011 at 10:18 PM.

  4. #4
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default Re: error code on my program

    hi thankyou very much for your help

    this was what i had to do

    Write a program to calculate and output the surface area and volume of a cube. The program should prompt the user for positive integers representing the width, length and height of the cuboid.

    have i done this right because it is asking me to enter details have i done this right sorry for being a pain..

    many thanks...

  5. #5
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: error code on my program

    You have not addressed the ONLY positive integers part yet.

  6. #6
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default Re: error code on my program

    what does that mean

    many thanks Andy..

  7. #7
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: error code on my program

    Well as of right now the user can input negative values into your program. As the instructions you posted stated the user should only be able to use positive integers, you have yet to implement that.

  8. #8
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: error code on my program

    Why are you creating new objects of type Area? Just make the four non-main methods static. That way they aren't associated with any particular instance of the class Area, but just the class as a whole. Then you don't need the two "new Area()."s - you can just say DisplayVolume(w,h,l); and DisplaySurfaceArea(w,h,l);. Furthermore, the way you are doing it now, it won't work, because the methods DisplayVolume and DisplaySurfaceArea are private, so you can't call them from outside the particular object you are calling them on (even if you are in the same class as said object, as you are in this case).

  9. #9
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default Re: error code on my program

    could you explain abit more please..

    many thanks Andy.

  10. #10
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: error code on my program

    The short answer is that you shouldn't create new Area objects - it's inefficient, and the way you have it set up now doesn't even work. So right now you have:
    Java Code:
    new Area().DisplayVolume(w,h,l);
    new Area().DisplaySurfaceArea(w,h,l);
    remove the two "new Area()." so it's just:
    Java Code:
    DisplayVolume(w,h,l);
    DisplaySurfaceArea(w,h,l);
    In order for that to work, you need to make the four non-main methods (GetCuboidVolume, GetCuboidSurfaceArea, DisplayVolume, DisplaySurfaceArea) static. So like, instead of "private void DisplayVolume", say "private static void DisplayVolume" and the same with the other three.

  11. #11
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: error code on my program

    It would work either way in Java but some other languages are more picky, or at least it let me do it that way. For better practice use static though instead of trying to create an object inside of it's own class.

    Here is what I mean by ONLY positive numbers:
    Java Code:
    import java.util.*;
    
    class Area {
    
    	public static void main(String args[]) {
    	Scanner sc = new Scanner(System.in);
    	float h, w, l;
    	
    		System.out.println("Please Enter the Height of the Cuboid:");
    		h = sc.nextInt();
    		while (h<=0){
    			System.out.println("Please Enter a Positive Number!:");
    			h = sc.nextInt();
    		}
    	
    		System.out.println("Please Enter the Width of the Cuboid:");
    		w = sc.nextInt();
    		while (w<=0){
    			System.out.println("Please Enter a Positive Number!:");
    			w = sc.nextInt();
    		}
    	
    		System.out.println("Please Enter the Length of the Cuboid:");
    		l = sc.nextInt();
    		 while (l<=0){
    			 System.out.println("Please Enter a Positive Number!:");
    				l = sc.nextInt();
    		 }
    	
    	
    	DisplayVolume(w,h,l);
    	DisplaySurfaceArea(w,h,l);
    	
    	}
    
    	private static float GetCuboidVolume(float width, float height,float length)
    	{
    	return width*height*length;
    	}
    	
    	private static float GetCuboidSurfaceArea(float width, float height,float length)
    	{
    	return (2*width*length)+(2*length*height)+(2*height*width);
    	}
    	
    	private static void DisplayVolume(float width, float height,float length)
    	{
    	System.out.println("The Volume is: "+GetCuboidVolume(width,height,length));
    	}
    	private static void DisplaySurfaceArea(float width, float height,float length)
    	{
    	System.out.println("The Surface Area is: "+GetCuboidSurfaceArea(width,height,length));
    	}
    
    }

  12. #12
    andnlou2678 is offline Member
    Join Date
    Sep 2011
    Posts
    69
    Rep Power
    0

    Default Re: error code on my program

    thankyou all for your help and explaining it all to me

    Andy..

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2011, 12:57 PM
  2. error in code
    By Akap in forum New To Java
    Replies: 4
    Last Post: 01-31-2011, 06:37 AM
  3. What code would you put in a wedding program?
    By frenchzebu in forum Forum Lobby
    Replies: 1
    Last Post: 07-03-2010, 03:08 PM
  4. Error Code???
    By andmartha in forum New To Java
    Replies: 11
    Last Post: 10-04-2008, 02:16 AM
  5. Pls help with a code error.
    By saytri in forum New To Java
    Replies: 8
    Last Post: 12-24-2007, 08:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •