CS 102 Alternate Assignment
This assignment is a walkthrough of lab2 to help those of you who had trouble with that lab to understand it better before you get too far behind. You will need internet access and a connection to Eagle for this assignment.
Login to eagle
start tcsh (type tcsh at the command line.)
Make a directory called Lab2-rev
move into the newly created directory.
copy the lab2 skeleton file into the new directory (if you put the skeleton in your home directory last time you can use the following command)
cp ../lab2Skeleton.zip .
Which means copy the lab2Skeleton.zip file from the parent directory to the current directory
now unzip the file
move into the lab2 directory that you just created
Look at the lab2 assignment. You need to first implement three has-a and 1 knows-a relationships in the LavaLamp class. Both the knows-a and has-a are implemented in two parts. The first part of each is to have an instance variable for the object that is had or known. Open LavaLamp.h and you will see that the instance variables are there already. So now open LavaLamp.cpp (using jed or your favorite editore) And look at the constructor.
Remember that for a knows-a relationship, you have to initialize the instance variable with a value that is passed in as a parameter to the constructor. There is only one parameter, so where it says //put your code here finish implementing the knows-a by doing:
_keyMan = keyMan;
Remember that to finish implementing the has-a relationship, you need to initialialize the instance variables with new objects. The instance variables are pointers, so we will initialize them with a pointer to a new object. So add the following three lines:
_top = new LampTop();
_base = new LampBase();
_jar = new LampJar('J'); //use whatever your first initial is instead of 'J' here.
to create three new objects and return pointers to those objects.
Now look at the call to the member function ncursesRun in your constructor. You will see a comment about needed to add the correct parameter. Look at the UML on the assignment. The parameter needs to be something of type LampJar*, Look at the header file for the LavaLamp.h. What variable that is of type LampJar*? So use that variable.
Your new function call line should look like:
Now save your file, and compile project using make all. You should have no compile errors if you have followed directions to this point. You should have an executable called lab2. Try running ./lab2 It runs quickly and doesn't really do much.
So look back at the assignment. The next thing that you ought to do, is create the lava lamp in the main function.
So open LavaLamp.cpp again and go all the way to the bottom of the file and look at the main function which has a comment that says: //create your lava lamp (and anything else you need) here
So we need to create a lava lamp. Look at the top of the file at the constructor that you just modified. It needs a parameter. What sort of parameter? A pointer to a KeyManager. Since you don't have a pointer to a KeyManager coming from anywhere else at this point, You will need to make one and then pass that to the LavaLamp Constructor. You can do this in two ways,
the first is similar to the way that you did the LampTop and friends above
KeyManager* keyMan = new KeyManager();
The other way is to create a regular key manager object, and then pass the address of the keymanager to the lavalamp using the address-of '&' operator.
At this point you have finished your code in the LavaLamp class. Save it, compile it and run. You now have the lavalamp showing up, but don't have anything happening.
The last thing to do, is getting the character that represents the bubble to rise when the spacebar is pressed.
The directions say: When the space button is pressed, the method/member function spaceBarPressed in the KeyHandler class is called for you. (you don't need to call it, it happens already) You do however need to implement this method so that it animates a bubble of the appropriate character.
So open KeyHandler.cpp and find the spaceBarPressed method, which is empty except for a comment asking for your code here. What goes here? Examine the UML for the project. It says that the KeyManager uses-a BubbleAnimator. So since there are no other functions in this class. This must be the place to use the bubble animator. So create a bubble animator by putting the following in the member function:
Now you see that according to the UML, Bubble animator only has one useful function – called animateBubble. This method takes a character. In fact it should be the character that will rise, the one selected when the LampJar was created.
There is a lampjar as a parameter in KeyManager::spaceBarPressed. But there is no way to get the private instance variable. (examine lampjar.h to see). Then compare lampjar.h, to the UML. There is one additional function in the UML beyond what is in the LampJar class. An accessor to get the value of the private instance variable _bubbleChar.
So you will need to implement this to make the LampJar class match it's UML
open the lampjar.h file and add the line
to the public section of the class declaration.
Then implement the function in lampjar.cpp
Now you are ready to finish writing KeyManager::spaceBarPressed. Open the file and you now know how to get the character:
char bubble = jar->getBubbleChar();
Now you have the bubble character and the bubble animator, so animate the bubble
Save, compile, (using make all) and run ./lab2
hit the space bar to see the 'bubble'.
As the bubble is rising, use the printscreen key to put a copy of the screen on the clipboard. Open up paint and paste the screenshot into a new document. Save it and sent it to me by email showing the bubble rising with the correct letter as your bubble along with your name an section of cs102.