Results 1 to 7 of 7
  1. #1
    cart1443 is offline Member
    Join Date
    Feb 2008
    Posts
    5
    Rep Power
    0

    Default 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 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;
       
                   } 
              } 
           }
    }
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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;
    
    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

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  4. #4
    cart1443 is offline Member
    Join Date
    Feb 2008
    Posts
    5
    Rep Power
    0

    Default

    Thanks for your help.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  6. #6
    cart1443 is offline Member
    Join Date
    Feb 2008
    Posts
    5
    Rep Power
    0

    Default

    Ya. It was perfect. I learned something new. Thanks.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Switch Statement Help
    By bluegreen7hi in forum New To Java
    Replies: 6
    Last Post: 02-06-2008, 05:16 AM
  2. Replies: 0
    Last Post: 01-04-2008, 09:32 AM
  3. Help with gigantamous switch statement
    By trill in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 08:11 AM
  4. Problem with a switch statement in Java
    By baltimore in forum New To Java
    Replies: 2
    Last Post: 08-02-2007, 04:43 AM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 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
  •