Skip to content

Commit fbd0dca

Browse files
committed
ch10 revisions
1 parent 6cc2f10 commit fbd0dca

File tree

3 files changed

+52
-17
lines changed

3 files changed

+52
-17
lines changed

ch10/Append.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

ch10/Builder.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.util.Scanner;
2+
3+
/*
4+
* StringBuilder example.
5+
*/
6+
public class Builder {
7+
8+
public static void main(String[] args) {
9+
Scanner in = new Scanner(System.in);
10+
11+
// less efficient
12+
System.out.println("Enter 10 lines:");
13+
14+
String slow = "";
15+
for (int i = 0; i < 10; i++) {
16+
String line = in.nextLine(); // new string
17+
slow = slow + line + '\n'; // two more strings
18+
}
19+
System.out.print("You entered:\n" + slow);
20+
21+
// more efficient
22+
System.out.println("Enter 10 lines:");
23+
24+
StringBuilder text = new StringBuilder();
25+
for (int i = 0; i < 10; i++) {
26+
String line = in.nextLine();
27+
text.append(line);
28+
text.append('\n');
29+
}
30+
System.out.print("You entered:\n" + text);
31+
32+
String result = text.toString();
33+
}
34+
35+
}

ch10/PointRect.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,40 @@ public class PointRect {
99
public static void main(String[] args) {
1010
Point blank;
1111
blank = new Point(3, 4);
12-
System.out.println(blank);
1312

1413
int x = blank.x;
1514
System.out.println(blank.x + ", " + blank.y);
1615
int sum = blank.x * blank.x + blank.y * blank.y;
1716

17+
// objects as parameters
18+
1819
Point p1 = new Point(0, 0);
1920
Point p2 = new Point(3, 4);
21+
double temp = distance(p1, p2);
2022
double dist = p1.distance(p2); // dist is 5.0
23+
System.out.println(blank);
24+
25+
// objects as return values
2126

2227
Rectangle box = new Rectangle(0, 0, 100, 200);
23-
moveRect(box, 50, 100);
28+
Point center = findCenter(box);
29+
30+
// rectangles are mutable
31+
32+
moveRect(box, 50, 100); // now at (50, 100, 100, 200)
2433
System.out.println(box);
34+
35+
box = new Rectangle(0, 0, 100, 200);
2536
box.translate(50, 100);
2637

38+
// aliasing revisited
39+
2740
Rectangle box1 = new Rectangle(0, 0, 100, 200);
2841
Rectangle box2 = box1;
2942

30-
System.out.println(box2.width);
3143
box1.grow(50, 50);
32-
System.out.println(box2.width);
44+
System.out.println(box1);
45+
System.out.println(box2);
3346
}
3447

3548
/**

0 commit comments

Comments
 (0)