DecimalFormat twoDecimals = new DecimalFormat( " #0.0");
//print a welcome message
System.out.println( "Welcome to the Calculator");
//read the operands
System.out.println( "Enter the first operand: ");
fp1 = scan.nextDouble();
System.out.print("Enter the second operand: ");
fp2 = scan.nextDouble();
//print a menu, then prompt for the operation
System.out.println( "\nOperations are: "
+"\n\t 1 for + or addition"
+"\n\t 2 for - for subtraction"
+"\n\t 3 for * for mulplication"
+"\n\t 4 for / for division"
+"\n\t 5 for ^ for power");
System.out.print( "Enter your selection: ");
operation= scan.next( );
operation= operation.toUpperCase( );
//perform the operation and print the result
switch ( operation)
{
case "1":
case "+":
System.out.println("The sum is "
+ twoDecimals.format( fp1+ fp2 ) );
break;
case "2":
case "-":
System.out.println("The diffrence is "
+ twoDecimals.format( fp1 - fp2) );
break;
case "3":
case "*":
System.out.println ("The product is"
+twoDecimals.format(fp1 * fp2) );
break;
case "4":
case "/":
if ( fp2== 0.0 )
System.out.println( "Dividing by 0 is not allowed, please enter a non zero value for fp2");
else
System.out.println( "The quotient is "
+ twoDecimals.format( fp1 / fp2 ) );
break;
case "5":
case "pow":
double result = Math.pow( fp1, fp2 );
System.out.println ("The power is"
+twoDecimals.format(fp2*2) );
default:
System.out.println( operation + " is not valid.");
}
}
}