Question

In: Computer Science

Create a program named CmdLineCalc.java that works like a simple calculator. You'll run this program from...

Create a program named CmdLineCalc.java that works like a simple calculator. You'll run this program from the command line:

$ java CmdLineCalc 1 + 2
3.0

$ java CmdLineCalc 1 - 2.5
-1.5

$ java CmdLineCalc 3 + 4 - 5
2.0

$ java CmdLineCalc 6.5 - 7 + 8
7.5

To keep it simple, your program only needs to support addition (+) and subtraction (-). You may assume that, starting with the first argument, every other argument will be a number. The arguments at odd indexes in the array will be operators. There is no limit to the number of arguments, that is, until the computer runs out of memory. The program should display the answer rounded to one decimal place.

There are three types of errors you need to handle: 1) the user might forget to give you command-line arguments, 2) the expression itself might be incomplete, and 3) the user might use operators you don't support. Here is what you should output in those cases:

$ java CmdLineCalc
Missing expression

$ java CmdLineCalc 1 +
Invalid expression

$ java CmdLineCalc 2 % 3
Invalid operator: %

When an error occurs, display the appropriate message and call System.exit() to terminate the program. Use a status of 1 to indicate that an error occurred. (By default, programs exit with a status of 0, meaning success.)

Any answers will receive a like.

Solutions

Expert Solution

public class CmdLineCalc {

   public static void main(String[] args) {
      
       if(args.length==0) {
           System.out.println("Missing expression");
           System.exit(1);
       }
       double result = 0.0;
       try {
       result = Double.parseDouble(args[0]);
       for(int i=1;i<args.length;) {
           if(args[i].equals("+")) {
               result += Double.parseDouble(args[i+1]);
               i=i+2; //used + and next number also
           }
           else if(args[i].equals("-")) {
               result -= Double.parseDouble(args[i+1]);
               i=i+2; //used - and next number also
           }
           else {
               System.out.println("Invalid operator: "+args[i]);
               System.exit(1);
           }
          
       }
       }catch(Exception e) {
           System.out.println("Invalid expression");
           System.exit(1);
          
       }      
       System.out.printf("%.1f", result);
   }

}


Related Solutions

Create a program using python that provides a simple calculator: Requires a login with a prompt...
Create a program using python that provides a simple calculator: Requires a login with a prompt for a username and a password prior to using the calculator If username and password are incorrect, the program should re-prompt to re-enter correct username and password Once logged in, the user should have access to the calculator and should have the ability for the following mathematical operators: Addition Subtraction Multiplication Division Square Root PI Exponents
n this task, you'll be asked to create a simple for-loop to loop over a simple...
n this task, you'll be asked to create a simple for-loop to loop over a simple data construct, in this case, to provide the maximum, minimum, and average length of words in a speech performing a lexicographical analysis not unlike what's used to measure reading level. Specifications Keep working on the same notebook Create a function named lexicographics() that takes one parameter: to_analyze, a required string Using a single for loop, calculate the following for your text: The maximum number...
In this program, you'll create a program that utilizes an enumerated data type to manipulate the...
In this program, you'll create a program that utilizes an enumerated data type to manipulate the array.   Here are the requirements: Write a program that contains an enumerated data type named Letters.    In the declaration, include the following three enumerators: ALPHA, BETA, DELTA. Then, create an array of integers three elements long. The array should be initialized to 0 using a 1-element initialization list. Instead of using integers as subscripts, use the enumerators from your enumerated data type to assign...
11.1 Simple Arithmetic Program Using the instructions from Week 1 Lab, create a new folder named...
11.1 Simple Arithmetic Program Using the instructions from Week 1 Lab, create a new folder named Project01. In this folder create a new class named Project01. This class must be in the default package. Make sure that in the comments at the top of the Java program you put your name and today's date using the format for Java comments given in the Week 1 Lab. For this lab, you will write a Java program to prompt the user to...
Complete these steps to write a simple calculator program: 1. Create a calc project directory 2....
Complete these steps to write a simple calculator program: 1. Create a calc project directory 2. Create a mcalc.c source file containing five functions; add, sub, mul, div, and mod; that implement integer addition, subtraction, multiplication, division, and modulo (remainder of division) respectively. Each function should take two integer arguments and return an integer result. 3. Create a mcalc.h header file that contains function prototypes for each of the five functions implemented in mcalc.c. 4. Create a calc.c source file...
Process: In this lab, you will be implementing a simple “Bop-it!”-like game. Your program will run...
Process: In this lab, you will be implementing a simple “Bop-it!”-like game. Your program will run for a single game. Start with implementing a “start menu”, output a line asking the user to push a keyboard key to start and wait for the user to push a key. The game will involve printing a line telling the user which key to press (chosen randomly by the program) and will wait a certain time for a response. After each successful action...
Introduction to Java Programing Using Loop Create a simple calculator program using loop Ask user to...
Introduction to Java Programing Using Loop Create a simple calculator program using loop Ask user to input two numbers using scanner class Print the instruction of the menu for the calculator program Ask user to press 0 to Quit Ask user to press 1 to Add Ask user to press 2 to Substract Ask user to press 3 to Multiply Ask user to press 4 to Divide Perform correct calcuation based on user inputs and print the result Print error...
C -Language Create a simple calculator that performs addition, subtraction, multiplication, and division. Your program should...
C -Language Create a simple calculator that performs addition, subtraction, multiplication, and division. Your program should prompt the user for the operation they wish to perform followed by the numbers they wish to operate on. You should have a function for each operation and use branches to determine which function to call. I need this to make any integers given, into decimal numbers, such as 3 to 3.0, or 2 to 2.0, also, so that I can multiply or add...
In this project you will create a basic console based calculator program. The calculator can operate...
In this project you will create a basic console based calculator program. The calculator can operate in two modes: Standard and Scientific modes. The Standard mode will allow the user to perform the following operations: (+, -, *, /) add, subtract, multiply, and divide The Scientific mode will allow the user to perform the same functionality as the Standard add, subtract, multiply, and divide (+, -, *, / ) plus the following: sin x, cos x, tan x. (sin x,...
Create a program that will ask the user to choose their order from a simple menu....
Create a program that will ask the user to choose their order from a simple menu. Customers first choose from three types of dishes: Sandwiches/wraps, Rice Meals, or Noodles. They can type in 1, 2, or 3 to select the type then, they choose a specific dish as shown below. Confirm their order by displaying it on the screen after they make their selection. 1. Sandwiches/wraps Hamburger Chicken shawarma 2. Rice meals Arroz con pollo Chana masala 3. Noodles Chow...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT