CSE 142 - Summer 2003 Homework 3 - Due: Sunday, July 13 9:30 PM A link to the turn-in page will appear on the course calendar webpage. ------------------ Written Assignment ------------------ To gain full credit on each question, your answer must have no significant flaws, must be clear to the reader, and must be complete (but remember that complete does not mean verbose -- be concise and to the point). As always, you should record your answers to the questions in a plain-text file (for example, "answers.txt") and then submit that text file along with the rest of your electronic turn-in. 1. Boolean arithmetic. Imagine that we run the following lines of code inside a method: int x = 4; double y = 6.0; boolean z = true; Determine the result of evaluating each of the following expressions: a) (x > 3) || z b) (x > 3) && z c) (x > y) || (!z) d) ((x > 6) || (y < 6.5)) && (x > 4) e) !((x > y) && (x < y)) 2. Loops. In class we recently saw how to count using a for loop. It looked something like this: for (int i=1; i <= 10; i++) { System.out.println(i); } To see what this piece of code prints out, you can type it into DrJava's Interactions pane using Shift+Enter (instead of Enter) to go to the next line. Then after the final "}", press Enter, and DrJava will begin executing. a) Show a revision of this code that counts backwards from 10 to 1. b) Show a revision of this code that counts from 1 to 10 by 3s. That is, it should display 1, then 4, then 7, then 10. c) Show a revision of this code (specifically, the loop control code) wherein the loop will never be executed. There are several ways you could accomplish this. 3. Consider the following rudimentary class made to represent a truck. public class Truck { /** * Create a truck with a full tank of gas of size capacity * with gas mileage mpg. * @param mpg Miles per gallon (mileage). * @param capacity The capacity of the gas tank. */ public Truck(double mpg, double capacity) { speed = 0.0; // begin at zero MPH gas = capacity; // start with full tank mileage = mpg; } /** * Calculate how many miles until more fuel is needed. * @return number of miles to go */ public double getMilesToGo() { double mtg = mileage * gas; return mtg; } //--------- /** speed of truck, in miles per hour */ double speed; /** gas in tank, in gallons */ double gas; /** gas mileage, in miles per gallon */ double mileage; } Do the following words refer to types, parameters, local variables, or instance variables? a) capacity b) double c) mtg d) mpg e) speed f) Truck (in line #1) 4. Consider the following snippets of code which have been cut out of some method. For each snippet, say what the value of the variable "answer" is at the end. Recall from Homework 2 that it's sometimes useful to keep track of the current value of each variable in a little table as you progress through each part. --- (a) --- int answer = 0; int x = 3; int y = 1; while (y <= x) { answer += 5; y++; } --- (b) --- int answer = 0; int x = 0; int y = 3; if (x == y) { answer = 15; } else if (x == answer) { answer = y; } else { answer = x; } --- (c) --- int answer = 0; int x = 5; int y = 30; if ((x == y) && (answer < 1)) { answer = 4; } --- (d) --- int answer = 0; for (int i=0; i < 50; i++) { answer += 3; } 5. This question requires you to use your web browser and the documentation for the CSE 142 graphics library. Just as in the case of the Java SDK library documentation, you can find a link to the CSE 142 graphics library documentation on the "Java Libraries" page on the course website. Go to the CSE 142 graphics library documentation web page. On the left of the screen is a list of Java classes that the library provides. Click on the class name Rectangle, and you will see a description of the Rectangle class in the main part of the window. Refer to the "Constructor Summary" section. a) There are three versions of the Rectangle constructor. How many parameters does each version take? What is the purpose of the version that takes 4 parameters? You can copy the text directly from the documentation page - you don't need to explain the details in your own words. b) In mostly your own words, what is the difference between the version of the Rectangle constructor that takes no parameters and the one you just looked at in part (a) that takes 4 parameters? Refer to the "Method Summary" section. c) Which corner of a Rectangle object is used by getX() and getY() to return the X and Y coordinates of the Rectangle? d) What would happen if I ran the following line of code on an already-existing Rectangle "myRect"? Why? myRect.moveTo(myRect.getX(), myRect.getY()); ---------------------- Programming Assignment ---------------------- Turn in Tree.java when you have completed this task. This project will introduce you to the UW CSE graphics library and give you some more practice creating a new Java class. Objective: - Implement a Tree class, based on the Horizon and Sun classes provided. 1. If you haven't done so already, download the cse142-hw3.zip file and unzip it. The project skeleton is in directory 'hw3'. 2. Before you can compile the code that is provided, you need to make sure that the uwcse.jar file is available to the compiler and the virtual machine. This .jar file contains the UWCSE graphics and animation libraries, among other things. You will be using many classes from these libraries throughout the rest of the quarter, and a good way to become familiar with them is to read through their documentation as in Written Question #5. If you are using DrJava and have not already told DrJava where to find uwcse.jar, follow the directions on the course web's "Computing at Home" page, under "DrJava", instruction #6. 3. If you skim through the code for the Director class in Director.java, you'll see that its job is to create a number of "props" (including Trees) and have them perform some actions on "stage". Unfortunately, you cannot compile the Director class in its current state, since you have not yet implemented the Tree class. If you would like to get an idea of what the program is supposed to do without the Trees, turn all the lines that refer to Trees into comments (programmers call this "commenting out" a line of code); then compile and run the Director. 4. Your job is to implement the Tree class. Trees are similar to the Horizon and the Sun, but just a little bit more complex. Look closely at the examples for information. a) There is one constructor for the Tree class. The constructor takes three parameters. * @param x the x pixel location of the base of the trunk * @param y the y pixel location of the base of the trunk * @param h the height of the trunk. Also used to determine the size * of the crown. In the Tree constructor, you should create at least one Rectangle to represent the trunk of the Tree and save a reference to it in an instance variable. You should also create at least one Oval to represent the crown (leaves) of the Tree and save a reference to it in an instance variable. An example image showing how your trees might look is posted on the 142 Calendar page. Information about the meaning of the parameters that you pass to the Rectangle and Oval constructors is available in the UWCSE library documentation, which you now know where to find. The size, color, and exact positioning of the trunk and crown are up to you. You can calculate the Tree's components' positions based on the values supplied to your Tree constructor. Careful: Notice that in the UWCSE graphics libraries, x increases from left to right across the screen, and y increases from top to bottom. (b) The Tree class implements the Prop interface, found in uwcse.animation. You don't yet know about Interfaces, but what this means is that you must implement the following two methods in Tree in order for it to be usable by the Director: public void addTo(GWindow g) public void removeFromWindow() Look at the code in Horizon and Sun to see what these methods need to do. Notice that since your Tree has two Shapes in it instead of one, it has to do a little more work than the Sun or the Horizon in order to add and remove itself from the picture. (c) Your Tree class should also implement two methods that change the color of the crown of the tree: public void fallTree() public void springTree() These methods should use the Oval's and Rectangle's method setColor(Color c) to set the color of the crown appropriate to the season (the specific colors are up to you). Colors are defined in class java.awt.Color (NOT to be confused with 'java.awt.color'). Again, refer to the Sun and Horizon classes to see how to use the Color class. 5. Once you have your Tree class implemented, you should be able to compile and run the entire application. The Producer class contains the "main" method for this application. The Director class is the one that actually calls the Tree constructor and the Tree methods. It will not compile as provided to you because it assumes that the Tree class exists. As suggested above, you can comment out those lines until you get something basic written for the Tree class. 6. As an OPTIONAL exercise, feel free to add other elements to the scene. You could, for example, change the color of the sun as it starts to set, add a lake, or color the sky. If you're good with math, try to make the sun follow a circular path rather than the linear one we made. If you make any changes to Director.java or create any additional .java files, please submit them along with your Tree.java and answers.txt file.