Click for Template

advertisement

Your Name Here

APCS, Mr. Merriman

Period 3, Station #

April 16, 2020

CHAPTER 9: MUTABLE OBJECTS

Exercise 1 - The point of this exercise is to make sure you understand the mechanism for passing Objects as parameters.

1. For the following program, draw a stack diagram showing the local variables and parameters of main and riddle, and show any objects those variables refer to.

2. What is the output of this program?

public static void main(String[] args) { int x = 5;

Point blank = new Point(1, 2);

System.out.println(riddle(x, blank));

System.out.println(x);

System.out.println(blank.x);

System.out.println(blank.y);

} public static int riddle( int x, Point p) { x = x + 7; return x + p.x + p.y;

}

Output here (stack diagram in space below)

Exercise 2

1. For the following program, draw a stack diagram showing the state of the program just before distance returns. Include all variables and parameters and the objects those variables refer to.

2. What is the output of this program?

public static double distance(Point p1, Point p2) { int dx = p1.x - p2.x; int dy = p1.y - p2.y; return Math.sqrt(dx*dx + dy*dy);

} public static Point findCenter(Rectangle box) { int x = box.x + box.width/2; int y = box.y + box.height/2; return new Point(x, y);

} public static void main(String[] args) {

Point blank = new Point(5, 8);

Rectangle rect = new Rectangle(0, 2, 4, 4);

Point center = findCenter(rect); double dist = distance(center, blank);

System.out.println(dist);

}

Output here (stack diagram in space below)

Exercise 3 - The method grow is part of the Rectangle class. Read the documentation at http://download.oracle.com/javase/6/docs/api/java/awt/Rectangle.html#grow(int, int) .

1. What is the output of the following program?

2. Draw a state diagram that shows the state of the program just before the end of main. Include all local variables and the objects they refer to.

public static void printPoint(Point p) {

System.out.println( "(" + p.x + ", " + p.y + ")" );

} public static Point findCenter(Rectangle box) { int x = box.x + box.width/2; int y = box.y + box.height/2; return new Point(x, y);

} public static void main(String[] args) {

Rectangle box1 = new Rectangle(2, 4, 7, 9);

Point p1 = findCenter(box1); printPoint(p1); box1.grow(1, 1);

Point p2 = findCenter(box1); printPoint(p2);

}

Output here (state diagram in space below)

3. At the end of main, are p1 and p2 aliased? Why or why not?

Answer

Exercise 4 You might be sick of the factorial method by now, but we’re going to do one more version.

1. Create a new program called Big.java and write an iterative version of factorial.

2. Print a table of the integers from 0 to 30 along with their factorials. At some point around 15, you will probably see that the answers are not right any more. Why not?

3. BigIntegers are Java objects that can represent arbitrarily big integers. There is no upper bound except the limitations of memory size and processing speed. Read the documentation of BigIntegers at http://download.oracle.com/javase/6/docs/api/java/math/BigInteger.html

.

4. To use BigIntegers, you have to add import java.math.BigInteger to the beginning of your program.

5. There are several ways to create a BigInteger, but the one I recommend uses valueOf. The following code converts an integer to a BigInteger: int x = 17;

BigInteger big = BigInteger.valueOf(x);

Type in this code and try it out. Try printing a BigInteger.

6. Because BigIntegers are not primitive types, the usual math operators don’t work. Instead we have to use methods like add. To add two BigIntegers, invoke add on one and pass the other as an argument. For example:

BigInteger small = BigInteger.valueOf(17);

BigInteger big = BigInteger.valueOf(1700000000);

BigInteger total = small.add(big);

Try out some of the other methods, like multiply and pow.

7. Convert factorial so that it performs its calculation using BigIntegers and returns a BigInteger as a result. You can leave the parameter alone —it will still be an integer.

8. Try printing the table again with your modified factorial method. Is it correct up to 30? How high can you make it go? I calculated the factorial of all the numbers from 0 to 999, but my machine is pretty slow, so it took a while. The last number, 999!, has 2565 digits.

Table with values for 0-30 here Final version of factorial using BigIntegers here

Exercise 5 Many encryption techniques depend on the ability to raise large integers to an integer power. Here is a method that implements a (reasonably) fast technique for integer

exponentiation: public static int pow( int x, int n) { if (n == 0) return 1;

// find x to the n/2 recursively int t = pow(x, n/2);

// if n is even, the result is t squared // if n is odd, the result is t squared times x if (n%2 == 0) { return t*t;

} else { return t*t*x;

}

}

The problem with this method is that it only works if the result is smaller than 2 billion. Rewrite it so that the result is a BigInteger. The parameters should still be integers, though.

You can use the BigInteger methods add and multiply, but don’t use pow, which would spoil the fun.

Rewritten version here

A.7 Exercises - If you are interested in graphics, now is a good time to read Appendix A and do the exercises there.

Exercise 1

Draw the flag of Japan, a red circle on white background that is wider than it is tall.

Code here

Exercise 2

Modify Mickey.java to draw ears on the ears, and ears on those ears, and more ears all the way down until the smallest ears are only 3 pixels wide.

The result should look like Mickey Moose:

Hint: you should only have to add or modify a few lines of code.

You can download a solution from http://thinkapjava.com/code/MickeySoln.java

.

Code here

Exercise 3

1. Download http://thinkapjava.com/code/Moire.java

and import it into your development environment.

2. Read the paint method and draw a sketch of what you expect it to do. Now run it. Did you get what you expected? For an explanation of what is going on, see http://en.wikipedia.org/wiki/Moire_pattern .

3. Modify the program so that the space between the circles is larger or smaller. See what happens to the image.

4. Modify the program so that the circles are drawn in the center of the screen and concentric, as in the following figure (left). The distance between the cir cles should be small enough that the Moiré interference is apparent.

5. Write a method named radial that draws a radial set of line segments as shown in the figure (right), but they should be close enough together to create a Moiré pattern.

6. Just about any kind of graphical pattern can generate

Moiré-like interference patterns. Play around and see what you can create.

Rewritten Moire.java here

Radial method here

Download