Assignment Project 4 and Calculator
Code
/// Name: Graham Pollock
/// Period: 5
/// Program Name: Calculator
/// File Name: Calculator.java
/// Date Finished: 04/10/2016/
import java.util.Scanner;
public class Calculator
{
public static void main(String[] args)
{
Scanner kb = new Scanner (System.in);
double a, b, c;
String op;
do
{
System.out.print("> ");
a = kb.nextDouble();
op = kb.next();
b = kb.nextDouble();
if (op.equals("+"))
c = add(a, b);
else if (op.equals("-"))
c = subtract(a, b);
else if (op.equals("*"))
c = multi(a, b);
else if (op.equals("/"))
c = divide(a, b);
else if (op.equals("%"))
c = modulus(a, b);
else if (op.equals("!"))
c = fact(a);
else if (op.equals("^"))
c = exp(a, b);
else
{
System.out.println("The operation '" + op + "' is not valid.");
c = 0;
}
if (a != 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 multi(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 modulus(double a, double b)
{
double total;
total = a % b;
return total;
}
public static double fact(double a)
{
double total = a;
double z = a - 1;
for (double n = z; n > 0; n--)
{
total = total * n;
}
return total;
}
public static double exp(double a, double b)
{
double total = a;
for (int n = 1; n < b; n++)
{
total = total * a;
}
return total;
}
}
Picture of the output