Sunteți pe pagina 1din 2

CSIT 115 Homework 2 Objects and Object References, and first Point.

java
1. Reference Semantics of Arrays. Arrays in Java are objects, so an array object can have multiple references to it as shown on pg. 462. Suppose list1, list2, and list3 are as shown there, i.e., the code at the top of pg. 462 has just been run. a. Suppose now "list2[2] = 0" is executed. What values exist now for the ints referred to as list1[2], list2[2], and list3[2]? b. Now suppose int[] list4 = list1 is executed. Draw a picture showing the references to the array object pointed to by list1. c. Now suppose incrementAll(list1) is executed (code on pg. 463). Show the resulting values in list1 and list4. 2, Problem 15, pg. 492. Note the discussion of Arrays.toString on pg. 448. 3. Problem 17, pg. 493. First read the section "Reversing an Array", pp. 454-458. You can use the swap method shown there, but for this case make it swap entries 0 and 1, then 2 and 3, then 4 and 5, etc., that is, i and i+1 in general. Figure out how to set up a for loop that hops by twos through the indexes. 4. Arrays of Objects. Look at the code and picture on pg. 470. These are JDK Points, so you need "import java.awt.*;". Suppose the array "points" has been set up as shown there. a. What is output by
for (i=0; i < points.length; i++) { System.out.print(" " + points[i].getX()); }

Recall from class that getX() returns the x-coordinate of a JDK Point. b. Now suppose points[1].translate(1,1) is executed. What are the new contents of the array? c. Now suppose "Point[] picture = points;" is executed. Explain how the drawing on pg. 470 should be modified or redraw it. 5. OO Programming. Problem 2, pg. 548. 6. Problem 3, pg.548.

7. Using JDK Point. Modify PointMain, pg. 532 by dropping the calls to distanceFromOrigin, and adding the "import java.awt.*" to allow use of JDK Ponts. Write code to put the two Poins in an array of 2 Points named pair. Do the translates using array references, such as pair[0] and pair[1] instead of p1 and p2. Print the points out using a loop over the array. Also try Arrays.toString on the array. 8. First Version of Point.java. In PointMain, pg. 507, the Points are not JDK Points but rather the first "user-defined" Point objects set up by Point.java on pg. 506. We can access the x and y coordinate of these Points by p1.x and p1.y instead of using the getX() and getY() methods of JDK Points. In fact, getX() and getY() won't work with these Points, because there are no methods at all defined in Point.java, only the fields x and y. But this shows us how to hold data inside objects. Write a program PointMain1.java that sets up such a Point at (10,20) and then translates it by 1 in each coordinate, and then prints it out (like line 32 of PointMain, pg. 508). To compile this, get Point.java and PointMain1.java in the same directory, and use DrJava or "javac *.java". 9. Problem 4, pg. 548. Add "import java.awt.*;". to make these Points be JDK Points. 10. Problem 7, pg. 548.

S-ar putea să vă placă și