Project#4 and Calculator
Code
/// Name: Ali Kurland
/// Period: 6
/// Program Name: Calculator
/// File Name: Calculator.java
/// Date Finished: 4/29/2016
import java.util.Scanner;
public class Calculator
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
String a, b, c;
double d=0;
String op;
double end=0;
int q=0;
do
{
System.out.print("> ");
a = keyboard.next();
b = keyboard.next();
c = keyboard.next();
double first = Double.valueOf(a);
double second = Double.valueOf(c);
if ( isNumeric(a) )
{
if ( b.equals("+") )
d = first + second;
else if ( b.equals("-") )
d = first - second;
else if ( b.equals("*") )
d = first * second;
else if ( b.equals("/") )
d = first / second;
else if ( b.equals("%") )
d = first % second;
else if ( b.equals("^") )
{
d = first;
for ( int f=2; f<(second+1); f++ )
{
d = (first * d);
}
}
else
{
System.out.println("Undefined operator: '" + b + "'.");
q = 1;
}
if ( (first != 0) && (q==0))
System.out.println(d);
else if ( (first != 0) && (q!=0))
System.out.println();
else
end = 1;
}
} while ( end != 1 );
System.out.println("Bye, now.");
}
public static boolean isNumeric( String s )
{
return ( isDouble(s) || isInteger(s) );
}
public static boolean isDouble( String s )
{
double n = 0;
try
{
n = Double.valueOf( s );
}
catch ( NumberFormatException e )
{
return false;
}
return true;
}
public static boolean isInteger( String a )
{
int n = 0;
try
{
n = Integer.valueOf( a );
}
catch ( NumberFormatException e )
{
return false;
}
return true;
}
}
Picture of the output