Skip to content

Commit 90cab32

Browse files
authored
ch10
1 parent 4bf0fed commit 90cab32

4 files changed

Lines changed: 160 additions & 137 deletions

File tree

ch10/Big.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.math.BigInteger;
2+
3+
/**
4+
* calculating factorial with BigInteger
5+
*/
6+
public class Big {
7+
public static BigInteger factorial(int n) {
8+
BigInteger factorial = BigInteger.valueOf(n);
9+
for (int i = 1; i < n; i++) {
10+
BigInteger mult = BigInteger.valueOf(i);
11+
factorial = factorial.multiply(mult);
12+
}
13+
return factorial;
14+
}
15+
16+
public static void main(String[] args) {
17+
for (int i = 0; i < 31; i++) {
18+
System.out.println(i + "\t" + factorial(i));
19+
}
20+
}
21+
}

ch10/PointRect.java

Lines changed: 90 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,90 @@
1-
import java.awt.Point;
2-
import java.awt.Rectangle;
3-
4-
/**
5-
* Demonstates use of Point and Rectangle classes.
6-
*/
7-
public class PointRect {
8-
9-
public static void main(String[] args) {
10-
Point blank;
11-
blank = new Point(3, 4);
12-
System.out.println(blank);
13-
14-
int x = blank.x;
15-
System.out.println(blank.x + ", " + blank.y);
16-
int sum = blank.x * blank.x + blank.y * blank.y;
17-
18-
Rectangle box = new Rectangle(0, 0, 100, 200);
19-
moveRect(box, 50, 100);
20-
System.out.println(box);
21-
box.translate(50, 100);
22-
23-
Rectangle box1 = new Rectangle(0, 0, 100, 200);
24-
Rectangle box2 = box1;
25-
26-
System.out.println(box2.width);
27-
box1.grow(50, 50);
28-
System.out.println(box2.width);
29-
}
30-
31-
/**
32-
* Prints the attributes of a Point object.
33-
*/
34-
public static void printPoint(Point p) {
35-
System.out.println("(" + p.x + ", " + p.y + ")");
36-
}
37-
38-
/**
39-
* Computes the distance between two points.
40-
*/
41-
public static double distance(Point p1, Point p2) {
42-
int dx = p2.x - p1.x;
43-
int dy = p2.y - p1.y;
44-
return Math.sqrt(dx * dx + dy * dy);
45-
}
46-
47-
/**
48-
* Finds the center of a Rectangle and returns a new Point.
49-
*/
50-
public static Point findCenter(Rectangle box) {
51-
int x = box.x + box.width / 2;
52-
int y = box.y + box.height / 2;
53-
return new Point(x, y);
54-
}
55-
56-
/**
57-
* Moves a Rectangle by modifying the x and y attributes.
58-
*/
59-
public static void moveRect(Rectangle box, int dx, int dy) {
60-
box.x = box.x + dx;
61-
box.y = box.y + dy;
62-
}
63-
64-
/**
65-
* Exercise on returning objects.
66-
*/
67-
public static void exercise2() {
68-
Point blank = new Point(5, 8);
69-
70-
Rectangle rect = new Rectangle(0, 2, 4, 4);
71-
Point center = findCenter(rect);
72-
73-
double dist = distance(center, blank);
74-
System.out.println(dist);
75-
}
76-
77-
/**
78-
* Exercise on aliasing.
79-
*/
80-
public static void exercise3() {
81-
Rectangle box1 = new Rectangle(2, 4, 7, 9);
82-
Point p1 = findCenter(box1);
83-
printPoint(p1);
84-
85-
box1.grow(1, 1);
86-
Point p2 = findCenter(box1);
87-
printPoint(p2);
88-
}
89-
90-
}
1+
import java.awt.Point;
2+
import java.awt.Rectangle;
3+
4+
/**
5+
* Demonstates use of Point and Rectangle classes.
6+
*/
7+
public class PointRect {
8+
9+
public static void main(String[] args) {
10+
Point blank;
11+
blank = new Point(3, 4);
12+
System.out.println(blank);
13+
14+
int x = blank.x;
15+
System.out.println(blank.x + ", " + blank.y);
16+
int sum = blank.x * blank.x + blank.y * blank.y;
17+
18+
Rectangle box = new Rectangle(0, 0, 100, 200);
19+
moveRect(box, 50, 100);
20+
System.out.println(box);
21+
box.translate(50, 100);
22+
23+
Rectangle box1 = new Rectangle(0, 0, 100, 200);
24+
Rectangle box2 = box1;
25+
26+
System.out.println(box2.width);
27+
box1.grow(50, 50);
28+
System.out.println(box2.width);
29+
}
30+
31+
/**
32+
* Prints the attributes of a Point object.
33+
*/
34+
public static void printPoint(Point p) {
35+
System.out.println("(" + p.x + ", " + p.y + ")");
36+
}
37+
38+
/**
39+
* Computes the distance between two points.
40+
*/
41+
public static double distance(Point p1, Point p2) {
42+
int dx = p2.x - p1.x;
43+
int dy = p2.y - p1.y;
44+
return Math.sqrt(dx * dx + dy * dy);
45+
}
46+
47+
/**
48+
* Finds the center of a Rectangle and returns a new Point.
49+
*/
50+
public static Point findCenter(Rectangle box) {
51+
int x = box.x + box.width / 2;
52+
int y = box.y + box.height / 2;
53+
return new Point(x, y);
54+
}
55+
56+
/**
57+
* Moves a Rectangle by modifying the x and y attributes.
58+
*/
59+
public static void moveRect(Rectangle box, int dx, int dy) {
60+
box.x = box.x + dx;
61+
box.y = box.y + dy;
62+
}
63+
64+
/**
65+
* Exercise on returning objects.
66+
*/
67+
public static void exercise2() {
68+
Point blank = new Point(5, 8);
69+
70+
Rectangle rect = new Rectangle(0, 2, 4, 4);
71+
Point center = findCenter(rect);
72+
73+
double dist = distance(center, blank);
74+
System.out.println(dist);
75+
}
76+
77+
/**
78+
* Exercise on aliasing.
79+
*/
80+
public static void exercise3() {
81+
Rectangle box1 = new Rectangle(2, 4, 7, 9);
82+
Point p1 = findCenter(box1);
83+
printPoint(p1);
84+
85+
box1.grow(1, 1);
86+
Point p2 = findCenter(box1);
87+
printPoint(p2);
88+
}
89+
90+
}

ch10/Pow.java

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1-
/**
2-
* BigInteger exercise.
3-
*/
4-
public class Pow {
5-
6-
/**
7-
* Integer exponentiation.
8-
*/
9-
public static int pow(int x, int n) {
10-
if (n == 0) return 1;
11-
12-
// find x to the n/2 recursively
13-
int t = pow(x, n / 2);
14-
15-
// if n is even, the result is t squared
16-
// if n is odd, the result is t squared times x
17-
if (n % 2 == 0) {
18-
return t * t;
19-
} else {
20-
return t * t * x;
21-
}
22-
}
23-
24-
}
1+
import java.math.BigInteger;
2+
3+
/**
4+
* BigInteger exercise.
5+
*/
6+
public class Pow {
7+
8+
/**
9+
* Integer exponentiation.
10+
*/
11+
public static BigInteger pow(int x, int n) {
12+
if (n == 0) return BigInteger.valueOf(1);
13+
14+
// find x to the n/2 recursively
15+
BigInteger t = pow(x, n / 2);
16+
17+
// if n is even, the result is t squared
18+
// if n is odd, the result is t squared times x
19+
if (n % 2 == 0) {
20+
return t.multiply(t);
21+
} else {
22+
return t.multiply(t).multiply(BigInteger.valueOf(x));
23+
}
24+
}
25+
26+
}

ch10/Riddle.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
import java.awt.Point;
2-
3-
/**
4-
* Exercise on passing objects as parameters.
5-
*/
6-
public class Riddle {
7-
8-
public static int riddle(int x, Point p) {
9-
x = x + 7;
10-
return x + p.x + p.y;
11-
}
12-
13-
public static void main(String[] args) {
14-
int x = 5;
15-
Point blank = new Point(1, 2);
16-
17-
System.out.println(riddle(x, blank));
18-
System.out.println(x);
19-
System.out.println(blank.x);
20-
System.out.println(blank.y);
21-
}
22-
23-
}
1+
import java.awt.Point;
2+
3+
/**
4+
* Exercise on passing objects as parameters.
5+
*/
6+
public class Riddle {
7+
8+
public static int riddle(int x, Point p) {
9+
x = x + 7;
10+
return x + p.x + p.y;
11+
}
12+
13+
public static void main(String[] args) {
14+
int x = 5;
15+
Point blank = new Point(1, 2);
16+
17+
System.out.println(riddle(x, blank));
18+
System.out.println(x);
19+
System.out.println(blank.x);
20+
System.out.println(blank.y);
21+
}
22+
23+
}

0 commit comments

Comments
 (0)