Home > Java > an unusual java class

an unusual java class

I was reading a forum thread in www.daniweb.com where someone was asking if there is any way to pick what method to run on run time rather than specifying what method to run during development. I have thought of the same problem before, and I decided to see if this is possible by trying to actually build a class that does this. Here is the code:


import javax.swing.*;

public class MixTapes{

public static void main(String[] args){
int s = 0;
while(s != -1){
s = new Integer(JOptionPane.showInputDialog("pick method:\n1 fo method1\n2 for method2\n25 for method25\n-1 for quiting")).intValue();
pickWhichMethodToRun(s);
}

}

public static void method1(){
System.out.println("First method has been chosen");
}

public static void method2(){
System.out.println("Second method has been chosen");
}

public static void method25(){
System.out.println("Twenty-fifth method has been chosen");
}
public static void pickWhichMethodToRun(int s){

switch(s){
case 1:
method1();
break;

case 2:
method2();
break;

case 25:
method25();
break;

default:
System.out.println("haven't picked an existing method");
}

}

}

Advertisements
Categories: Java
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: