James Madison University, Spring 2018 Semester
Lab21: Simulation of mutable objects
Background
Object-oriented programming changes the view of a program as a series of steps to an interaction between objects. You design the objects by specifying what they should look like (attributes) and what behaviors they should have (methods). Your program will control how those objects interact in the "world" (which in the case of Greenfoot, is just another object).
Objectives
Design an interactive simulation using Greenfoot.
Write constructor, accessor, and mutator methods.
Part 1 - Create a scenario
Last time, we took our first look at game design with Greenfoot. Today we will create a new simulation from scratch.
-
Run Greenfoot (from the Linux menu) and select "Scenario > New Java Scenario..." (from the Greenfoot menu). Name your scenario
Lab21
(under your CS149 directory). -
Right-click the "MyWorld" rectangle on the right, and select "Delete". We'll create a different World in the next step.
-
Right-click the "World" rectangle on the right, and select "New subclass..." Name the subclass
Road
and select a suitable image from the "backgrounds" category. -
Right-click the "Actor" rectangle on the right, and select "New subclass..." Name the subclass
Car
and select a suitable image from the "transport" category. -
Press the Reset button, and then add a
new Car()
to the world. After placing the car, select "Controls > Save the World" from the menu. If that menu is disabled, try running and resetting the program.
When you click "Save the World", a new editor window should open with Road.java. Saving the world automatically creates (or edits) the prepare method which is invoked by the constructor.
Part 2 - Basic Car methods
Double-click the Car rectangle (on the right) to open Car.java. Write the code for the following steps above the
act()
method.Create three
private
variables within the class: an integer that holds the car's year, a string that holds the car's make, and an integer that holds the car's speed.-
Create a constructor that accepts the model year and make, and sets the speed to 0. Make the constructor's parameters the same names as the private attributes, and use the
this
keyword when assigning the values. -
Create a
toString()
method that accepts no parameters and returns a string in the format"A %d %s that is going %d mph"
.-
Do NOT print anything in the method (no System.out). It should only create and return the string.
-
String.format()
operates just likeprintf()
. You should use this method to create the string.
-
-
Compile your
Car
class and correct any errors. You may want to ask a TA to check over your code. At this point, you should have a compiler error in your Road.java file. Fix this error by providing info about your car.
The compiler error (should have) happened when you added a constructor that required parameters. When there is no constructor, Java automatically generates a default constructor (without parameters) that does nothing.
Part 3 - Test your Car
-
Create a second car object of your choice in the
Road.prepare()
method using the year and make of your "dream" car. Go back to Car.java and add the following line at the top:
import javax.swing.JOptionPane;
Add the following code to the
Car.act()
method:// handle mouse click events if (Greenfoot.mouseClicked(this)) { JOptionPane.showMessageDialog(null, this.toString()); }
Remember, you can press Ctrl+Shift+I to fix the indenting of your code.
From the main screen, compile the project and click "Run". Then click on each car and see what happens.
Part 4 - Go cars, go!
- Pause or reset the simulation before writing new code. Right-click one of the car objects, and see what methods are in the context menu.
For this lab, you do NOT have to write documentation comments for each method. But double check that you have written your name and today's date in both files.
-
Create accessor methods that let you access each individual attribute of the
Car
. They should be named with "get" followed by the name of the attribute. So one of your accessor methods would begetSpeed()
. Each accessor should take no parameters and return the value of that attribute. -
Create two mutator methods,
accelerate()
andbrake()
. These methods take no parameters and return no values. When called, the accelerate() method should increase the speed variable by 1 mile per hour. The brake() method should decrease the speed variable by 1 mile per hour. Add the following code to the
Car.act()
method:// update speed and move the car if (Greenfoot.isKeyDown("up")) { accelerate(); } if (Greenfoot.isKeyDown("down")) { brake(); } move(speed);
- Compile and run the simulation, then click on a car while it's moving. You may need to adjust the simulation speed slider at the bottom.
Part 5 - Stop cars, stop!
Add a private boolean named
crash
to the Car class. Set this variable to false in the constructor.In the act method, surround the "update speed and move the car" code with a decision that checks if the car has not crashed.
After the line
move(speed);
(i.e., just before the closing brace of the if block) add the following code:// check if the car has crashed Actor other = getOneIntersectingObject(Car.class); if (other != null) { crash = true; }
Test your code by moving and rotating your cars so that they will collide. If everything is correct, they should stop.
Add sound effects to your project! Download a wav or mp3 file to your scenario/sounds directory. (Not all files are supported; if one doesn't work, try another one.) Then add the following example code where applicable:
Greenfoot.playSound("crash.wav");
At some point, you should try running your scenario with "lots" of cars. Don't forget to save the world! :) Submit your Car.java and Road.java files via Canvas by the end of the day. Make sure you wrote your name and today's date in the Javadoc comments of both source files.