Homework #2 - University of Washington



Homework #2

Purpose

The purpose of this assignment is to give you experience defining your own classes, instance variables, constructors, and methods. You will be given an incomplete Scene class, that when invoked, creates a road with a moving car on it. Your job is to finish the roadside scene by adding other cars on the road and populating the background with stationary and animated objects. The objects in the background could be clouds, a sun, trees, signs, mountains, etc…. the limit is your imagination and time. Your roadside scene must have at least two distinct animated cars, two stationary background shape objects and at least one other animated background object.

Instructions

1. If you are working on the IPL computers, you will need to do the following steps to set up your computer:

a) Download java-142.bat and save to the C:\CSE\tools directory. (This is the same folder that contains jeva-142.bat and javac-142.bat)

b) Download setpaths.bat and save in this same directory (C:\CSE\tools) Click "YES" if prompted to overwrite and existing file

c) Follow the instructions on the Computing at Home webpage to test your setup.

If you are working from home, go to the Computing at Home page --  **This page has be revised significantly just on this Monday, so please read it carefully again.  Follow the instruction and make sure your environment is ready to go by compiling and executing the Test program. ** Start your text editor of choice and open the file setpath.bat. Make sure the SDKPATH in the file is the same with the directory where you installed the Java SDK and the TOOLPATH is the same with the directory where you saved the javac-142.bat and jeva-142.bat. If they are different, modify them correctly.

2. Download our implementation of the Car, Driving_Scene, Scene, and RoadStripe class (e.g., using right-click and Save Target As...) and put it in a newly created Homework2 folder, e.g. C:\CSE\homework2.

3. Double click on the setpaths.bat file (located at C:\CSE\tools on the IPL computers) and a command window should appear with a command prompt. The command prompt should indicate that you are in the C:\CSE\tools directory. At the command prompt type in

cd c:\

Then you are in your homework2 directory. You can run the downloaded Java program by first compiling it and then executing it. Type

javac-142 *.java

in order to compile all of the class files or just javac-142 “class name here”.java if you want to compile a single file. Then type:

java-142 Driving_Scene

to execute the main method in the Driving_Scene class. A graphical window should open and a car should drive across the screen on a two-lane road. Once it is done moving, you can just close the graphical window and return to the command prompt.

4. Decide what your roadside scene should be. It must have

1. the original driving car

2. a new driving car which drives somewhere on the road.

3. at least one other background objects using multiple shapes from the UWCSE graphics library, i.e., a Rectangle, Oval, Polygon, Triangle, etc..

4. at least one animated background shape using the UWCSE graphics library, i.e., a Rectangle, Oval, Polygon, Triangle, etc..

For example: You could add a tree made from a Rectangle and a Triangle, or you could add a flower made from a Line and a Circle as a background object. An animated object could be a sun that rises from an Oval, or some flying birds made with Polygon, or whatever you can imagine. We suggest that you sketch out your scene before you do any programming, identifying the main layout parameters like the various x and y coordinates and widths and heights you'll need to construct the various shapes.

Not Required: Feel free to create another class that is a copy of Car.java, but has a different class name (such as “Truck.java”). You can then create a different body type so that your new vehicle can drive in the opposite direction and on the other side of the street as the original car.

1. In order to edit the existing Java Classes, and the new ones you will create, use a text-editor of some kind (wordpad, notepad, BlueJ, etc…). Create a new background object class by opening your text-editor of choice and saving the file as “Sun.java” for an example. Make sure to change the file extension type to .java if the editor adds .txt to the end of filename. Each new class must be in its own file, and the files must have the same name as the class they contain (for example a class called “Tree” could not be in a file named “Flower.java” – it must be called “Tree.java”).

2. Your new class will need a constructor to initiate the shapes that make up your drawing. These constructors can have as many variables as needed to create the shapes at appropriate locations. Add a comment to the top of your file to explain what your class does to your users. Include your name and your section in your comment. Using the Car.java or RoadStripe.java as an example, create a move(), addTo(), and removeFromWindow() method for the new class.

1. The addTo method takes a GWindow and returns nothing. As you can guess, sending addTo to a background object should have the effect of causing it to appear on the argument GWindow.

2. On the same note, removeFromWindow() method causes the object to be erased.

3. The move() method uses the shape method moveBy to move the object on the GWindow by the specified integers deltaX and deltaY.

Once completed, save the file and at the command prompt compile the new class using javac-142. If there are errors, return to the text-editor and edit accordingly. Each time you edit the file it will need to be recompiled before it can be run again. If no errors, try to create an instance of it using jeva-142.bat by typing

Example: GWindow gw = new Gwindow();

Sun risingSun = new Sun(….);

risingSun.addTo(gw);

Hopefully what appears is what you meant to create.

3. Repeat the class creation for each new background object making sure to have a move() method if you plan to animate the shapes.

4. In the Scene.java class, add a new instance variable for your new class (e.g., "Sun risingSun;") and additional instance variables for your other background shapes.

5. In Scene.java’s constructor you will find the java code that creates the objects placed on the GWindow. Here is where you place the necessary lines of code to add your new objects to the GWindow. Notice that the order that you addTo the GWindow dictate the window layer placement. Since the Car was added to the GWindow last, it occupies the top layer. Now, when the constructor is invoked, your objects will be added to the existing driving scene. Hint: Look at where the existing objects are added to the GWindow for an idea of where to add your objects.

6. In order to animate the new background objects, you will need to call the objects’ move() method from the Animate() method already given to you in Scene.java. Animate() already contains a move() call for the car geo_metro. It moves one pixel (motion_deltaX) in the x direction and zero pixels (motion_zero) in the y direction for a specified number of moves. Depending on the animated object you choose to create, you may want it to move in the +/-Y and +/-X direction. To do this, place a one or negative one value in place of the X and Y direction variables for your new objects move() call. This way your object can move in any direction you want.

7. Ignore the contents of Driving_Scene.java for this assignment. All you need to know is that when Driving_Scene.java is executed, the main() method is invoked that creates a new instance of the Scene class you just edited. The main() method is a required feature for a stand-alone program to run. You may look at the code if you like, but Driving_Scene.java will not be changed for this assignment.

8. Once you’ve finished compiling all of your classes, compile and execute Driving_Scene.java. Hopefully objects are moving all over the screen in the pattern that you created. It is okay if the object moves off the screen and can no longer be seen (ex: rising Sun, flying birds, etc).

9. Just to be on the safe side -- go back and reread the instructions to make sure you've done everything (a good practice for every homework!)

10. Electronically turn in your Scene.java file as well as all of your new object class files through this electronic submission turnin-form.

Grading

You will be graded on two things: the correctness of your program (i.e., how well you followed directions, and how well your program does what we asked), and on the clarity of your program (i.e., how readable it is, and how well it communicates the intent of what you were trying to accomplish to the human reader). Clarity will benefit from good explanatory comments, good choice of variable names, good use of local variables, and good use of indentation to group things.

Grading will be based on a five point system.

5 – Mastery of the program, many background and animated objects moving independently.

4 – All requirements are meet and the program runs smoothly.

3 – The program runs for the most part, but a requirement is missing.

2 – The program doesn’t run, but you showed good effort.

1 – Program doesn’t run and due to lack of comments, we cannot tell what you did.

0 – Did not Turn the program in or it was late.

Have fun!

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download