![]() ![]() 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). If I typed: s2g.py Terraheads (no arrow parenthesis around Terraheads) I got no syntax errors but it said “NameError: name ‘s2g’ is not defined” I have all of the files that the ReadMe file instructed in the ScratchFoot Master File.Are you in the command line or the Python interactive interpreter? The error message sounds like the latter.Object-oriented programming changes the view of a program as a series of steps to an interaction between objects. Or wihout “python”: s2g.py Which gave me a syntax error under “”. Okay so what I typed was: python s2g.py which gave me a “Synta圎rror: invalid syntax” syntax error under ‘s2g.py’. $ cd path/to/files $ python sg2.py Terraheads2.sb2 TerraheadsReplace “path/to/files” with the actual location to the folder that contains Terraheads2.sb2īookOwl wrote:Can you copy and paste the whole error message? That would help a lot with figuring out what is wrong.First off I have no Idea how to program in python. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |