Re: error code on my program
You need to capitalize A in new area()
Re: error code on my program
Also don't add semicolons to the end of your class methods:
Code:
private float GetCuboidVolume(float width, float height,float length);
{
return width*height*length;
}
Change to
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
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:
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));
}
}
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...
Re: error code on my program
You have not addressed the ONLY positive integers part yet.
Re: error code on my program
what does that mean
many thanks Andy..
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.
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).
Re: error code on my program
could you explain abit more please..
many thanks Andy.
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:
Code:
new Area().DisplayVolume(w,h,l);
new Area().DisplaySurfaceArea(w,h,l);
remove the two "new Area()." so it's just:
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.
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:
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));
}
}
Re: error code on my program
thankyou all for your help and explaining it all to me
Andy..