Java Programming Main topics: User validated input, Basic String Methods, if & i
ID: 3675754 • Letter: J
Question
Java Programming
Main topics: User validated input, Basic String Methods, if & if - else Statements, Loop Statements
Program Specification
Write a Java program that emulates a simple calculator:
• Declare and initialize a String variable (expression) to the empty string ""
• Prompt the user to input the left operand and read it into a double variable (lOperand)
• Declare and initialize a double variable (resultSoFar) to lOperand’s value
• Concatenate lOperand’s value to the expression string
• Repeat the following until the ( Exit ) option is selected:
1. Display a menu which gives the user the choice of the following menu options:
1 -> Multiplication
2 -> Division
3 -> Addition
4 -> Subtraction
5 -> Exit
and uses a user validation loop to read their response into a String variable.
Note: You must repeatedly get the users response until it is exactly one of the characters 1 2 3 4 5
2. If ( Exit ) was NOT selected:
(a) Concatenate the operator selected to the expression string
(b) Prompt the user to input the right operand and read it into a double variable (rOperand)
(c) Concatenate rOperand’s value to the expression string
(d) Compute the result of the arithmetic expression, based on the current value of resultSoFar (left operand), the operator indicated, and the value of rOperand. Store this result back into the variable resultSoFar
• Display the entire expression with its result.
Sample run(s):
---------------------------------------------
Enter the left operand : 1
---------------------------------------------
1 -> Multiplication
2 -> Division
3 -> Addition
4 -> Subtraction
5 -> Exit
---------------------------------------------
Choose an operator from the above menu : 6
Choose an operator from the above menu : -1
Choose an operator from the above menu : a
Choose an operator from the above menu : 1
---------------------------------------------
Enter the right operand : 2
---------------------------------------------
1 -> Multiplication
2 -> Division
3 -> Addition
4 -> Subtraction
5 -> Exit
---------------------------------------------
Choose an operator from the above menu : 2
---------------------------------------------
Enter the right operand : 3
---------------------------------------------
1 -> Multiplication
2 -> Division
3 -> Addition
4 -> Subtraction
5 -> Exit
--------------------------------------------
Choose an operator from the above menu : 3
---------------------------------------------
Enter the right operand : 4
---------------------------------------------
1 -> Multiplication
2 -> Division
3 -> Addition
4 -> Subtraction
5 -> Exit
---------------------------------------------
Choose an operator from the above menu : 4
---------------------------------------------
Enter the right operand : 5
---------------------------------------------
1 -> Multiplication
2 -> Division
3 -> Addition
4 -> Subtraction
5 -> Exit
---------------------------------------------
Choose an operator from the above menu : 5
---------------------------------------------
1.0 * 2.0 / 3.0 + 4.0 - 5.0 = -0.33333333333333304
---------------------------------------------
Explanation / Answer
import java.text.DecimalFormat;
import java.util.Scanner;
public class DoTheMath_A {
public static void main(String[] args) {
// First number storage
double num1 = getFirstNumber(); // Value - returning method.
// Second number storage
double num2 = getSecondNumber(); // Value - returning method.
// Displays Math Operations menu
showMenu(); // Void - returning method
// Math Operation choice storage
int choice = getSelection(); // Value - returning method
// Calculates the users numbers into an result. No output.
double result = doTheMath(choice, num1, num2); // Value - returning
// method
// Displays the output with formatting to show 2 decimal places.
displayResult(choice, result, num1, num2); // Void - returning method
} // end of main()
// Value - returning method =======================================
private static double getFirstNumber() {
Scanner input = new Scanner(System.in);
System.out.print("Enter the first number: ");
double num1 = input.nextDouble(); // First number storage
return num1; // Returns First number to main
} // end of getFirstNumber()
// Value - returning method =======================================
private static double getSecondNumber() {
Scanner input = new Scanner(System.in);
System.out.print(" Enter the second number - "
+ "(Cannot be zero for Divison): ");
double num2 = input.nextDouble(); // Second number storage
return num2;
} // end of getSecondNumber()
// Void - returning method ============================
private static void showMenu() {
System.out.print(" 1. Addition 2. Subtract 3. Multiply"
+ " 4. Divide Which math operation? "
+ "Enter your choice: ");
} // end of showMenu()
// Value - returning method =======================================
private static int getSelection() {
Scanner input = new Scanner(System.in);
int choice = input.nextInt(); // Reads User pick for math operation and
// stores it into 'choice'
switch (choice) { // Start of Switch
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
default:
System.out.println("Illegal Operation");
} // end of Switch
return choice;
} // end of getSelection()
// Value - returning method =======================================
private static double doTheMath(int choice, double num1, double num2) {
double result;
if (choice == 1)
result = num1 + num2;
else if (choice == 2)
result = num1 - num2;
else if (choice == 3)
result = num1 * num2;
else
result = num1 / num2;
return result;
} // end of doTheMath(int choice, double num1, double num2)
// Void - returning method =======================================
private static void displayResult(int choice, double result, double num1, double num2) {
// formatting numbers to 1 decimal places
DecimalFormat df = new DecimalFormat("#,###,##0.0");
if (choice == 1)
System.out.println(num1 + " + " + num2 + " = " + df.format(result));
else if (choice == 2)
System.out.println(num1 + " - " + num2 + " = " + df.format(result));
else if (choice == 3)
System.out.println(num1 + " * " + num2 + " = " + df.format(result));
else
System.out.println(num1 + " / " + num2 + " = " + df.format(result));
} // end of displayResult(int choice, double result, double num1, double
// num2)
} // end of main()
let's look at the method bodies of getFirstNumberand getSecondNumber:
getFirstNumber:
Scanner input = new Scanner(System.in);
System.out.print("Enter the first number: ");
double num1 = input.nextDouble(); // First number storage
return num1; // Returns First number to main
getSecondNumber:
Scanner input = new Scanner(System.in);
System.out.print(" Enter the second number - "
+ "(Cannot be zero for Divison): ");
double num2 = input.nextDouble(); // Second number storage
return num2;
These two methods look identical, except for the fact that the second method added a 2 to the end of the variable names, and added a different message.
Since these are so similar, you might as well just merge these two methods into a single method; after all, they both return a double input from the user.
You can also use a do while loop for getting the right input.
It will be something like this:
do
{
System.out.println("Enter your choice. Enter 5 for exit.");
int a=sc.nextInt;
}
while(a!=5)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.