Project 4

code

///Name: Mutsu Osoegawa
///Period: 7
///Project Name: Calculator
///File Name: Calculator.java
///Date: 4/12/2016

import java.util.Scanner;

public class Calculator
{
	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("+") )
            {
				c = add(a, b);
            }
            else if ( op.equals("-") )
            {
                c = subtract(a, b);
            }
            else if ( op.equals("*") )
            {
                c = multiply(a, b);
            }
            else if ( op.equals("/") )
            {
                c = divide(a, b);
            }
            else if ( op.equals("^") )
            {
                c = exponent(a, b);
            }
			else
			{
				System.out.println("Undefined operator: '" + op + "'.");
				c = 0;
			}

			System.out.println(c);

		} while ( a != 0 );
        System.out.println("Bye, now.");
	}
    
    public static double add( double a, double b )
    {
        double total;
        total = a + b;
        return total;
    }
    public static double subtract( double a, double b)
    {
        double total;
        total = a - b;
        return total;
    }
    public static double multiply( double a, double b)
    {
        double total;
        total = a * b;
        return total;
    }
    public static double divide( double a, double b)
    {
        double total;
        total = a / b;
        return total;
    }
    public static double exponent( double a, double b)
    {
        double total = a;
        for ( double x = 1; x < b; x++)
            total = total * a;
        return total;
    }
}