Anda di halaman 1dari 4

//the code for the basic calculator

public class Basic{


private String add;
private String sub;
private String mult;
private String div;

public Basic (String a, String s, String m, String d){


add=a;
sub=s;
mult=m;
div=d;
}
//getters
public void setAdd(String a)
{
add=a;
}
public void setSub(String s)
{
sub=s;
}
public void setMult(String m)
{
mult=m;
}
public void setDiv(String d)
{
div=d;
}

// getters
public String getAdd()
{
return add;
}

public String getSub()


{
return sub;
}
public String getMult()
{
return mult;
}
public String getDiv()
{
return div;
}
}

//the code for the scientific calculator

public class Science{


private String sin;
private String cos;
private String tan;

public Science (String s, String c, String t){


sin=s;
cos=c;
tan=t;
}
//setters
public void setSin(String s)
{
sin=s;
}
public void setCos(String c)
{
cos=c;
}
public void setTan(String t)
{
tan=t;
}
// getters
public String getSin()
{
return sin;
}
public String getCos()
{
return cos;
}
public String getTan()
{
return tan;
}

}
//the main code for the calculator and menu
/*group 3:
december 16 2008
11 pm
*/
import javax.swing.JOptionPane;
public class Calculator{

public static void main (String []args){


Basic arth=new Basic("add", "sub", "mult", "div");
Science arth2=new Science ("sin", "cos", "tan");

while(true){ //MAIN MENU

int choice=Integer.parseInt (JOptionPane.showInputDialog ("enter your choice:\n 1.


basic 2. scientific 3. exit"));//MAIN MENU

//THE CHOICES:
if (choice==1)
{
int basic=Integer.parseInt (JOptionPane.showInputDialog ("do you want to: \n 1. add
\n 2. subtract \n 3. multiply \n 4. divide"));
//THE CHOICES UNDER THE BASIC CALCULATOR
switch (basic)
{
case 1:
String add=JOptionPane.showInputDialog ("enter first number");
String add2=JOptionPane.showInputDialog ("enter second number");
float anum1=Float.parseFloat(add);
float anum2=Float.parseFloat(add2);
float sum=anum1+anum2;
JOptionPane.showMessageDialog (null, "the sum is "+sum);
break;

case 2:
String sub=JOptionPane.showInputDialog ("enter first number");
String sub2=JOptionPane.showInputDialog ("enter second number");
float snum1=Float.parseFloat(sub);
float snum2=Float.parseFloat(sub2);
float dif=snum1-snum2;
JOptionPane.showMessageDialog (null, "the difference is "+dif);
break;

case 3:
String mult=JOptionPane.showInputDialog ("enter first number");
String mult2=JOptionPane.showInputDialog ("enter second number");
float mnum1=Float.parseFloat(mult);
float mnum2=Float.parseFloat(mult2);
float prod=mnum1*mnum2;
JOptionPane.showMessageDialog (null, "the product is "+prod);
break;

case 4:
String div=JOptionPane.showInputDialog ("enter first number");
String div2=JOptionPane.showInputDialog ("enter second number");
float dnum1=Float.parseFloat(div);
float dnum2=Float.parseFloat(div2);
float quo=dnum1/dnum2;
JOptionPane.showMessageDialog (null, "the quotient is "+quo);
break;
}
}

else if (choice==2)
{
int science=Integer.parseInt (JOptionPane.showInputDialog ("choose a funtion \n 1.
sine \n 2. cosine \n 3. tangent"));

//THE CHOICES UNDER THE SCIENTIFIC CALCULATOR


switch (science)
{
case 1:
String sin=JOptionPane.showInputDialog ("enter number");
double sinnum=Double.parseDouble(sin);
JOptionPane.showMessageDialog (null, "the answer is "+Math.sin(sinnum));
break;
case 2:
String cos=JOptionPane.showInputDialog ("enter number");
double cosnum=Double.parseDouble(cos);
JOptionPane.showMessageDialog (null, "the answer is "+Math.cos(cosnum));
break;
case 3:
String tan=JOptionPane.showInputDialog ("enter number");
double tannum=Double.parseDouble(tan);
JOptionPane.showMessageDialog (null, "the answer is "+Math.tan(tannum));
break;
}
}
/*note that these are converted to radians and not degrees, or grad
to convert the answer into degress, this is added:
Math.sin(Math.toDegrees(sinnum));*/

else if (choice==3)
{
JOptionPane.showMessageDialog (null, "bye!!!");
System.exit(0);
}

else {
JOptionPane.showMessageDialog (null, "unknown command");
}
}
}
}

-o0o-ending (it worked) yehey –o0o-

Anda mungkin juga menyukai