Assignemnt Project #4 and Calculator
Code
///Name: Daniel Tiffany-Appleton
///Period: 7
///Program Name: Calculator
///File Name: Calculator
///Date Finished: 3/18/16
import java.util.Scanner;
public class Calculator
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
double intA, intB, answer;
String op;
System.out.println("");
System.out.println("Calculator version 0.12 (c) 2015 TA Inc." );
do
{
System.out.print("> ");
intA = keyboard.nextDouble();
op = keyboard.next();
if( intA==0 )
{
System.out.println("Bye now.");
return;
}
if(op.equals("!"))
{
answer = Factorials(intA);
System.out.println(answer);
continue;
}
intB = keyboard.nextDouble();
answer = 1;
if( op.equals("+"))
{
answer = intA + intB;
System.out.println(answer);
}
else if( op.equals("-"))
{
answer = intA - intB;
System.out.println(answer);
}
else if( op.equals("*"))
{
answer = intA * intB;
System.out.println(answer);
}
else if( op.equals("/"))
{
answer = intA / intB;
System.out.println(answer);
}
else if( op.equals("^"))
{
answer = Power(intA,intB);
System.out.println(answer);
}
else if( op.equals("%"))
{
answer = intA % intB;
System.out.println(answer);
}
else
{
System.out.println("ERROR! UNRECOGNIZED OPERATION!");
}
}while(intA != 0);
}
public static double Power(double intA, double intB)
{
double answer, power;
answer = 1;
power = 0;
while(power != intB)
{
answer = intA * answer;
power++;
}
return answer;
}
public static double Factorials(double intA)
{
double answer, n, x;
answer = 1;
x = 1;
for(n = 1; n <= intA; n++)
{
answer = x * n;
x = answer;
}
return answer;
}
}
Picture of the output