CSE 113 - Spring 2011 - Banner
  • CSE 113 - Spring 2011
CSE 113 - Spring 2011 - Navigation

Lab 5
Last modified: March 04 2011 10:59:16 AM

CSE 113 - Spring 2011 - Announcements

Catch Your Supper

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:

  • CatchYourSupper - The world in which this scenario takes place.
  • DownwardMover - A class that will be the superclass for the food that will drop from the sky.
  • LeftRightMover - A class that will be the superclass for the hero that will catch the food.

 

You are to complete the following functionality for the scenario.

Food

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.

Compile and Run

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.

Food Disappears at Bottom

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.

Compile and Run Again

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.

Food Appears at Top of Screen

Edit the constructor of the CatchYourSupper class to put two pieces of food at the top of the screen when the scenario begins.

Compile and Run Again

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.

FoodCatcher

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.

Compile and Run Again

You will see the food drop again and the food catcher at the bottom of the screen at this point.

FoodCatcher Moves Left and Right

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.

Compile and Run Again

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.

Food Appears Randomly

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.

Compile an Run Again

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.

Catcher catches the Food

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.

Compile an Run Again

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.

Counting the food

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.

Compile an Run Again

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.

Stopping the Scenario

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.

Compile an Run One Last Time

to see the final functionality of this lab.


Submitting the Assignment

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.


Grading Information

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.

  1. The Food moves down the screen whenever act() is called.
  2. The Food is removed when it hits the bottom of the world.
  3. The FoodCatcher is placed at the bottom of the screen when the scenario is loaded.
  4. The FoodCatcher moves left when the user presses the left arrow key.
  5. The FoodCatcher moves right when the user presses the right arrow key.
  6. Food randomly appears in the world.
  7. The Food disappears when caught by the FoodCatcher.
  8. There is an instance variable inside the FoodCatcher to keep track of how many pieces of food are caught.
  9. The instance variable inside of FoodCatcher updates appropriately when Food is caught.
  10. The scenario stops when the FoodCatcher catches 5 or more pieces of food.

Lab authored by Adrienne Decker

CSE 113 - Spring 2011 - Footer

Page maintained by Adrienne Decker

Contact: adrienne@buffalo.edu | 130 Bell Hall | (716)645-3184