For this assignment, you will work with the concepts covered by Chapter 4 of the text, but you will not work with the Little Crab scenario any longer.
If you don't remember how to do any of the steps (like, copying, zipping, etc), please refer back to Lab 1.
First, you will need to copy over the scenario for this assignment. If you are working from home, you can download the scenario here. If you are in lab, the scenario is located in /eng/class/notes/cse113/scenarios/Lab5.
Take note of the following classes in the scenario:
You are to complete the following functionality for the scenario.
Create a class that is a subclass of DownwardMover named Food. You can select whatever image you would like for your food. In the act method for the Food, insert the code to make the food move.
You have just completed a piece of functionality of your project. STOP coding. Compile all the files in the scenario and fix any compiler errors that are reported. After all the erors are fixed, place a Food object in the world and click Run. Note that the food will move to the bottom edge and then stop. When you feel that this code is working properly, move on to the next step.
Continue to add functionality to the act method of food so that when the food reaches the bottom of the screen, the food is removed from the world. Remember that you will need the keyword "this" to properly remove the food from the world.
Every time you complete a piece of functionality, you need to compile and run your scenario. There is no other way to ensure that the functionality is what you intended it to be. Simply writing code in an actor does not guarantee that it performs the way you intended, so making sure to stop and compile and run often will help to ensure your code is working properly.
Edit the constructor of the CatchYourSupper class to put two pieces of food at the top of the screen when the scenario begins.
This time you should see the two pieces of food start at the top of the screen and move to the bottom. When they reach the bottom, they should disappear. If these things do not happen, you need to STOP working and go back and fix the code so that the program works as described up until this point.
Create a class that is a subclass of LeftRightMover named FoodCatcher. Give the food catcher any image you would like. Edit the constructor of the CatchYourSupper class again to put the FoodCatcher in the world when it starts up. The food catcher should be positioned at the bottom of the screen.
You will see the food drop again and the food catcher at the bottom of the screen at this point.
Write code in the act method of the food catcher so that when the user presses the left key, the catcher moves left, and when the user presses the right key, the catcher moves right. Note that there are methods named moveLeft and moveRight built into the LeftRightMover that you can use to help you.
This time, the food will fall and you should be able to move the food catcher left and right along the bottom of the screen.
Instead of making the food always appear when the scenario is loaded, write code in the act method so that the food appears randomly. The food should always appear at the top of the screen, but it should not appear every time act is called on the world. In fact, you will want to make the food appear very infrequently in the final scenario. Take random numbers out of 1000 perhaps and look for numbers less than 10.
This time, the food should not appear at the beginning of the scenario, but rather should show up over the course of time that the scenario is running.
Write the code inside the food catcher that looks to see if the catcher intersects with the food that is falling. If it does, we should remove the food from the world. Note that the method getIntersectingFood in the LeftRightMover class returns an Actor, not a boolean. This Actor is the actor that the LeftRightMover is intersecting with. It will have the value "null" if there is no intersection happening. Therefore, we are intersecting with a food if the value that comes back from the call getIntersectingFood is not null.
Now, if you move the food catcher under the food as it is falling, it should disappear from the screen when it touches the food catcher.
Add an instance variable to the FoodCatcher class to count the number of pieces of food the food catcher has caught. Create a constructor for the FoodCatcher class and initialize the instance variable to zero inside the constructor. Modify your code in act so that when the food catcher intersects with the food, the count is increased by 1.
This time, you will not see a change in the behavior of the food catcher. But, if after you catch a piece of food, you stop the scenario and check to see if the value of your instance variable has changed. Right click on the food catcher actor and select Inspect from the menu. You will get a dialog that appears and inside it you will see the value of the instance variable for the food catcher. If you saw your food catcher collide with a piece of food, that value should be greater than zero.
You will once again modify the act code in the food catcher so that after a food catcher has intersected with a food, removed it from the world and updated the counter variable, the catcher checks the value of the instance variable. If the value is greater than or equal to 5, then the scenario should stop.
to see the final functionality of this lab.
Once you have completed the functionality for Lab 5, you will submit the zipped up version of the Lab 5 scenario.
For directions on submitting, please see here.
Please recall that you must achieve a grade of at least 50% on each of the labs to be allowed to take the third practical exam. This lab is technically due on Friday, March 18th at 11:59:59pm. However, if you want to stay current with the course, I would recommend having this assignment completed within a week. You will need mastery of the skills you learn in this lab to be successful at practical exam 2.
Please make sure to look at your grading information on Web-CAT. See the grading information page for more details about how to interpret Web-CAT grade reports. (At this time, grading for this assignment via Web-CAT is not enabled.)
The following tests were run on your assignment submission. Each test was given equal weight.
Lab authored by Adrienne Decker