Project #4
Code
//Colin Hinton
//5th Period
//Calc.java
//3/9/2016
import java.util.Scanner;
public class Calc
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
double a, b, c;
String op;
do
{
System.out.print("> ");
a = keyboard.nextDouble();
op = keyboard.next();
b = keyboard.nextDouble();
if ( op.equals("+") )
System.out.println(Add(a,b));
else if (op.equals("-"))
System.out.println(Sub(a,b));
else if (op.equals("*"))
System.out.println(Mul(a,b));
else if (op.equals("/"))
System.out.println(Div(a,b));
else if (op.equals("^")) //write your own
System.out.println(Exp(a,b));
else if (op.equals("%"))
System.out.println(Mod(a,b));
else if (op.equals("!"))
System.out.println(Fac(a));
else if (op.equals("sin"))
System.out.println(Sin(b));
else if (op.equals("cos"))
System.out.println(Cos(b));
else if (op.equals("tan"))
System.out.println(Tan(b));
else if (op.equals("(-)"))
System.out.println(Neg(b));
else
{
System.out.println("Undefined operator: '" + op + "'.");
}
} while ( a != 0 );
}
public static Double Add(Double c, Double d)
{
Double e = c + d;
return e;
}
public static Double Sub(Double c, Double d)
{
Double e = c - d;
return e;
}
public static Double Mul(Double c, Double d)
{
Double e = c*d;
return e;
}
public static Double Div(Double c, Double d)
{
Double e = c/d;
return e;
}
public static Double Exp(Double c, Double d) //without Math.pow
{
Double pow = c;
for(int x=1;x 1; x = x - 1)
{
c = c*(x-1);
}
return c;
}
public static Double Sin(Double d)
{
Double e = Math.sin(d);
return e;
}
public static Double Cos(Double d)
{
Double e = Math.cos(d);
return e;
}
public static Double Tan(Double d)
{
Double e = Math.tan(d);
return e;
}
public static Double Neg(Double d)
{
Double e = 0 - d;
return e;
}
}
Picture of the output