CS610 – LAB 4 We will begin a “ClunkyCalculator” class that will mimic a simple calculator using BlueJ’s ability to manually call methods. We will do half of the Calculator class in this lab and finish it in the next lab. To begin, make a Lab4 folder in your CS610 folder. The ClunkyCalculator The purpose of this project is to make some methods that use the basic mathematical and relational operators, practice using local variables, and gain more experience with the conditional if-else statement. One design of computer CPU’s is known as an accumulator machine (or a one-address machine). This type of CPU has a special register called the accumulator. Any arithmetic operation, such as adding two values, uses the contents of the accumulator as one of the operands and the result of the operation is stored back in the accumulator. If you are interested, the CPU that was in the original Apple II computer—the MOS Technology 6502 processor—was an accumulator machine. This is the type of calculator we are going to create in this project. Using BlueJ, create a new project in your Lab4 folder, and call the project clunky-calculator. Then create a new class, which will be called Calculator. This class will have a single field (class instance variable), which is an int and is called accumulator. The Constructor This class has one constructor. The constructor takes no parameters, and it simply assigns accumulator an initial value of zero. The Method clear This is a mutator method that has two statements in its body. The first statement assigns the accumulator a zero and then the second statement is similar to: System.out.println( “Accumulator = ” + accumulator ); The Method loadAccumulator This is another mutator method, but one that takes a parameter. Its actions are to assign the accumulator the value of its parameter, and then it prints the contents of the accumulator to the terminal window using a similar statement to that in the previous method. The Arithmetic Methods CS610 Page 1 of 4 Lab 4 Next, we will create five methods to perform the arithmetic operations of add ( + ), subtract ( - ), multiply ( * ), divide ( / ), and mod ( % ). These methods will be quite similar, so you might find it easier to write the first method, then use copy and paste for the other methods because there will only be slight changes. The Method add This method is a void method, and it takes a single parameter. In the body of the method, a local variable is declared and this variable is assigned the value of accumulator. The reason for this local variable is that the value of accumulator will be changed, but we want to record its value before the change so that it can be printed out. After the contents of accumulator is saved in the local variable, add accumulator with the parameter and assign the result back to accumulator. Then a statement is printed to the terminal window that shows the original value of the accumulator (saved in the local variable), a plus sign, the value of the parameter, an equal sign, and the new value of the accumulator. Thus, if the accumulator originally contained the value 3 and the add method was called with a parameter of 5, the statement that appears in the terminal window should be: 3+5=8 The other 4 methods are similar to the add method. The difference, of course, is the operation each method performs, and the operator that appears in the statement printed to the terminal window. For example, I started with an accumulator containing 0. Then I called add with a parameter of 20, subtract with a parameter of 5, multiply with a parameter of 3, divide with a parameter of 9, and mod with a parameter of 2. Note that I am naming each method with the name of the operation it is performing. My terminal window then contained the following. Notice that in the display the starting value of the accumulator is always the leftmost digit, the parameter to the method is the next digit, and the resulting value of the accumulator is the rightmost digit. The Method compute Now we will add one more method. This method is called compute. It is a void method and takes no parameters. Its purpose is to allow us to test our calculator by placing a sequence of method calls inside this method, compile our class, create an object of the class, and call the compute method. CS610 Page 2 of 4 Lab 4 The Final Test When all your methods are working correctly, place the following sequence of method calls in your compute method. loadAccumulator( 15 ); add( 20 ); subtract( 30 ); multiply( 8 ); divide( 4 ); mod( 4 ); subtract( 5 ); clear(); Now, compile your class, create an object of the class, and call the compute method. If you get the following terminal window: Show this to your lab instructor, give her/him a printout of your class, and you are done for this lab. Next we will add more methods to this class. The methods that we will add will implement the relational operators. Note, as an aid, I have attached a partial copy of my documentation for this class. CS610 Page 3 of 4 Lab 4 Note: This lab was originally written by Dr. Broeg. CS610 Page 4 of 4 Lab 4