Project #4

Code



        /// Name: Daniel Nguyen
    /// Period: 6
    /// Project Name: TrigCalc
    /// File Name: TrigCalc.java
    /// Date: 3/11/2015
import java.util.Scanner;
import java.util.InputMismatchException;

public class TrigCalc
{
	public static void main( String[] args )
	{
		Scanner keyboard = new Scanner(System.in);

		String a = "";
		double c = 0;

		System.out.println(" Trig Calculator (input in degrees) + Basic calculator by Iaroslav Titov");

		do
		{
				try
				{
					System.out.print("> ");
					a = keyboard.next();

					if (Numeric.isNumeric(a)) {
						double d = Double.parseDouble(a);
						c = basicCalc(d, keyboard);
					} else c = trigCalc(a, keyboard);

					c = roundToThreeDec(c);

					if (!a.equals("0")) System.out.println(c);
				}
				catch (InputMismatchException e)
				{
					System.out.println("Wrong input, retry.");
				}

		} while (!a.equals("0"));

		System.out.println("Bye now.");
	}

	public static double roundToThreeDec(double c)
	{
		c *= 1000;
		c = Math.round(c);
		c /= 1000;
		return c;
	}

	public static double basicCalc(double a,Scanner keyboard)
	{

		String op = keyboard.next();
		double b = keyboard.nextDouble();

		switch (op) {
			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: '" + op + "'.");
				return 0;
			}
		}
	}

	public static double trigCalc(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

Assignment 3
BACK HOME NEXT