hw2

advertisement
CSIT 115 Homework 2 Objects, Object References, and
Point class
This homework is worth 70 points, and is due at the beginning of class on 10/21/2015 on
paper. Each question is worth 10 points, so the best 7 out of 11 questions will count toward
your grade.
1. Reference Semantics of Arrays. Arrays in Java are objects, so an array object
can have multiple references to it as shown on pg. 476.
int[] list1 = new int[5];
int[] list2 = new int[5];
for (int i = 0; i < list1.length; i++) {
list1[i] = 2 * i + 1;
list2[i] = 2 * i + 1;
}
int[] list3 = list2;
Suppose list1, list2, and list3 are as shown there, i.e., the code at the top of pg.
476 has just been run. The questions below ask you what will happen after
certain code is executed, they are in order, so b should take into account what
was run in a and then b; c should take into account what was run in a and then b
and then c.
a. Suppose now "list3[0] = 4" is executed. What values exist now for the ints
referred to as list1[0], list2[0], and list3[0]?
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. 477):
public static void incrementAll(int[] data) {
for (int i = 0; i < data.length; i++) {
data[i]++;
}
}
Show all values in list1, list2, list3, and list4 after executing that.
2. Problem 19, pg. 507 (with minor modifications). What is the output of the
following program?
public class ReferenceMystery1 {
public static void main(String[] args) {
int x = 0;
int[] a = {0, 0, 0, 0};
x++;
mystery(x, a);
System.out.println(x + " " +
Arrays.toString(a));
}
public static void mystery(int x, int[] a) {
x++;
a[x] += 1;
System.out.println(x + " " +
Arrays.toString(a));
}
}
Note the discussion of Arrays.toString in (pp. 462-464).
3. Problem 21, pg. 508. Write a method called swapPairs that accepts an array
of integers and swaps the elements at adjacent indices. That is, elements 0 and
1 are swapped, elements 2 and 3 are swapped, and so on. If the array has an
odd length, the final element should be left unmodified. For example, the list
[10, 20, 30, 40, 50] should become [20, 10, 40, 30, 50] after a call to your
method. First read the section "Reversing an Array", 7.2, pp. 468-472. 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. Suppose we use the following code from p. 484 (section
7.4):
Point[] points = new Point[3];
points[0] = new Point(3, 7);
points[1] = new Point(4, 5);
points[2] = new Point(6, 2);
There is also the following picture on that page:
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. 484 should be modified or redraw it.
5. OO Programming. Problem 2, pg. 564. What is an object? How is an object
different from a class?
6. Problem 3, pg.564. What is the state of a String object? What is its behavior?
7. Using JDK Point. Modify PointMain, pp. 547-548 by dropping the calls to
distanceFromOrigin, and adding the "import java.awt.*" to allow use of JDK
Points. The file can be downloaded here. Write code to put the two Points 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. If you do not modfy the code
at the link and just copy that you will get zero points. Also, note that you are
using JDK Point objects. You cannot access x and y directly, but you do have
access to all the methods for the Point class of the JDK.
8. First Version of Point.java. In PointMain, pp. 523-524, the Points are not
JDK Points but rather the first "user-defined" Point objects set up by Point.java
on pg. 522. The files can be downloaded here. 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.
524, or at link). To compile this, get Point.java and PointMain1.java in the
same directory, and use DrJava or "javac *.java".
9. Problem 4, pg. 564. What is the output of the following program (note that it
uses the original user-defined Point class from part 8, rather then JDK points)?
public class ReferenceMystery3 {
public static void main(string[] args) {
int a = 7;
int b = 9;
Point p1 = new Point(2, 2);
Point p2 = new Point(2, 2);
addToXTwice(a, p1);
System.out.println(a + " " + b + " " + p1.x +
" " +
p2.x);
addToXTwice(b, p2);
System.out.println(a + " " + b + " " + p1.x +
" " +
p2.x);
}
Public static void addToXTwice(int a, Point p1) {
a += a;
p1.x = a;
System.out.println(a + " " + p1.x);
}
}
10. Problem 7, pg. 564. Create a class called Name that represents a person’s
name. The class should have fields representing the person’s first name, last
name, and middle initial. Choose the most appropriate fields for these. The
class should contain only fields.
Download