Project 4
Code
/// Name: Xinting Chen
/// Period: 7
/// Program Name: Project 4
/// File Name: draft.java
/// Date Finished: 5/9/2016
import java.util.Scanner;
public class draft
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
String a = "";
double c = 0;
System.out.println(" Calculator ");
do
{
System.out.print("> ");
a = keyboard.next();
if (Numeric.isNumeric(a))
{
double d = Double.parseDouble(a);
c = basic(d, keyboard);
} else c = trig(a, keyboard);
c = threeDecimals(c);
if (!a.equals("0")) System.out.println(c);
{
System.out.println("Wrong input, retry.");
}
} while (!a.equals("0"));
System.out.println(" Bye ");
}
public static double threeDecimals(double c)
{
c *= 1000;
c = Math.round(c);
c /= 1000;
return c;
}
public static double basic(double a,Scanner keyboard)
{
String choice = keyboard.next();
double b = keyboard.nextDouble();
switch (choice) {
case "+":
return (a + b);
case "-":
return (a - b);
case "*":
return (a * b);
case "/":
return (a / b);
case "^":
return (Math.pow(a,b));
default: {
System.out.println("Undefined operator: '" + choice + "'.");
return 0;
}
}
}
public static double trig(String a,Scanner keyboard)
{
double b = keyboard.nextDouble();
b*= Math.PI/180;
switch (a)
{
case "sin":
return (Math.sin(b));
case "cos":
return (Math.cos(b));
case "tan":
return (Math.tan(b));
case "cot":
return (Math.pow(Math.tan(b),-1));
case "sec":
return (Math.pow(Math.cos(b),-1));
case "csc":
return (Math.pow(Math.sin(b),-1));
default:
{
System.out.println("Undefined operator: '" + a + "'.");
return 0;
}
}
}
}
Picture of the output