Project 4 calc.java

Code

    /// Name: Boris Kazantsev
    /// Period: 6
    /// Program Name: calc
    /// File Name: calc.java
    /// Date Finished: 5/21/2016
    
  import java.util.Scanner;
import java.util.InputMismatchException;

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

		String a = "";
		double c = 0;

		System.out.println("Basic calculator");

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

					if (Numeric.isNumeric(a)) {
						double d = Double.parseDouble(a);
						c = basicCalc(d, 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");
	}

	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);
			default: {
				System.out.println("ERROR: '" + op + "'.");
				return 0;
			}
		}
	}
}



   
    

Picture of the output

Pic