this is a program that the user is suppose to select a computation from a menu, the menu calls the function. and then the output is displayed in a nice java window. I think most of the coding is correct but i am having problems with the switch statement at the bottom of the code. The switch statement calls the new MyFuntions man and man calls the void function that does the computation. My compiler is lame. all it's telling me is that "case 1:" is an orphan case. look fo case 1 all the way at the bottom.
bombs away...
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication40;
/**
*
* @author nancysmith
*/
import javax.swing.*;//for JOptionPane
import java.util.*;// input
import java.text.*;//decimal formating
import java.io.*;//?? s stream in java is the io
public class Main {
public static void main(String[] args) {
}//default constructor
//Menu
public int menu()
{
JOptionPane myWindow;
myWindow = new JOptionPane();
myWindow.setVisible(true);//window size, title, visibility
String myCal = "";//
myCal = JOptionPane.showInputDialog(
"Enter the number corresponding to the calculation" +
"you want me to preform:\n"
+" 1. Absoulte Vaule\n"
+" 2. Factorial\n"
+" 3. Fibonacci\n"
+" 4. Permuation\n"
+" 5. Combination\n"
+" 6. Radians_to_Degree\n"
+" 7. Degree_to_Radians\n"
+" 8. Determinant_2x2\n"
+" 9. My_exp\n"
+"10. Sin_of_x\n"
+"11. Cos_of_x\n"+"12. exit");
int selection =Integer.parseInt(myCal);
return selection;
}
//ABSOULUTE VALUE FUNCTION
public void absoulte()
{
String input1=JOptionPane.showInputDialog("Enter number\n");
double result = Double.parseDouble(input1);
if(result<0.0)
result = 1.0*result;
JTextArea outputAbsolute = new JTextArea();
outputAbsolute.setText("The Absolute of you number:" + result);
JOptionPane.showMessageDialog(null,outputAbsolute,"Absolute Value", JOptionPane.INFORMATION_MESSAGE);
}
public void factorial()
{
String input2=JOptionPane.showInputDialog("Enter n for for n!");
int temp = 1;
int result = Integer.parseInt(input2);
//Long.parseLong(input1);
for(int i = 1; i<=result;i++)
temp = temp*i;
//case for "0" as well
JTextArea outputFac = new JTextArea();
outputFac.setText( input2 +"!: "+ result);
JOptionPane.showMessageDialog(null,outputFac,"Factorial Value", JOptionPane.INFORMATION_MESSAGE);
}//end fac
// factorial overload
public void fibonacci()
{
String input3=JOptionPane.showInputDialog("Enter the nth Fibonacci Position: \n");
double n = Double.parseDouble(input3);
double Fn = 1/Math.pow(5,.5)*(Math.pow(((1+Math.pow(5,.5))/2),n)-(Math.pow(((1-Math.pow(5,.5))/2),n))); // fibonacci formula
JTextArea outputAbsolute = new JTextArea();
outputAbsolute.setText("The "+ input3 +
"th Fibonacci is "+ Fn);
JOptionPane.showMessageDialog(null,outputAbsolute,"nth Fibonacci Number", JOptionPane.INFORMATION_MESSAGE);
}
public void permutation()//int n, int r)// not the same as the n,r declared in the client
{
String input4=JOptionPane.showInputDialog("Enter n for permutation: r<=" +"n \n");
int n = Integer.parseInt(input4);
String input5=JOptionPane.showInputDialog("Enter r for permutation: r<=" +"n \n");
int r = Integer.parseInt(input5);
int nc = factorial(n);
int rc = factorial(r);
int perm = nc/r;
JTextArea outputAbsolute = new JTextArea();
outputAbsolute.setText(n+ "P"+ r+" = "+ perm);
JOptionPane.showMessageDialog(null,outputAbsolute,"Permutation", JOptionPane.INFORMATION_MESSAGE);
}
public void combination()//int n, int r)// not the same as the n,r declared in the client
{
String input6=JOptionPane.showInputDialog("Enter n for permutation: r<=" +"n \n");
double n = Double.parseDouble(input6);
String input7=JOptionPane.showInputDialog("Enter r for permutation: r<=" +"n \n");
double r = Double.parseDouble(input7);
double nr = n-r;
int combo;
//factorial(n)/(factorial(r)*factorial(nr));
int nc = factorial(n);
int rc = factorial(r);
int nrc =factorial(nr);
combo = nc/(rc*nrc);
JTextArea outputAbsolute = new JTextArea();
outputAbsolute.setText(n+ "C"+ r+" = "+ combo);
JOptionPane.showMessageDialog(null,outputAbsolute,"Combinations", JOptionPane.INFORMATION_MESSAGE);
}
public void radians_to_degrees()
{
String input8=JOptionPane.showInputDialog("Enter number in radians: \n");
double result = Double.parseDouble(input8);
result = (180/3.14159)*result;
JTextArea outputAbsolute = new JTextArea();
outputAbsolute.setText( " "+input8 +" radians is " +result+ " degrees");
JOptionPane.showMessageDialog(null,outputAbsolute,"Radians to Degrees", JOptionPane.INFORMATION_MESSAGE);
}
public void degrees_to_radians()
{
String input9=JOptionPane.showInputDialog("Enter number in degrees: \n");
double result = Double.parseDouble(input9);
result = (3.14159/180)*result;
JTextArea outputAbsolute = new JTextArea();
outputAbsolute.setText( " "+input9 +" degrees is " +result+ " radians");
JOptionPane.showMessageDialog(null,outputAbsolute,"Degrees to Radians", JOptionPane.INFORMATION_MESSAGE);
}
public void determinant_2x2()
{
String input10=JOptionPane.showInputDialog("Enter 2x2 matrix [a,b,c,d] seperated by spaces or commas\n");
Scanner scan = new Scanner(System.in);
StringTokenizer st = new StringTokenizer(input10, ","+" ");
String a1 = st.nextToken();
String b1 = st.nextToken();
String c1 = st.nextToken();
String d1 = st.nextToken();
Double a = Double.parseDouble(a1);
Double b = Double.parseDouble(b1);
Double c = Double.parseDouble(c1);
Double d = Double.parseDouble(d1);
result = (a*d)-(b*c);
JTextArea outputAbsolute = new JTextArea();
outputAbsolute.setText("The determinant of the 2x2 matrix ["+a+","+b+";"+c+","+d+"] is: "+ result);
JOptionPane.showMessageDialog(null,outputAbsolute,"Determinant of a 2x2 Matrix", JOptionPane.INFORMATION_MESSAGE);
}
public void my_exp()
{
String input11 = JOptionPane.showInputDialog("Enter number to apply my_exp: \n");
double y = Double.parseDouble(input11);
double sum = 0;
for (int i=1; i <= 30; i++)
sum = sum + Math.pow(y, i-1)/factorial(i-1);
JTextArea outputSine = new JTextArea();
outputSine.setText("The my_exp of " + input11 +": " +sum);
JOptionPane.showMessageDialog(null,outputSine,"my_exp Value", JOptionPane.INFORMATION_MESSAGE);
}
public void sine()
{
String input12 = JOptionPane.showInputDialog("Enter Angle in Degrees\n");
double result = Double.parseDouble(input12);
double x = result/57.269;//convert degrees to radian
double temp = 0.0;
int sign = 1; //alternate
for(int i = 1; i <= 7; i = i + 2)
{
temp = temp + sign*Math.pow(x, i)/factorial(i);
sign = sign*(-1);
}
JTextArea outputSine = new JTextArea();
outputSine.setText("Sine("+result+") is "+ temp);
JOptionPane.showMessageDialog(null,outputSine,"Sine Function Value", JOptionPane.INFORMATION_MESSAGE);
}
public void cosine()
{
String input13 = JOptionPane.showInputDialog("Enter Angle in Degrees\n");
double result = Double.parseDouble(input13);
double x = result/57.269;//convert degrees to radian
double temp = 0.0;
int sign = 1; //alternate
for(int i = 1; i <= 7; i++)
{
temp = temp + sign*Math.pow(x, 2*i-2)/factorial(2*i-2);
sign = sign*(-1);
}
JTextArea outputSine = new JTextArea();
outputSine.setText("Coine("+result+") is "+ temp);
JOptionPane.showMessageDialog(null,outputSine,"Cosine Function Value", JOptionPane.INFORMATION_MESSAGE);
}
if(selection>=12)
{
JOptionPane.showMessageDialog(null, "invalid choice");
}
myFunctions man = new myFunctions();
selection = man.menu();//fire menu function
// switch(choice)
// {
// case 1:
switch ( selection )
{
case 1:
man.absolute();
break;
case 2:
man.factorial();
break;
case 3:
man.fibonacci();
case 4:
man.permutation();
case 5:
man.combination();
case 6:
man.radians_to_degrees();
case 7:
man.degree_to_radians();
case 8:
man.Determinant_2x2();
case 9:
man.my_exp();
case 10:
man.sine();
case 11:
man.cosine();
case 12:
System.exit(0);
break;
default:
System.exit(0);
break;
System.exit(0);
}
}