1h 50m left Language Java 8 Autocomplete Ready ? 3. Calculator ALL = The following program implements a basic calculator
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
1h 50m left Language Java 8 Autocomplete Ready ? 3. Calculator ALL = The following program implements a basic calculator
1h 50m left Language Java 8 Autocomplete Ready ? 3. Calculator ALL = The following program implements a basic calculator with three string parameters that contain two values and a calculation operator The calculator method must read the three parameters and perform the calculation based on the supplied calculation operator. The calculation operators are "Add" "Subtract" "Multiply" and "Divide" The implementation does not work and fails with an error Your goal is to find and repair the defects in the Calc method. 1 Hints 2 3 1. Parameters value and value2 may contain non-numero values in these cases, set the ErrorMessage variable to "VALUES MUST BE NUMERIC" 2. As the calculation operator is passed in as a string, it can be set to anything. Should this be the case, set the Error Message to "INCORRECT OPERATOR 4 1 > import java.io.*; 14 15 class Result 16 public static string ErrorMessage = ""; 17 public static Double Calc(String valuel, String value2, String calcOperator) 18 decimal vi = decimal.Parse(valuel); 19 decimal v2 = decimal.Parse(valuel); 20 decimal returnValue = 0; 21 22 switch (calcOperator) 23 { 24 case "add" : 25 returnValue = v1 + v2; 26 break; 27 case "Subtract": 28 returnValuee = vi - V2: 29 break; 30 case "Multiply": 31 returnValue = v1 * V2; 32 break; 33 case "Divide": 34 returnValue = vi / v2; 23 35 36 1 37 return result; 38 } 39 1 40 v public class Solution I 41 public static void main(String[] args) throws IOException { 42 Buffered Reader bufferedReader = new BufferedReader(new InputStreamReader (System.in)); 43 BufferedWriter bufferedWriter = new BufferedWriter (new FileWriter(System.getenv ("OUTPUT_PATH")); 44 45 String valuel = bufferedReader.readLine(); 46 47 String value2 = bufferedReader.readLine(): 48 49 String calcOperator = bufferedReader.readLine(); 50 51 double result = Result.Calc(valuel, value2, calcOperator); 52 break; 5 3. Remember to cater for basic and standard mathematical rules in relation to the effect of each operator on two values in the case of an arithmetic rule violation, set the ErrorMessage to "ARITHMETIC ERROR" 6 7 8 9 Line: 14 Col: 1
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!