Assignement: Project4: A Basic Calculator
Code
/// Name: Sean Harrison
/// Period: 7
/// Program name: CalcProject
/// File Name: CalcProject.html
/// Date Finished: 4/14/2016
import java.util.Scanner;
public class CalcProject
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
double n1, n2, n3;
String operation;
do
{
System.out.print("> ");
n1 = keyboard.nextDouble();
operation = keyboard.next();
n2 = keyboard.nextDouble();
if ( operation.equals("+") )
{
n3 = add(n1, n2);
}
else if ( operation.equals("-") )
{
n3 = subtract(n1, n2);
}
else if ( operation.equals("*") )
{
n3 = multiply(n1, n2);
}
else if ( operation.equals("/") )
{
n3 = divide(n1, n2);
}
else if ( operation.equals("^") )
{
n3 = exponent(n1, n2);
}
else
{
System.out.println("Undefined operator: '" + operation + "'.");
n3 = 0;
}
System.out.println(n3);
} while ( n1 != 0 );
System.out.println("Bye, now.");
}
public static double add( double n1, double n2 )
{
double total;
total = n1 + n2;
return total;
}
public static double subtract( double a, double b)
{
double total;
total = a - b;
return total;
}
public static double multiply( double n1, double n2)
{
double total;
total = n1 * n2;
return total;
}
public static double divide( double n1, double n2)
{
double total;
total = n1 / n2;
return total;
}
public static double exponent( double n1, double n2)
{
double total = n1;
for ( double x = 1; x < n2; x++)
total = total * n1;
return total;
}
}
Picture of the output