Question

Write a scripting interface that accepts the following commands:    The program must run a continuous...

Write a scripting interface that accepts the following commands:
   The program must run a continuous loop and not shut down after
   each operation -- that is, continuously asks for input until "exit"
   is typed.

Specification:
   Run as: java Calc
   This throws up a prompt which accepts the following syntax:
    PROMPT>> <number1> + <number2>
    PROMPT>> <number1> - <number2>
    PROMPT>> <number1> * <number2>
    PROMPT>> <number1> / <number2>
    PROMPT>> <number1> % <number2>

    example:

    PROMPT>> 3 + 6
      9
    PROMPT>> 4 - 3
      1

   Handle gracefully as many incorrect inputs as you can. For example:
         
    ex. ADDER > 3 $ 6
                         error: No such operator '$'
    
    ex. ADDER > 3 + 2 + 1
                         error: must supply only two numbers
    
    ex. ADDER > a + 2
                         error: symbol 'a' not a valid real

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

Code:

import java.util.Scanner;
public class Main
{
   public static void main(String[] args) {
  
   Scanner sc=new Scanner(System.in);
   while(true){
   System.out.print("PROMPT>>");
   String inp=sc.nextLine();
   if(inp.equals("exit")){
   System.out.println("Thank You");
   break;
   }
   else{
   String []inp1=inp.split(" ");
   if(inp1.length==3){
   if(inp1[1].equals("+" )|| inp1[1].equals("-" )|| inp1[1].equals("*" )|| inp1[1].equals("/" )|| inp1[1].equals("%")){
   float a,b;
   try{
   a=Float.parseFloat(inp1[0]);
   try{
   b=Float.parseFloat(inp1[2]);
   if(inp1[1].equals("+" )){
   System.out.println(a+b);
   }
   else if(inp1[1].equals("-" )){
   System.out.println(a-b);
   }
   else if(inp1[1].equals("*" )){
   System.out.println(a*b);
   }
   else if(inp1[1].equals("/" )){
   System.out.println(a/b);
   }
   else if(inp1[1].equals("%" )){
   System.out.println(a%b);
   }
   }
   catch(Exception e){
   System.out.println(" symbol '"+inp1[2]+"' not a valid real");
   }
   }
   catch(Exception e){
   System.out.println(" symbol '"+inp1[0]+"' not a valid real");
   }
   }
   else{
   System.out.println("No such operator '"+inp1[1]+"'");
   }
   }
   else{
   System.out.println("must supply only two numbers and an operation");
   }
   }
   }
  
   }
}


Output:

Add a comment
Know the answer?
Add Answer to:
Write a scripting interface that accepts the following commands:    The program must run a continuous...
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
  • 1. Specification Write a C program to implement a simple calculator that accepts input in the...

    1. Specification Write a C program to implement a simple calculator that accepts input in the following format and displays the result of the computation: calc [operand_1] [operator] [operand_2] The operands operand_1 and operand_2 are non-negative integers. The operator is one of the following: addition (+), subtraction (-), multiplication (x), division (/) and modulo (%). Note: For the multiplication operator, use letter ‘x’. If you use the asterisk ‘*’, your program will not work properly 2. Implementation • The program...

  • Creating a Shell Interface Using Java This project consists of modifying a Java program so that...

    Creating a Shell Interface Using Java This project consists of modifying a Java program so that it serves as a shell interface that accepts user commands and then executes each command in a separate process external to the Java virtual machine. Overview A shell interface provides the user with a prompt, after which the user enters the next command. The example below illustrates the prompt jsh> and the user’s next command: cat Prog.java. This command displays the file Prog.java on...

  •  Write a Perl script that accepts exactly 2 integer arguments where the first argument must...

     Write a Perl script that accepts exactly 2 integer arguments where the first argument must be less than the second argument. The script will print a comma separated list of integers starting with the first argument up through the second argument.  The last printed value should be the second command line argument not be followed by a comma.  The script should also be able to handle the following errorsituations: o incorrect number of arguments o the first...

  • **This is for the C Language. Trigonometric Calculator Specification You are required to write a program...

    **This is for the C Language. Trigonometric Calculator Specification You are required to write a program that calculates and displays values of the trigonometric functions sine, cosine, and tangent for user-supplied angular values. The basic program must comply with the following specification. 1. When the program is run, it is to display a short welcome message. TRIG: the trigonometric calculator 2. The program shall display a message prompting the user to enter input from the keyboard. Please input request (h-help,...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • PYTHON The provided code in the ATM program is incomplete. Complete the run method of the...

    PYTHON The provided code in the ATM program is incomplete. Complete the run method of the ATM class. The program should display a message that the police will be called after a user has had three successive failures. The program should also shut down the bank when this happens. [comment]: <> (The ATM program allows a user an indefinite number of attempts to log in. Fix the program so that it displays a message that the police will be called...

  • Your task is to develop a large hexadecimal integer calculator that works with hexadecimal integers of...

    Your task is to develop a large hexadecimal integer calculator that works with hexadecimal integers of up to 100 digits (plus a sign). The calculator has a simple user interface, and 10 \variables" (n0, n1, ..., n9) into which hexadecimal integers can be stored. For example a session with your calculator might look like: mac: ./assmt1 > n0=0x2147483647 > n0+0x3 > n0? 0x214748364A > n1=0x1000000000000000000 > n1+n0 > n1? 0x100000000214748364A > n0? 0x214748364A > exit mac: Note: \mac: " is...

  • How do i write the pseudocode for this java code? First, write out pseudocode, and then create a program to help you by accomplishing the following tasks: :  Use command line interface to ask the use...

    How do i write the pseudocode for this java code? First, write out pseudocode, and then create a program to help you by accomplishing the following tasks: :  Use command line interface to ask the user to input the following. ○ How many apples are on hand ○ How many apples should be in stock ○ How many oranges are on hand ○ How many oranges should be in stock  Perform an operation to determine how many of...

  • Edit a C program based on the surface code(which is after the question's instruction.) that will...

    Edit a C program based on the surface code(which is after the question's instruction.) that will implement a customer waiting list that might be used by a restaurant. Use the base code to finish the project. When people want to be seated in the restaurant, they give their name and group size to the host/hostess and then wait until those in front of them have been seated. The program must use a linked list to implement the queue-like data structure....

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