Results 1 to 12 of 12
Thread: error code on my program
- 10-11-2011, 10:54 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
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..
- 10-11-2011, 11:04 PM #2
Senior Member
- Join Date
- Oct 2011
- Posts
- 115
- Rep Power
- 0
Re: error code on my program
You need to capitalize A in new area()
- 10-11-2011, 11:08 PM #3
Senior Member
- Join Date
- Oct 2011
- Posts
- 115
- Rep Power
- 0
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; }
Java Code:private float GetCuboidVolume(float width, float height,float length) { return width*height*length; }
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 11:18 PM.
- 10-11-2011, 11:24 PM #4
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
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...
- 10-11-2011, 11:25 PM #5
Senior Member
- Join Date
- Oct 2011
- Posts
- 115
- Rep Power
- 0
Re: error code on my program
You have not addressed the ONLY positive integers part yet.
- 10-11-2011, 11:27 PM #6
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
Re: error code on my program
what does that mean
many thanks Andy..
- 10-11-2011, 11:31 PM #7
Senior Member
- Join Date
- Oct 2011
- Posts
- 115
- Rep Power
- 0
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.
- 10-11-2011, 11:41 PM #8
Member
- Join Date
- Oct 2011
- Posts
- 81
- Rep Power
- 0
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).
- 10-11-2011, 11:45 PM #9
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
Re: error code on my program
could you explain abit more please..
many thanks Andy.
- 10-11-2011, 11:50 PM #10
Member
- Join Date
- Oct 2011
- Posts
- 81
- Rep Power
- 0
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);
Java Code:DisplayVolume(w,h,l); DisplaySurfaceArea(w,h,l);
- 10-12-2011, 12:00 AM #11
Senior Member
- Join Date
- Oct 2011
- Posts
- 115
- Rep Power
- 0
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)); } }
- 10-12-2011, 12:26 AM #12
Member
- Join Date
- Sep 2011
- Posts
- 69
- Rep Power
- 0
Similar Threads
-
How to code a program to send messages to a chat program?
By josh2992 in forum New To JavaReplies: 2Last Post: 04-02-2011, 01:57 PM -
error in code
By Akap in forum New To JavaReplies: 4Last Post: 01-31-2011, 07:37 AM -
What code would you put in a wedding program?
By frenchzebu in forum Forum LobbyReplies: 1Last Post: 07-03-2010, 04:08 PM -
Error Code???
By andmartha in forum New To JavaReplies: 11Last Post: 10-04-2008, 03:16 AM -
Pls help with a code error.
By saytri in forum New To JavaReplies: 8Last Post: 12-24-2007, 09:10 PM
Bookmarks