CS 161 LAB 5 This lab will have two parts. In the first part, we will create a class to automate the drawing of the robot from the second lab. For the second part, 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 this week and finish it as part of next week s lab. To begin, make a Lab5 folder in your CS161 folder. The two projects you make will go in this folder. Part I As part of the second lab, you were to save the final settings for the robot picture. Find those settings as we will have the computer draw the robot for us. From your Lab2 folder, copy the shapes2 project and paste it into your Lab5 folder. Now open this project in BlueJ. The Picture class Once BlueJ has open this project, select the new class option. Call this class Picture, and when the editor opens with the skeleton class, put your name and the date in the appropriate locations and change the documentation at the beginning of the class to state that it will automatically draw the robot picture from the second lab. The class instance variables Since this class will draw the robot picture, it needs a class instance variable for each of the components. This means the class must have two objects of type Square, three objects of type Rect, two objects of type Triangle, and two objects of type Circle. Rather that naming the objects square1, square2, etc, as BlueJ does, name each object for its role in the picture: Name one of the Square objects head and the other body; name the Rect objects neck, leftarm, and rightarm; name the Triangle objects leftleg and rightleg; and the Circle objects leftwheel and rightwheel. The Constructor The constructor for this class is not very complex. It needs to initialize the nine objects using the new operator. So, for example, one line in the constructor is the following: head = new Square(); Remember that the types of these objects have already been declared at the beginning of the class. DO NOT put the type before the object name; i.e. do NOT write: Square head = new Square(); If you do, you will make the object head a local variable. This means that the class instance variable head would be undefined outside of the constructor. CS 161 Page 1 of 8 Lab 5
CS 161 Page 2 of 8 Lab 5
The Method drawhead We will make a separate method to draw each of the parts of the robot. While this means there will be nine methods, plus a tenth method that will put it all together, each method will be rather short, simple, and will perform only one action. The drawhead method is public, void, and takes no parameters. Be sure to add the appropriate documentation to the beginning of this method that explains what action the method performs. The first action it takes is to make head visible. If you have forgotten the names of the methods in the Square class, you might want to open the editor for the Square class and switch to the Documentation view. For example, a portion of the Documentation view of the Square class looks like the following: This not only tells you the names of the methods in each class, but what information, if any, you must give to a method to have it perform its actions. Also remember that each method call is an external method call. It must be prefixed with an object name using the dot notation. So, to make head visible, the statement is: head.makevisible(); After head is made visible, it must be resized. Call the appropriate method, passing in the size that you saved from Lab2. CS 161 Page 3 of 8 Lab 5
Next, head must be moved to the proper X-Y location. To move the objects to their proper location, use the methods slowmovehorizontal and slowmovevertical. To use these methods, however, you will need to do some calculations. The various methods to move an object in the Square class add their parameter to either the current X-coordinate or the current Y- coordinate. What you most likely saved from Lab2 was the final X and Y-coordinates. To get the proper value to pass to the move methods, you must use the editor to look at the code for the Square class to find the initial X and Y positions and subtract these numbers from the final X and Y positions. It is the difference between the starting and ending coordinates that represents the distance each object must move, and it is this distance that must be passed to move methods. Of course, if you really wanted to, there is no reason why you could not add accessor methods for the X and Y coordinates to the various classes in this project. Then, using the accessor methods, you could get the initial positions and use subtraction statements with your final positions, saving the results in local variables. These variables could be used as actual parameters in the methods that move the objects. This is just a thought, and it is certainly not required for this lab. To review, the steps the drawhead method as well as the other drawing methods must take are the following. 1. Make the object visible. 2. Resize the object to the size you saved from Lab2. 3. Move the object horizontally and vertically to the coordinates that you saved from Lab2. Once you have completed the drawhead method, compile the Picture class, make an object of the class and call the drawhead method. Does it work correctly? If not, fix it before going on to another method. The Other draw Methods There are eight other draw methods to create, one for each part of the robot s body. Call these methods drawbody, drawneck, drawrightarm, and so on. Each of these methods follows the same pattern as the drawhead method. After you finish each method, test the method and correct any errors in the method before you start the next method. In addition, add the appropriate documentation at the beginning of each method. The makepicture method The last method to add is a makepicture method. This method just contains nine statements. Each statement is an internal method call to one of the draw methods, for example: drawhead(); Thus, calling the makepicture method will cause the robot to be drawn. CS 161 Page 4 of 8 Lab 5
When the makepicture method works correctly and draws the robot picture, make a printout of the class. Be sure that each method has the appropriate documentation. Then demonstrate the makepicture method for your lab instructor. Part II: The ClunkyCalculator Now for the second project. 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 Lab5 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 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 CS 161 Page 5 of 8 Lab 5
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. The Final Test CS 161 Page 6 of 8 Lab 5
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 a printout of your class, and you are done for this week. Next week 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. CS 161 Page 7 of 8 Lab 5
Note: This lab was originally written by Dr. Broeg. CS 161 Page 8 of 8 Lab 5