|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

03-13-2008, 04:04 AM
|
|
Member
|
|
Join Date: Feb 2008
Posts: 5
|
|
|
Method in a Switch Statement
So I need to find a way to have a method inside a switch statement. I have no idea how though. Any help would be appreciated. In each of the following cases are the different methods I need to have executed if the case is called.
So, a menu comes up asking what they want to do. They enter 1,2,3,4, or 0 and it goes to the method in the case where it asks them what the radius or length of the side is, and it tells them the area or volume.
import java.util.Scanner;
public class Menu { public static void main( String args[] ) { Scanner input = new Scanner( System.in );
int num; double radius; double volume; double length; double area;
System.out.println("1. Circle Area"); System.out.println("2. Sphere Volume"); System.out.println("3. Square Area"); System.out.println("4. Cube Volume"); System.out.println(); System.out.println("Enter number (or 0 to exit)"); num = input.nextInt();
while (num != 0) { if(num <0 || num >4) { System.out.println("Wrong input"); } if(num >=1 && num <=4) { switch(num) { case 1: { public void determineCircleArea() { Scanner input = new Scanner( System.in );
System.out.print( "Enter radius of circle: " ); double radius = input.nextDouble();
System.out.printf( "Area is %f\n", circleArea( radius ) ); }
public double circleArea( double radius ) { double area = ( 4.0 / 3.0 ) * Math.PI * Math.pow( radius, 2 ); return area; } } break; case 2: {
public void determineSphereVolume() { Scanner input = new Scanner( System.in );
System.out.print( "Enter radius of sphere: " ); double radius = input.nextDouble();
System.out.printf( "Volume is %f\n", sphereVolume( radius ) ); }
public double sphereVolume( double radius ) { double volume = ( 4.0 / 3.0 ) * Math.PI * Math.pow( radius, 3 ); return volume; } }
} break; case 3: {
public void determineSquareArea() { Scanner input = new Scanner( System.in );
System.out.print( "Enter length of square: " ); double length = input.nextDouble();
System.out.printf( "Volume is %f\n", squareArea( length ) ); }
public double squareArea( double length ) { double area = ( 4.0 / 3.0 ) * Math.pow( length, 2 ); return area; } } } break; case 4: {
public void determineCubeVolume() { Scanner input = new Scanner( System.in );
System.out.print( "Enter length of sphere: " ); double length = input.nextDouble();
System.out.printf( "Volume is %f\n", cubeVolume( length ) ); }
public double cubeVolume( double length ) { double volume = ( 4.0 / 3.0 ) * Math.pow( length, 3 ); return volume; } } } break; } } } } }
|
|

03-13-2008, 06:59 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,959
|
|
Yes, you can't do that. In simple word you are try to access/implement a non-static method in a static method(actually in main method.)
Did you use IDE for coding. Lots of additions brackets are found.
Try to write functions separately. Your coding practice is completely wrong in my view. Better to read OOP concepts also pal.
Here is the code I have work out, and I'm not saying that I'm 100% perfect But this will help you. Just compare two code and try to identify where you going wrong.
import java.util.Scanner;
public class Menu
{
// Case 1
public static void determineCircleArea()
{
Scanner input = new Scanner( System.in );
System.out.print( "Enter radius of circle: " );
double radius = input.nextDouble();
System.out.printf( "Area is %f\n", circleArea( radius ) );
}
public static double circleArea( double radius )
{
double area = ( 4.0 / 3.0 ) * Math.PI * Math.pow( radius, 2 );
return area;
}
// Case 2
public static void determineSphereVolume()
{
Scanner input = new Scanner( System.in );
System.out.print( "Enter radius of sphere: " );
double radius = input.nextDouble();
System.out.printf( "Volume is %f\n", sphereVolume( radius ) );
}
public static double sphereVolume( double radius )
{
double volume = ( 4.0 / 3.0 ) * Math.PI * Math.pow( radius, 3 );
return volume;
}
// Case 3
public static void determineSquareArea()
{
Scanner input = new Scanner( System.in );
System.out.print( "Enter length of square: " );
double length = input.nextDouble();
System.out.printf( "Volume is %f\n", squareArea( length ) );
}
public static double squareArea( double length )
{
double area = ( 4.0 / 3.0 ) * Math.pow( length, 2 );
return area;
}
// Case 4
public static void determineCubeVolume()
{
Scanner input = new Scanner( System.in );
System.out.print( "Enter length of sphere: " );
double length = input.nextDouble();
System.out.printf( "Volume is %f\n", cubeVolume( length ) );
}
public static double cubeVolume( double length )
{
double volume = ( 4.0 / 3.0 ) * Math.pow( length, 3 );
return volume;
}
public static void main( String args[] )
{
Scanner input = new Scanner( System.in );
int num;
double radius;
double volume;
double length;
double area;
System.out.println("1. Circle Area");
System.out.println("2. Sphere Volume");
System.out.println("3. Square Area");
System.out.println("4. Cube Volume");
System.out.println();
System.out.println("Enter number (or 0 to exit)");
num = input.nextInt();
while (num != 0)
{
if(num <0 || num >4)
{
System.out.println("Wrong input");
}
if(num >=1 && num <=4)
{
switch(num)
{
case 1:
{
determineCircleArea();
}
break;
case 2:
{
determineSphereVolume();
}
break;
case 3:
{
determineSquareArea();
}
break;
case 4:
{
determineCubeVolume();
}
break;
}
}
}
}
}
Hope it is helpful to you.
Eranga
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

03-13-2008, 07:01 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,959
|
|
|
Much better practice I have believe and I get use is, as much as possible try to include one two lines in main() method. You can use as much I need, large number of methods. Simply call one from the other, just one line of code needed.
What you think of it.
Eranga.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

03-13-2008, 08:25 AM
|
|
Member
|
|
Join Date: Feb 2008
Posts: 5
|
|
|
Thanks for your help.
|
|

03-13-2008, 08:34 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,959
|
|
|
Any time. Is it ok, as you expect?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

03-13-2008, 06:14 PM
|
|
Member
|
|
Join Date: Feb 2008
Posts: 5
|
|
|
Ya. It was perfect. I learned something new. Thanks.
|
|

03-14-2008, 04:48 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,959
|
|
|
That's good. In every post here in our community have to learn something pal.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|