Tino Intro To Java

Last modified: April 25, 2023

Greenfoot Practice Quiz 2 - Cars

In this mini-lab, you will write code for two kinds of cars that react and behave differently. Watch this video to see how the program behaves.

Practice Quiz Instructions

  • Create a new project named in the format PX_LastName_FirstName_Cars
  • Make a subclass of Actor named Car and choose any car image from Transportation.

Car Class

To start with, add the following code to the Car's act() method:

public void act() {
    moveCar();
    bounceOffWalls();
    handleCollisions();
}
  1. The moveCar() method should move the car horizontally in its current direction (left or right). Hint: You will need the Car to keep track of its velocity. You decide the default velocity for now.
  2. The bounceOffWalls() method should reverse the car's direction as soon as it touches the left or right edges of the World.
  3. The handleCollisions() method should check whether the car is touching another car and if so it calls onCrash() on BOTH itself and the car it crashed into.
  4. When a Car crashes, an Apple is placed at the car's location and the car is removed from the world. Hint: You need to make the Apple class.
  5. Add a default constructor that makes a default Car travel to the right by 5 pixels per frame.
  6. Add a second constructor that allows a Car to travel at a given velocity.
  7. Add getter and setter methods for the velocity

Bumper Car Class

Make a subclass of Car named BumperCar and choose a different car image for it.

In the BumperCar class:

  1. Add the same two constructors as Car.
  2. Do not override the act() method because the parent class Car already has an act() method that does everything we want.
  3. A BumperCar can travel horizontally or vertically, but NOT diagonally. Add any instance variable(s) you need for the car to know whether it should move vertically or horizontally. 4. The two constructors should both initialize the car to move horizontally.
  4. Add a third constructor that takes two parameters and initializes instance variables according to the values passed in.
    • a variable representing whether the movement is horizontal or vertical (you decide what the values mean).
    • An integer representing the velocity
  5. Override the moveCar() method so it moves the car in whichever direction it should move (vertically or horizontally) by the current velocity.
  6. Override the bounceOffWalls() method so it calls its parent's bounceOffWalls() method and then checks whether to bounce off the top or bottom edges of the world.
  7. Override the onCrash() method so that instead of dropping an Apple and removing itself, a BumperCar changes its direction from horizontal to vertical or vise-versa with the same speed. So, if the BumperCar was travelling horizontally and crashes into another car, it changes to travelling vertically.

Submission Instructions

Zip your entire project folder and submit it below.

  • Name your project folder PX_LastName_FirstName_Cars.
  • Right click on your project folder and Send To -> Compressed (zipped) folder. On a mac compressing is similar, using a right click, but has a slightly different option.
    • You should end up with a zip file named PX_LastName_FirstName_Cars.zip. For example if you were in 3rd period and named Michael Wang, then you would name the file P3_Wang_Michael_Cars.zip

You must Sign In to submit to this assignment

Dark Mode

Outline