23.4k views
4 votes
Java code - Write a class called Calculator, which performs basic arithmetic operations. The class

should have two public methods for processing a line of input, containing the operation
and for getting the result of the last operation:
public void Process(String command)
public String GetResult()
The class should have one private data field, which stores the result of the last command.
That field should be retrieved by calling the GetResult() method above. Write a main
program to create an instance of the Calculator class and use it to repeatedly process and
display the result of commands entered by the user. NOTE: Your program could stop
when the user enters a special value or an empty command line.

Think carefully about the code in your programs – variable names, comments, etc.
Programs must be fully commented – add a comment in the beginning describing what
your program does and how! Also add any other comments where necessary

User Thomasine
by
8.7k points

1 Answer

5 votes

Final answer:

To solve this problem, you need to create a class called Calculator with two public methods: Process and GetResult. The Process method takes a string command as input and performs the arithmetic operation specified in the command. The GetResult method returns the result of the last operation.

Step-by-step explanation:

To solve this problem, you need to create a class called Calculator with two public methods: Process and GetResult. The Process method takes a string command as input and performs the arithmetic operation specified in the command. The GetResult method returns the result of the last operation. The class should have a private data field to store the result of the last command, which can be accessed using the GetResult method. In the main program, you can create an instance of the Calculator class and use it to repeatedly process and display the result of commands entered by the user. The program can stop when the user enters a special value or an empty command line.

User Kishan Chauhan
by
8.6k points