Don't forget to save the world! :) Submit your Car.java and Road.java files via Canvas by the end of the day. Then add the following example code where applicable:Īt some point, you should try running your scenario with "lots" of cars. If everything is correct, they should stop.Īdd sound effects to your project! Download a wav or mp3 file to your scenario/sounds directory. Test your code by moving and rotating your cars so that they will collide. In the act method, surround the "update speed and move the car" code with a decision that checks if the car has not crashed.Īfter the line move(speed) (i.e., just before the closing brace of the if block) add the following code:Īctor other = getOneIntersectingObject(Car.class) Set this variable to false in the constructor. You may need to adjust the simulation speed slider at the bottom.Īdd a private boolean named crash to the Car class. Compile and run the simulation, then click on a car while it's moving.The brake() method should decrease the speed variable by 1 mile per hour. When called, the accelerate() method should increase the speed variable by 1 mile per hour. These methods take no parameters and return no values. Each accessor should take no parameters and return the value of that attribute.Ĭreate two mutator methods, accelerate() and brake(). So one of your accessor methods would be getSpeed(). They should be named with " get" followed by the name of the attribute. But double check that you have written your name and today's date in both files.Ĭreate accessor methods that let you access each individual attribute of the Car. Right-click one of the car objects, and see what methods are in the context menu.įor this lab, you do NOT have to write documentation comments for each method. Pause or reset the simulation before writing new code.Then click on each car and see what happens. Remember, you can press Ctrl+Shift+I to fix the indenting of your code.įrom the main screen, compile the project and click "Run". JOptionPane.showMessageDialog(null, this.toString()) Go back to Car.java and add the following line at the top:Īdd the following code to the Car.act() method: Fix this error by providing info about your car(year as int 1969 and String for make "Ford Mustang").Ĭreate a second car object of your choice in the Road.prepare() method using the year and make of your "dream" car. You may want to ask a TA to check over your code.Īt this point, you should have a compiler error in your Road.java file. You should use this method to create the string.Ĭompile your Car class and correct any errors. String.format() operates just like printf(). It should only create and return the string. Make the constructor's parameters the same names as the private attributes, and use the this keyword when assigning the values.Ĭreate a toString() method that accepts no parameters and returns a string in the format "A %d %s that is going %d mph".ĭo NOT print anything in the method (no System.out). Write the code for the following steps above the act() method.Ĭreate 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.Ĭreate a constructor that accepts the model year and make, and sets the speed to 0. After placing the car, select "Controls > Save the World" from the menu.ĭouble-click the Car rectangle (on the right) to open Car.java. Press the compile button and then add a new Car() to the world. 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. 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. Run Greenfoot (from the Linux menu) and then select "Scenario > New." from the Greenfoot menu. Today we will create a new simulation from scratch. Write constructor, accessor, and mutator methods.īefore the break, we took our first look at game design with Greenfoot. Objectivesĭesign an interactive simulation using Greenfoot. Your program will control how those objects interact in the "world" (which in the case of Greenfoot, is just another object). You design the objects by specifying what they should look like (attributes) and what behaviors they should have (methods). Object-oriented programming changes the view of a program as a series of steps to an interaction between objects.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |