Question

Hi. This is a prototype of Java. The following Java program was developed for prototyping a...

Hi. This is a prototype of Java.

The following Java program was developed for prototyping a mini calculator. Run the program and provide your feedback as the user/project sponsor. (Save the code as MiniCalculator.java; compile the file: javac MiniCalculator.java; and then run the program: java MiniCalculator).

HINTs: May give feedback to the data type and operations handled by the program, the way the program to get numbers and operators, the way the calculation results are output, the termination of the program and the way the program handles wrong data types and other exceptions.

import java.util.Scanner;

class DataAndOperator {

int operand1;

int operand2;

int result;

String operator;

}

public class MiniCalculator {

public static void main(String[] args) {

    // Scan for keyboard input.

    Scanner input = new Scanner(System.in);

    // operands and operator

    DataAndOperator dataAndOperator = new DataAndOperator();

    // Loop until the user enters zeros for both numbers.

    do {

         System.out.println("(Enter zeros for both operands to exit)");

         GetOperands(input, dataAndOperator);

         if ((dataAndOperator.operand1 != 0) || (dataAndOperator.operand2 != 0))

         {

           System.out.print("Enter the operator(+, -, *, / or %): ");

           dataAndOperator.operator = input.next();

// Print the result of the calculations.

if (DoMath(dataAndOperator)) {

    System.out.println(dataAndOperator.operand1 + " "

    + dataAndOperator.operator.charAt(0) + " "

    + dataAndOperator.operand2 + " = " + dataAndOperator.result);

} else

          System.out.println("Calculation not performed!");

}while ((dataAndOperator.operand1 != 0) || (dataAndOperator.operand2 != 0));

}

System.out.println("Exiting...");

}

private static void GetOperands(Scanner _input, DataAndOperator _dataAndOperator) {

while (true) { //until get the right data type input System.out.print("Enter first number: ");
try {

             _dataAndOperator.operand1 = _input.nextInt();

break; }

       catch (Exception e) {

             _input.nextLine();

             System.out.println("Wrong data typ. Please enter an integer.");

} }

while (true) { //until get the right data type input System.out.print("Enter second number: ");
try {

             _dataAndOperator.operand2 = _input.nextInt();

break; }

       catch (Exception e) {

             _input.nextLine();

} }

}

System.out.println("Wrong data typ. Please enter an integer.");

// Switch statement to decide what operation to do based on the operand.

private static boolean DoMath (DataAndOperator _dataAndOperator) {

       boolean success=true;

       char _operator=_dataAndOperator.operator.charAt(0);

       switch (_operator) {

       case '+':

             _dataAndOperator.result= _dataAndOperator.operand1 +

             _dataAndOperator.operand2;

             break;

       case '-':

             _dataAndOperator.result= _dataAndOperator.operand1 -

             _dataAndOperator.operand2;

             break;

       case '*':

             _dataAndOperator.result= _dataAndOperator.operand1 *

             _dataAndOperator.operand2;

             break;

       case '/':

             try {

_dataAndOperator.result= _dataAndOperator.operand1 /
_dataAndOperator.operand2;
}
catch (Exception e) {
success=false;
System.out.println("Cannot be divided by 0.");

}

       break;

case '%':

try {
       _dataAndOperator.result= _dataAndOperator.operand1 %
       _dataAndOperator.operand2;
}
catch (Exception e) {
success=false;
System.out.println("Cannot be modulared by 0.");

}

       break;

default:

System.out.println("Please select one operator from { +, -, *, /, % }.");
       success=false;
}
return success;

} }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You.

Given code will provide the result back after applying the following operators

+, -, *, / or %.

operand will be integers.

a sample execution is as below:

(Enter zeros for both operands to exit) 1 2 Enter the operator (+, -, *, / or %): + 1 + 2 = 3

on proving operands other than integer it will show error and prompt the user to enter numbers again.

(Enter zeros for both operands to exit) 11.11 Wrong data typ. Please enter an integer.

On entering the wrong operator it will show error and asks the user to enter operand again.

(Enter zeros for both operands to exit) 21 3 Enter the operator (+,-, *, / or %): f Please select one operator from { +, -, *

Arithmetic exceptions also handled such as divide b zero

(Enter zeros for both operands to exit) 3 10 Enter the operator (+,-, *, , or $): / Cannot be divided by 0.

The program ends when both operands are zero

(Enter zeros for both operands to exit) 00 00 Exiting...

Add a comment
Know the answer?
Add Answer to:
Hi. This is a prototype of Java. The following Java program was developed for prototyping a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • My Question is: I have to modify this program, even a small modification is fine. Can...

    My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...

  • IN JAVA: Write a program that displays a menu as shown in the sample run. You...

    IN JAVA: Write a program that displays a menu as shown in the sample run. You can enter 1, 2, 3, or 4 for choosing an addition, subtraction, multiplication, or division test. After a test is finished, the menu is redisplayed. You may choose another test or enter 5 to exit the system. Each test generates two random single-digit numbers to form a question for addition, subtraction, multiplication, or division. For a subtraction such as number1 – number2, number1 is...

  • Looking for some simple descriptive pseudocode for this short Java program (example italicized in bold directly...

    Looking for some simple descriptive pseudocode for this short Java program (example italicized in bold directly below): //Create public class count public class Count {     public static void main(String args[])     {         int n = getInt("Please enter an integer value greater than or equal to 0");                System.out.println("Should count down to 1");         countDown(n);                System.out.println();         System.out.println("Should count up from 1");         countUp(n);     }            private static void countUp(int n)     {...

  • Breaking it Down Problem One problem that I find in new Java programmers is a tendency...

    Breaking it Down Problem One problem that I find in new Java programmers is a tendency to put too much code in "main()". It is important to learn how to break a program down into methods and classes. To give you practice at this, I have a program that has too much code in main. Your job will be to restructure the code into appropriate methods to make it more readable. I will provide you with lots of coaching on...

  • Modify the program that you wrote for the last exercise in a file named Baseball9.java that...

    Modify the program that you wrote for the last exercise in a file named Baseball9.java that uses the Player class stored within an array. The program should read data from the file baseball.txt for input. The Player class should once again be stored in a file named Player.java, however Baseball9.java is the only file that you need to modify for this assignment. Once all of the input data from the file is stored in the array, code and invoke a...

  • I have the following java-program, that creates a random walk starting at (0,0) and continuing until...

    I have the following java-program, that creates a random walk starting at (0,0) and continuing until x or y is greater than abs(n). First: I get an error, when I try closing the Scanner by inserting "reader.close(); " in line 28. It says "Unreachable code". How can I fix that? Second: Is it possible to create a different colour for the StdDraw.point when it reaches a point on one of the edges "n+1" or "n-1" ? 1 import java.util.*; 3...

  • Java Im doing a binary search on an array and need to allow as many queries...

    Java Im doing a binary search on an array and need to allow as many queries as possible and cannot figure out how to. The output should read something like this. Enter a number. 3 3 is a prime number. Enter another number. 4 4 is not a prime number. Enter another number. 8 Current file not large enough for 8. Enter another number. -1 Bye. My code so far looks like this. public static void main(String[] args)    {...

  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

  • The Calculator Class Create an application dlass called Colcustor with the following members Methods Retuns firtNumber...

    The Calculator Class Create an application dlass called Colcustor with the following members Methods Retuns firtNumber secondumber static void mainsering Asks for two integer undan uiet", ..",ЛТеп shows the result according to Note Use the attached example program and add required methods. Then une a switch tructure in the try black to call proper method according to the input operator Sample Output 1 : 2074 Sample Output 2 D1VLdeBy LerOWLthEkceptionHahaling-Jav / Handling ArithmeticExceptions and InputMismatchExceptions. import java.util.InputMismatchException; import java.util.Scanner; public...

  • Can you help me rearrange my code by incorporating switch I'm not really sure how to...

    Can you help me rearrange my code by incorporating switch I'm not really sure how to do it and it makes the code look cleaner. Can you help me do it but still give the same output? Also kindly add an in-line comment on what changes and rearrangement you've done so I can understand what's going on. import java.util.ArrayList; import java.util.Scanner; public class Bank { /** * Add and read bank information to the user. * @param arg A string,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT