Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-13-2008, 04:04 AM
Member
 
Join Date: Feb 2008
Posts: 5
cart1443 is on a distinguished road
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 mainString args[] )
{
Scanner input = new ScannerSystem.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 <|| num >4)
          {
              
System.out.println("Wrong input");
            }
            
          if(
num >=&& num <=4)
          {
              switch(
num)
              {
    
   case 
1:
   {
public 
void determineCircleArea()
   {
      
Scanner input = new ScannerSystem.in );

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

      
System.out.printf"Area is %f\n"circleArearadius ) );
   }

   public 
double circleAreadouble radius )
   {
      
double area = ( 4.0 3.0 ) * Math.PI Math.powradius);
      return 
area;
   }
    }
   break;
   
   
   case 
2:
   {

   public 
void determineSphereVolume()
   {
      
Scanner input = new ScannerSystem.in );

      
System.out.print( "Enter radius of sphere: " );
      
double radius input.nextDouble();

      
System.out.printf"Volume is %f\n"sphereVolumeradius ) );
   }

   public 
double sphereVolumedouble radius )
   {
      
double volume = ( 4.0 3.0 ) * Math.PI Math.powradius);
      return 
volume;
   } 
}

    }
    break;
    
    case 
3:
   {

   public 
void determineSquareArea()
   {
      
Scanner input = new ScannerSystem.in );

      
System.out.print( "Enter length of square: " );
      
double length input.nextDouble();

      
System.out.printf"Volume is %f\n"squareArealength ) );
   }

   public 
double squareAreadouble length )
   {
      
double area = ( 4.0 3.0 ) * Math.powlength);
      return 
area;
   }
}
    }
    break;
    
    case 
4:
   {

   public 
void determineCubeVolume()
   {
      
Scanner input = new ScannerSystem.in );

      
System.out.print( "Enter length of sphere: " );
      
double length input.nextDouble();

      
System.out.printf"Volume is %f\n"cubeVolumelength ) );
   }

   public 
double cubeVolumedouble length )
   {
      
double volume = ( 4.0 3.0 ) * Math.powlength);
      return 
volume;
   }

    }
    break;
   
               } 
          } 
       }
}

Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-13-2008, 06:59 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,959
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.


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
__________________
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-13-2008, 07:01 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,959
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-13-2008, 08:25 AM
Member
 
Join Date: Feb 2008
Posts: 5
cart1443 is on a distinguished road
Thanks for your help.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 03-13-2008, 08:34 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,959
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 03-13-2008, 06:14 PM
Member
 
Join Date: Feb 2008
Posts: 5
cart1443 is on a distinguished road
Ya. It was perfect. I learned something new. Thanks.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 03-14-2008, 04:48 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,959
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Switch Statement Help bluegreen7hi New To Java 6 02-06-2008 06:16 AM
Switch statement to display the name of the month Java Tip Java Tips 0 01-04-2008 10:32 AM
Help with gigantamous switch statement trill New To Java 2 08-06-2007 09:11 AM
Problem with a switch statement in Java baltimore New To Java 2 08-02-2007 05:43 AM
Statement or Prepared Statement ? paty Database 3 08-01-2007 05:45 PM


All times are GMT +3. The time now is 06:09 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org