In this lab, you will practice using multiple-alternative and nested if statemen
ID: 3556858 • Letter: I
Question
In this lab, you will practice using multiple-alternative and nested if statements in your
program.
For this task, you will create a five-function calculator. Name your class CheapCalculator. The
input to your program will be a binary arithmetic expression, with the operator separated
from the operands by a space like 24 * 4. The output will be the binary arithmetic
expression, followed by an equal sign, surrounded by spaces, followed by the answer, followed
by a newline..
You may assume that the operands will be floating-point numbers (use doubles). The operator
should be one of: *, /, +, -, or %. Heed the following additional specifications:
Emit an appropriate prompt
If the operator is not one of *, /, +, -, or %, issue an appropriate error message that
contains the offending operator.
If operator is "/" and the second operand is zero, issue an appropriate error message.
Try to avoid repeated code. You are free to do this as you wish, but consider using a
couple of boolean flags: divideByZero and badOperator, that your program sets when
it encounters those situations. Then use those flags at the end to determine whether
to emit an error message or the answer.
Shown below are several example runs to illustrate various possibilities.
So that is my assignment. I am struggling to make the bad Operator meaning that the user didn't enter one of the 4 operators +, *, -, and /. The if statement should print out a "BadOperator" but, I am struggling to make this happen.
This is what I have so far.
import java.util.Scanner;
public class CheapCalculator {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a binary arithmetic expression, with" +
" with spaces between operands and operator.");
double operand1 = in.nextDouble();
String operator = in.next();
double operand2 = in.nextDouble();
double total = 0.00;
if (operator.equals("+")) {
total = operand1 + operand2;
}
if (operator.equals("-")){
total = operand1 - operand2;
}
if (operator.equals("*")){
total = operand1 * operand2;
}
if (operator.equals("/")){
if (operand2 == 0){
System.out.println("Cannot divide by 0");
}
else {
total = operand1 / operand2;
}
if (operator.equals("+") || ("/") || ("-") || ("*")) // This is the part I am struggling to create// {
System.out.println("Sorry don't know operator " + operator );
}
}
System.out.println(operand1 + " " + operator + " " + "= " + total);
}
}
Any help would be greatly appreciated.
Explanation / Answer
import java.util.Scanner;
import java.io.*;
public class CheapCalculator {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter a binary arithmetic expression, with spaces between operands and operator.");
double operand1 = in.nextDouble();
String operator = in.next();
double operand2 = in.nextDouble();
double total = 0.00;
boolean divideByZero = false;
boolean badOperator = false;
if (operator.equals("+")) {
total = operand1 + operand2;
}
if (operator.equals("-")){
total = operand1 - operand2;
}
if (operator.equals("*")){
total = operand1 * operand2;
}
if (operator.equals("/"))
{
if(operand2 == 0)
{
divideByZero=true;
}
else
{
total = operand1 / operand2;
}
}
if (operator.equals("+") || operator.equals("/") || operator.equals("-") || operator.equals("*") )
badOperator= false;
else
badOperator=true;
if(badOperator)
System.out.println("Sorry dont know operator " + operator );
else
{
if(divideByZero)
System.out.println("Sorry Cannot divide by Zero");
else
{
System.out.println(String.valueOf(operand1) + " " + operator + " " +String.valueOf(operand2) + " = " + String.valueOf(total) );
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.