Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Below is the code I wrote in java but it shows error when outputting, please tell me the complete modified code, I really need your

image text in transcribedimage text in transcribedBelow is the code I wrote in java but it shows error when outputting, please tell me the complete modified code, I really need your help I have tried for many hours. Thanks!

import java.util.Scanner;

public class Calculator {

private static int add(int n, int m) { return n + m; }

private static double add(double n, double m) { return n + m; }

private static double add(int n, double m) { return n + m; }

private static double add(double n, int m) { return n + m; }

private static int subtract(int n, int m) { return n - m; }

private static double subtract(double n, double m) { return n - m; }

private static double subtract(int n, double m) { return n - m; }

private static double subtract(double n, int m) { return n - m; }

private static int multiply(int n, int m) { return n * m; }

private static double multiply(double n, double m) { return n * m; }

private static double multiply(int n, double m) { return n * m; }

private static double multiply(double n, int m) { return n * m; }

private static int divide(int n, int m) { return n / m; }

private static double divide(double n, double m) { return n / m; }

private static double divide(int n, double m) { return n / m; }

private static double divide(double n, int m) { return n / m; }

private static int modulo(int n, int m) { return n % m; }

public static void main(String[] args) { int n = 0; int m = 0; double n2 = 0; double m2 = 0;

Scanner sc = new Scanner(System.in); while (true) { //user enter System.out.println(">>"); String input = sc.nextLine(); String[] inputArray = input.split(" "); String operator = inputArray[0];

if (operator.equals("q")) { System.out.println("exit"); break; } else { if (!inputArray[1].contains(".")) { n = Integer.parseInt(inputArray[1]); } else { n2 = Double.parseDouble(inputArray[1]); }

if (!inputArray[2].contains(".")) { m = Integer.parseInt(inputArray[2]); } else { m2 = Double.parseDouble(inputArray[2]); }

switch (operator) { //add numbers case "+": if (n == 0 && m == 0) { System.out.printf("%.2f ", add(n2, m2)); } else if (n == 0 && m != 0) { System.out.printf("%.2f ", add(n2, m)); } else if (n != 0 && m == 0) { System.out.printf("%.2f ", add(n, m2)); } else if (n != 0 && m != 0) { System.out.println(add(n, m)); } break; //digital subtraction case "-": if (n == 0 && m == 0) { System.out.printf("%.2f ", subtract(n2, m2)); } else if (n == 0 && m != 0) { System.out.printf("%.2f ", subtract(n2, m)); } else if (n != 0 && m == 0);

//multiply numbers case "*": if (n == 0 && m == 0) { System.out.printf("%.2f ", multiply(n2, m2)); } else if (n == 0 && m != 0) { System.out.printf("%.2f ", multiply(n2, m)); } else if (n != 0 && m == 0) { System.out.printf("%.2f ", multiply(n, m2)); } else if (n != 0 && m != 0) { System.out.println(multiply(n, m)); } break; //Divide numbers case "/":

if (n == 0 && m == 0) {

if (m2 == 0) { System.out.println("Error."); } else { System.out.printf("%.2f ", divide(n2, m2)); } } else if (n == 0 && m != 0) { if (m == 0) { System.out.println("Error."); } else { System.out.printf("%.2f ", divide(n2, m)); } } else if (n != 0 && m == 0) { if (m2 == 0) { System.out.println("Error."); } else { System.out.printf("%.2f ", divide(n, m2)); } } else if (n != 0 && m != 0) { if (m == 0) { System.out.println("Error."); } else { System.out.println(divide(n, m)); } } break; } }

} } }

ASSIGNMENT 1. The program will accept user input that is one line long and is in one of the accepted formats. The code will then print out the appropriate mathematical answer or an error message. 2. The program will continue looping in a sentinel-controlled loop until the user enters ' q ', at which point the program will break out of the loop. 3. The user is prompted to enter an expression by the prompt ">>" 4. The user will enter one line of input of the correct form. Note, there is no need for validation here. Assume that the form is correct. 5. Using the methods of the Scanner class, determine whether to exit or calculate a value. 6. Each operation will be handled in one of sixteen overloaded static method. The method signatures, using UML conventions are: - add(n : integer, m : integer ) integer - add(n : double, m : double ) : double - add ( n : integer, m : double): double - add(n: double, m : integer) : double - subtract( n : integer, m : integer) : integer - subtract (n : double, m : double ): double - subtract( n : integer, m : double ): double - subtract( n : double, m : integer ) : double - multiply ( n : integer, m : integer ) : integer - multiply( n : double, m : double ): double - multiply ( n : integer, m : double ): double - multiply( n : double, m : integer ) : double - divide( n : integer, m : integer ) : integer - divide( n : double, m : double ): double - divide( n : integer, m : double ): double - divide( n : double, m : integer ) : double 7. Notes: 1. If you read the first token on the input, and it is the single-character string " q ", there is no need (or ability!) to check the rest of the line. stop now, before trying to read the rest of the non-existent line. 2. If the first token is NOT " q "' then read the next two tokens as strings and convert to the appropriate data type 3. Use the valueOf methods of classes Integer and Double to transform a String value to a numeric one, when needed. 1. The determination of using Integer or Double is strictly determined by the presence of the ". (the decimal point). If there is no decimal point in the string, use Integer. If there is a decimal point in the sting, use Double. 4. This looks like a lot, but there's a lot of repetition, and a copy and modify strategy will help. Also, each of the sixteen methods should be one-liners 5. There must be a space on either side of the operator like the line below, or you will ruin your week. It is hard to parse the line, otherwise. 6. Integer division truncates and returns an integer 7. Dividing by zero produces an output of "Error." 5+2 7 8.0/2 4.00 >8/ Error. >8/3 2 >8.0/3.0 2.67 >520.0 100.00 >q exit

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

=+impact member states and MNEs?

Answered: 1 week ago

Question

How do modern Dashboards differ from earlier implementations?

Answered: 1 week ago