CS 102 Homework Laboratory # 5
Objective: To gain experience with loops and concurrency.

The Scenario

For this lab, we would like you to write a program that plays the game Frogger.

In this game, you control a frog that is trying to cross a busy 4-lane highway. Each lane has cars or trucks zooming (well ok, crawling) by. The vehicles in a given lane all travel at the same speed, but vehicles in different lanes may travel at different speeds (and even in different directions if you would like). The user is in control of a frog. Clicking in front of the frog moves it forward one hop (one hop is the width of a lane of traffic), clicking behind moves it back, and similarly for clicking to the left and right of it. The goal is for the user to get the frog across the highway without it getting squished.

If the frog does get squished it displays an "OUCH!" message at the bottom of the screen. The user can restart the frog from its original starting position by clicking the mouse once in the area below the lanes of the highway.

To start,

Click this link (while holding the Shift key) to download the Frogger starter script to your home folder.

Now open a terminal window and type

source initFrogger
As a result, there will now be a CS102/lab5 folder containing the following. You can also download these files using your browser by clicking on the links below: Open the .java files with Dr. Java. The .gif files are images of the frog and the cars and trucks.

A demo version should appear below if your web browser supports Java. You'll notice that the animation is not very smooth and the response to the mouse click is not always immediate. This will be true for your code too, and you shouldn't worry about it. In fact, getting smooth animations in Java requires some engineering expertise that is beyond the scope of this course (you can read about it on the web if you google for "Java" and "animation").



Preparing for this lab

Read all the instructions before doing anything else. We will begin by describing the classes you need to implement. After we describe the classes, we will outline a plan for proceeding with your implementation.

The classes

The key to good Java design is the choice of classes to represent the objects in your program. There are four different kinds of objects involved in the Frogger game: the frog (class Frog), the lanes of traffic (class Lane), the vehicles (class Vehicle), and the window controller (class Frogger). (There are also some graphical objects on the screen to represent the lane markings on the highway, but we won't discuss these, becuase we have provided in the starter folder the code that draws them.) Here is a general description of the purpose of each class:

The window controller Frogger

The Frogger class extends FrameWindowController; therefore it controls the game and its response to the player. It is responsible for: While these are weighty responsibilities, the code to do it is quite simple. We've supplied most of the initialization of the screen.

The Frog

The class Frog will define the appearance and behavior of the frog, which is represented on the screen by an image supplied by Frogger. The frog will be able to do the following things:

The Lanes

The class Lane defines a non-graphical active object that is responsible for producing an unending sequence of cars. Most importantly, the interval between successive cars varies randomly within some reasonable limits. The Lane is reponsible for this randomness. If you look again at the example game, you'll notice that the distance between cars isn't completely predictable - otherwise, the game would be rather boring. This randomness is produced by randomly chosen pause times in the run method of class Lane.

The Vehicles

Although many many vehicles will be created in the course of the game, by the miracle of modern object-oriented programming language technology, you only have to write simple code for one vehicle. The main task of your code is to animate the vehicle by moving it a given distance to the right (or left, if you want to include this feature) each time through the animation loop. Your code also must determine whether the vehicle has killed the frog, and remove the image from the canvas when it reaches the end of the lane.

The Image Files

There is an image file for the frog, and 8 images for vehicles. There are 4 different types of vehicles. For each type of vehicle there is an image of that vehicle facing right and another facing left. The image files are included in the files provided when you source initFrogger

It is fine if you only use a single image file so that all your vehicles look the same and move in the same direction. Using multiple pictures is a feature that we hope you add (it does make the display look better), but it is not required.

The frog is 83 pixels wide and 48 pixels tall. The widest vehicle is 139 pixels wide. The tallest vehicle is 66 pixels tall. This information should help you figure out how to place the vehicles and frog within the lanes. Remember to use constants effectively so that you would not need to change many values (if we introduce a much taller vehicle, for example).

Getting started on the full implementation

To see details on the implementation of each class, follow the links below:

There are many ways of proceeding to a full implementation. Here is one suggested ordering:

  1. Read the begin method in Frogger.java to understand how we've drawn the highway background for you.
  2. Add parameters and code to the Frog constructor to display the Frog's image. Then implement the kill, reincarnate, and hopToward methods to make the image move appropriately, and display and hide the OUCH message.
  3. Modify the Frogger class to create the frog, and write onMousePress to control the movements of the frog. Make sure that the frog hops around appropriately.
  4. Add the method overlaps to the Frog class.
  5. Write the Vehicle class.
  6. Test the Vehicle class by having the begin method in the Frogger class put one car on the road. (You don't want the code here in the end, but for now it lets you test the Vehicle class before you've written the Lane class.) Move the frog into the road to see if it gets killed by the car. Add code to reincarnate the frog and test it.
  7. Write the constructor of Lane. Move the code that creates a car from the Frogger class to the run method of the Lane class so that a lane generates a stream of cars on the lane. Make sure they don't bump into each other (they shouldn't as they are all going the same speed). Make sure that the frog gets killed if hit by any of the cars (though our cars will be hit-and-run - they don't stop!). Make sure that the gaps between the cars are not too small, not too large, and are randomly chosen.

Advanced Features

You may notice that the sample version of Frogger we have provided on this page has more features than we have required. Vehicles move in both directions and several different images are used to represent vehicles. You are encouraged, but definitely not required, to incorporate such extensions in your program for a small amount of extra credit. Only do so, however, after completing the construction of a program that meets the basic requirements.

Turning in your program

Hand in your final program in the usual way: make sure the files Frogger.java, Frog.java, Lane.java and Vehicle.java, as well as all the .gif files you're actually using, are in your-home-folder/CS102/lab5. If not, copy them there, and compile and run your program one last time in that folder to make sure that everything works properly.

To submit your files go to moodle in the CS102 course page and the Lab 5 section. There will be separate place to upload for each file. Three questions for you to answer are also provided below, which should be answered in the third activity provided in the moodle under lab 5.

  1. What was the important idea that you should have learned from this lab.
  2. Did the lab effectively teach you this idea.
  3. What questions do you still have about the material presented in this lab.

Good luck and have fun!

Grading Point Allocations

The grading of this lab will be more stringent for commenting. Make sure you have comments that describe briefly the purpose of each method you write.

Grading Point Allocations

Value Feature
Style (6 pts total)
2 pts. Appropriate comments
1 pt. Good choice of names
1 pt. Good use of boolean expressions
1 pt. Not doing more work than necessary
1 pts. Appropriate parameters
Correctness (14 pts total)
2 pt. Gaps between cars vary randomly
2 pt. Gaps between cars are not too small or large
2 pt. Car disappears at end of lane
2 pt. Car kills frog appropriately
2 pt. Frog moves correctly on clicks
2 pt. Frog says "ouch" when killed
2 pt. Frog reincarnated properly

Three feedback questions (4 pts total)


This lab is based on materials developed by the department of Computer Science at Williams College.