Lab 4
Lab assignment: Methods and Calculator
In this week's lab you must write a Java program to simulate a simple calculator.
The execution of the program should look like the following:
Welcome to the calculator. Enter two integers, then the operator.
Entering 'x' for the operator exits the program.
Enter the two integers to be used: 7 5
Enter the operator: -
7 - 5 = 2
Enter the two integers to be used: 1 99
Enter the operator: x
Exiting program...
You will write several methods, to read in the numbers on which the calculations will be done, and to do the calculations, all called from one method. Your answer should be stored in a global variable.
Stage 1 (1 point):
Finish the mainLoop() method so that when executed the program works with the four operations (+, -, *, /). You must write both the code that defines the methods (outside mainLoop) and the code that calls them (inside mainLoop).
Stage 2 (1 point):
Add one more operation to your calculator: exponentiation, that is the first number to the power of the second number. Assume the two numbers are integers for the exponentiation.
Stage 3 (1 point):
Have the program do error control. For instance, if the user types a character different from one of the 4 (or 5 if you are doing stage 2) operations or dffferent from x (to exit), the program should print a mistake message on the screen and read again in input.
Extra Credit (1 point): After the first calculation, prompt the user to use the current answer to do the next calculation, or to clear it and let him enter two new numbers.
Tips:
Read carefully the comments in the file. There are several methods that are being called in the mainLoop() method (e.g., methods for reading input). You must implement these methods (i.e. write their code). Other methods are not called, but you must implement them anyway (e.g., the methods for the sum, difference and division have not been called, nor written). When executed, the program must work with all the operations.
Submission:
1. You should work with a partner for a grade. Only one of you need to submit the program to Blackboard. But both of your names should be shown in the program you submit.
2. You should turn in to Blackboard by the end of the lab (8:50 for the 8-9 lab session, 9:50 for the 9-10 lab session), at which point the assignment for the lab is disabled.
/**
* * @author rigel */
public class Calculator { double theAnswer; /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Calculator calc = new Calculator(); calc.mainLoop(); } private void mainLoop() { System.out.println("Welcome to the calculator. Enter two numbers, then an operator " + "to be used. \n Entering an operator of x exits the program."); while(true) { //this is a loop that keeps repeating as long as no break; is executed System.out.println(); System.out.println("Enter the two integers to be used: "); //declaring local variables to store operands double operand1 = 0; double operand2 = 0; //declaring a local variable to store the operator char operator=' '; //the method readOneOperand() has not been written //you must write the code for this method. This method //should just read a number typed on the keyboard, and return it in output //so that it is stored in the variable. This method should do //nothing else, just read a number in input and return it. operand1 = readOneOperand(); operand2 = readOneOperand(); //at this point operand1 and operand2 contain two numbers read in input. //Now must read operator. The method readOperator() has not been written. //You must write the code for this method. This method should just read a character //typed on the keyboard and return it in output, so that it is stored in the variable //operator. This method should do nothing else. operator = readOperator(); //now there are two numbers in operand1 and operand2 //and a character in operator. The character is one of +, -, *, /, x if(operator == '*') //if this is a multiplication theAnswer = multiply(operand1,operand2);//you must write the code for the method multiply //then here write the code for the other operations and for the exit. //Remember to write the methods for the other operations and also to print out the answer. } } private char readOperator() { throw new UnsupportedOperationException("Not yet implemented"); } private double readOneOperand() { throw new UnsupportedOperationException("Not yet implemented"); } private double multiply(double operand1, double operand2) { throw new UnsupportedOperationException("Not yet implemented"); } }