# Thread: Method in a Switch Statement

1. Member Join Date
Feb 2008
Posts
5
Rep Power
0

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

PHP Code:
```import java.util.Scanner;

{
public static void main( String args[] )
{
Scanner input = new Scanner( System.in );

int num;
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: " );

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: " );

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;

}
}
}
}
}```  Reply With Quote

2. ## 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.

Java Code:
```import java.util.Scanner;

{
// Case 1
public static void determineCircleArea()
{
Scanner input = new Scanner( System.in );

System.out.print( "Enter radius of circle: " );

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: " );

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 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  Reply With Quote

3. ## 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.  Reply With Quote

4. Member Join Date
Feb 2008
Posts
5
Rep Power
0

##   Reply With Quote

5. ## Any time. Is it ok, as you expect?  Reply With Quote

6. Member Join Date
Feb 2008
Posts
5
Rep Power
0

## Ya. It was perfect. I learned something new. Thanks.  Reply With Quote

7. ## That's good. In every post here in our community have to learn something pal.  Reply With Quote

#### Posting Permissions

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