Methods:

public class metod {
    public static String computation1 {
        JOptionPane.showMessageDialog(null," Area of a Circle");
        String n = JOptionPane.showInputDialog(null,"Enter a Number");  
        int num = Integer.parseInt(n);
        int form1 = (3.14 * (num*2));
        JOptionPane.showMessageDialog(null,"… is "+form1);
    }

    public static String computation2 {
        JOptionPane.showMessageDialog(null,"… of a Trapezoid");
        String h = JOptionPane.showInputDialog(null,"Enter Height");
        int heig = Integer.parseInt(h);
        String b1 = JOptionPane.showInputDialog(null,"Enter Base1");
        int bas = Integer.parseInt(b1);
        String b2 = JOptionPane.showInputDialog(null,"Enter Base2");
        int base = Integer.parseInt(b2);
        int form2 = (heig * (bas+base) / 2);
        JOptionPane.showMessageDialog(null,"… is "+form2);
    }
    public static String computation3 {
        JOptionPane.showMessageDialog(null,"… of a Sphere");
        String r = JOptionPane.showInputDialog(null,"Enter Radius");
        int rad = Integer.parseInt(r);
        int form3 = (.75 * 3.14 *(rad*2));
        JOptionPane.showMessageDialog(null,"… is "+form3);
    }
    public static String computation4 {
        JOptionPane.showMessageDialog(null,"… of a Cylinder");
        String r2 = JOptionPane.showInputDialog(null,"Enter Radius");
        int radiu = Integer.parseInt(r2);
        String h2 = JOptionPane.showInputDialog(null,"Enter Height");
        int heigh = Integer.parseInt(h2);
        int form4 = (3.14 * (radiu * 2) + heigh);   
        JOptionPane.showMessageDialog(null,"… is "+form4);
    }
}

Main:

import javax.swing.JOptionPane;
public class main {

    public static void main (String[]srgs){
        JOptionPane.showMessageDialog(null,"1… of a Circle\n" + "2.Area of a Trapezoid\n" + "3.Volume of a Sphere\n" + "4.Volume of a Cylinder");
    String ipt = JOptionPane.showInputDialog(null,"Input your desired choice(1,2,3,4)");
    Integer iptipt = Integer.parseInt(ipt); 

        switch (iptipt)
        {
            case 1 :
            {
                JOptionPane.showMessageDialog(null,"\n" + computation1.getcomputation1); break;
            }
            case 2 :
            {
                JOptionPane.showMessageDialog(null,"\n" + computation2.getcomputation2); break; 
            }   
            case 3 :
            {
                JOptionPane.showMessageDialog(null,"\n" + computation3.getcomputation3); break;
            }
            case 4 :
            {
                JOptionPane.showMessageDialog(null,"\n" + computation4.getcomputation4); break;
            }
            default :
            {
                JOptionPane.showMessageDialog(null,"Unknown");
            }
        }   
    }
}

this is wrong completely... i just want to know how would i make this work.. this is on method and switch case staments....(java language)

Recommended Answers

All 12 Replies

Use code tags when posting code. Click the button code and put your code inside.
Then explain your problem. What errors do you get?

public class metod {
public static String computation1 {
JOptionPane.showMessageDialog(null," Area of a Circle");
String n = JOptionPane.showInputDialog(null,"Enter a Number"); 
int num = Integer.parseInt(n);
int form1 = (3.14 * (num*2));
JOptionPane.showMessageDialog(null,"… is "+form1);
}

public static String computation2 {
JOptionPane.showMessageDialog(null,"… of a Trapezoid");
String h = JOptionPane.showInputDialog(null,"Enter Height");
int heig = Integer.parseInt(h);
String b1 = JOptionPane.showInputDialog(null,"Enter Base1");
int bas = Integer.parseInt(b1);
String b2 = JOptionPane.showInputDialog(null,"Enter Base2");
int base = Integer.parseInt(b2);
int form2 = (heig * (bas+base) / 2);
JOptionPane.showMessageDialog(null,"… is "+form2);
}
public static String computation3 {
JOptionPane.showMessageDialog(null,"… of a Sphere");
String r = JOptionPane.showInputDialog(null,"Enter Radius");
int rad = Integer.parseInt(r);
int form3 = (.75 * 3.14 *(rad*2));
JOptionPane.showMessageDialog(null,"… is "+form3);
}
public static String computation4 {
JOptionPane.showMessageDialog(null,"… of a Cylinder");
String r2 = JOptionPane.showInputDialog(null,"Enter Radius");
int radiu = Integer.parseInt(r2);
String h2 = JOptionPane.showInputDialog(null,"Enter Height");
int heigh = Integer.parseInt(h2);
int form4 = (3.14 * (radiu * 2) + heigh);	
JOptionPane.showMessageDialog(null,"… is "+form4);
}
}

Erros
class, interface, or enum expected

Main:

import javax.swing.JOptionPane;
public class main {

public static void main (String[]srgs){
JOptionPane.showMessageDialog(null,"1… of a Circle\n" + "2.Area of a Trapezoid\n" + "3.Volume of a Sphere\n" + "4.Volume of a Cylinder");
String ipt = JOptionPane.showInputDialog(null,"Input your desired choice(1,2,3,4)");
Integer	iptipt = Integer.parseInt(ipt);	

switch (iptipt)
{
case 1 :
{
JOptionPane.showMessageDialog(null,"\n" + computation1.getcomputation1); break;
}
case 2 :
{
JOptionPane.showMessageDialog(null,"\n" + computation2.getcomputation2); break;	
}	
case 3 :
{
JOptionPane.showMessageDialog(null,"\n" + computation3.getcomputation3); break;
}
case 4 :
{
JOptionPane.showMessageDialog(null,"\n" + computation4.getcomputation4); break;
}
default :
{
JOptionPane.showMessageDialog(null,"Unknown");
}
}	
}
}

errors:
cannot find symbol variable computation1
cannot find symbol variable computation2
cannot find symbol variable computation3
cannot find symbol variable computation4

~ this is wrong i know. its just that this is how it should like like.. i need a method and put it in the case statements to call it.. but i don't know how to.

computation1 is a static method of the class metod. You need to call it like this: metod.computation1(arguments)

Also you haven't declared the methods correctly in the metod class.

Try to read some tutorials on how to create and call methods.

could you help me in declaring the methods in metod class. just give me one example that i can understand. believe me ive been reading and searching and even watching in youtube just to try and understand and still i can't please help.

Search any tutorial or open any basic book.
Also this is very important. Don't rely on the free solutions of the internet. If you want to study and learn, buy a book.

And in another thread you said that you have already studied the basics. It is not our job to teach here the language step by step. You need to put effort and study yourself.

Anyway, in order to declare a method:

public [static] <return_type> methodName(arguments or empty) {

}

[] : that is optional

public static int methodName(int a, String b, ...... ) {
  ....
  return 0; // since it is declared to return int
}

When you declare or define a method a method has a method signature which is the access modifier ( public, private, or non which makes it package level ), then you have a return type ( void or of some type such as String, Int ( integer ) , etc ) , then there is the name of the method that you create or want , next is the arguments that it takes that go in parenthesis.

So an example would be

public void changeColor()

That is called a method signature.
The body is every thing after the open bracket up to the closing bracket...

public void changeColor(){

//Body is here
//Statements go in body

}

A methods arguments that go inside the parenthesis is used for passing values and references around. Think of the methods arguments as an entrance for things (objects and primitive types).

public void changeColor(Color color){

//Do things with the color passed into this method

}

Some other method may invoke or call that method...

car.changeColor(red);

//Now the cars color will be red

Methods are used to do actions, behaviors, operations on objects.They can change the state of an object ( mutator methods ) or they can get/receive information from an object ( getters/Accessor methods );

I hope the short summary was what you were looking for and it helps.

here i got the anz to your all question with correction of you program so go through it and try to understand...

import javax.swing.*;
import java.text.DecimalFormat;

class test
{
        public static double form1,form3,form4;
        public static int form2;

        test()
{
        form1=0;
        form2=0;
        form3=0;
        form4=0;
}}

class method
{
        public static void computation1(boolean bl) {


       DecimalFormat twoDigit = new DecimalFormat("0.00");

       if(bl==true)
{

       String n = JOptionPane.showInputDialog(null,"Enter a Number");
       int num = Integer.parseInt(n);
       test.form1 =  3.14 * (num*2);
       JOptionPane.showMessageDialog(null,"… is "+twoDigit.format(test.form1));
}
       else
       JOptionPane.showMessageDialog(null,"… is "+twoDigit.format(test.form1));
}

       public static void computation2(boolean b)
{
       if(b==true)
{
      JOptionPane.showMessageDialog(null,"… of a Trapezoid");
      String h = JOptionPane.showInputDialog(null,"Enter Height");
      int heig = Integer.parseInt(h);
      String b1 = JOptionPane.showInputDialog(null,"Enter Base1");
      int bas = Integer.parseInt(b1);
      String b2 = JOptionPane.showInputDialog(null,"Enter Base2");
      int base = Integer.parseInt(b2);
      test.form2 = (heig * (bas+base) / 2);
      JOptionPane.showMessageDialog(null,"… is "+test.form2);
}
      else
      JOptionPane.showMessageDialog(null,"… is "+test.form2);
}


      public static void computation3(boolean a) {

      if(a==true)
{
      JOptionPane.showMessageDialog(null,"… of a Sphere");
      String r = JOptionPane.showInputDialog(null,"Enter Radius");
      int rad = Integer.parseInt(r);
      test.form3 = (.75 * 3.14 *(rad*2));
      JOptionPane.showMessageDialog(null,"… is "+test.form3);
}
      else
      JOptionPane.showMessageDialog(null,"… is "+test.form3);
}

      private static void  computation4(boolean A) {

     if(A==true)
{
     JOptionPane.showMessageDialog(null,"… of a Cylinder");
     String r2 = JOptionPane.showInputDialog(null,"Enter Radius");
     int radiu = Integer.parseInt(r2);
     String h2 = JOptionPane.showInputDialog(null,"Enter Height");
     int heigh = Integer.parseInt(h2);
     test.form4 = (3.14 * (radiu * 2) + heigh);
     JOptionPane.showMessageDialog(null,"… is "+test.form4);
}
     else
     JOptionPane.showMessageDialog(null,"… is "+test.form4);
}

     public static void main(String[] args)
{
     computation1(true);
     computation2(true);
     computation3(true);
     computation4(true);

     String D_Play="you enter the wrong number";

     String ipt = JOptionPane.showInputDialog(null,"Input your desired choice(1,2,3,4)");
     int iptipt = Integer.parseInt(ipt);

     switch(iptipt)
{
     case 1:
     computation1(false);
     break;

     case 2:
     computation2(false);
     break;

     case 3:
     computation3(false);
     break;

     case 4:
     computation3(false);
     break;

     default:
     JOptionPane.showMessageDialog(null,D_Play,"Alert",JOptionPane.ERROR_MESSAGE);

}}
} //  ending  class method

now these are your mistake try to void it again

problem 1: int form1 = (3.14 * (num*2));

you are calculating double value and then you are assigning it to int datatype thats not the way you should take form1 as double and futher you have then the same mistake here int form3 = (.75 * 3.14 *(rad*2)) and here int form4 = (3.14 * (radiu * 2) + heigh);

problem 2: public static String computation1

it would better if you use return statement public static void computation2()

problem 3: DecimalFormat twoDigit = new DecimalFormat("0.00");

I have used DecimalFormat class so that if you get bigger double number e.g 2.4566666 so this class will round it to twodigit e.g 2.46.

problem 4: JOptionPane.showMessageDialog(null,"\n"+computation1.getcomputation1);break;

By going through your code i come to known you want to access your method in switch statement by entering 1,2,3,4 as input but the problem was that this statement "computation1.getcomputation1" access your full method again instead of giving the specfic ans of computation1,2,3,4 method e.g 6.56 so thats why i made a new class test and initialize form1,form2 etc in it and then use boolean so that if you want the specfic anz of method computation1,2,3,4 you can access it.

HOPE IT WORKS FOR YOU AND LET ME KNOWN

please mark the problem as solve if you are done

i need excercises for switch case with methods

@miltondollar. Please do not violate forum rules.
Do not hijack old dead threads. Start you own new thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.