1
1
package chapter7 ;
2
2
3
3
public class Chapter7 {
4
- public static void main (String [] args ){
5
- /*** 7.4 ***/
6
- System .out .println ("*** Test 7.4" );
4
+ public static void main (String [] args ){
5
+ test_Prime ();
6
+ // 7.1: no code required
7
+ // 7.2: no code required
8
+ test_SevenPoint3 ();
9
+ test_SevenPoint4 ();
10
+ // 7.5: Skip (no new concepts in this problem. Only trick: line goes through center of 2 squares
11
+ test_SevenPoint6 ();
12
+ test_SevenPoint7 ();
13
+ }
14
+
15
+ public static void test_Prime (){
16
+ System .out .println ("*** Test Primes" );
17
+ System .out .println ("Is 11 prime? " + Prime .isPrime (11 ));
18
+ System .out .println ("Is 12 prime? " + Prime .isPrime (12 ));
19
+ System .out .println ("Is 13 prime? " + Prime .isPrime (13 ));
20
+ boolean [] primes = Prime .generatePrimes (50 );
21
+ System .out .print ("Primes from 1-50: " );
22
+ for (int i = 0 ; i < primes .length ; i ++){
23
+ if (primes [i ])
24
+ System .out .print (i + " " );
25
+ }
26
+ }
27
+
28
+ public static void test_SevenPoint3 (){
29
+ System .out .println ("\n \n *** Test 7.3: Intersecting Lines" );
30
+ Line line1 = new Line (new Point (2 ,0 ), new Point (2 ,3 ));
31
+ Line line2 = new Line (new Point (2 ,5 ), new Point (2 ,7 ));
32
+ Line line3 = new Line (new Point (1 ,2 ), new Point (3 ,3 ));
33
+ Line line4 = new Line (new Point (2 ,3 ), new Point (4 ,4 ));
34
+
35
+ System .out .println ("Lines intersect? (should be true): " + LineFunctions .intersect (line1 , line2 ));
36
+ System .out .println ("Lines intersect? (should be true): " + LineFunctions .intersect (line1 , line3 ));
37
+ System .out .println ("Lines intersect? (should be true): " + LineFunctions .intersect (line1 , line4 ));
38
+ System .out .println ("Lines intersect? (should be true): " + LineFunctions .intersect (line2 , line3 ));
39
+ System .out .println ("Lines intersect? (should be true): " + LineFunctions .intersect (line2 , line4 ));
40
+ System .out .println ("Lines intersect? (should be false): " + LineFunctions .intersect (line3 , line4 ));
41
+
42
+ // Tests our Line, LineFunctions classes for functions used in 7.6
43
+ System .out .println ("\n Lines intersect? (should be true): " + LineFunctions .sameLine (line1 , line2 ));
44
+ System .out .println ("Lines intersect? (should be false): " + LineFunctions .sameLine (line1 , line3 ));
45
+ System .out .println ("Lines intersect? (should be false): " + LineFunctions .sameLine (line1 , line4 ));
46
+ System .out .println ("Lines intersect? (should be false): " + LineFunctions .sameLine (line2 , line3 ));
47
+ System .out .println ("Lines intersect? (should be false): " + LineFunctions .sameLine (line2 , line4 ));
48
+ System .out .println ("Lines intersect? (should be false): " + LineFunctions .sameLine (line3 , line4 ));
49
+ }
50
+
51
+ public static void test_SevenPoint4 (){
52
+ System .out .println ("\n *** Test 7.4: Subtraction, Multiplication, Division" );
7
53
int result = SevenPoint4 .subtract (4 , 7 );
8
54
System .out .println (" 4 - 7 = " + result + "\n " );
9
55
@@ -15,39 +61,37 @@ public static void main (String [] args){
15
61
System .out .println (" 2 * -3 = " + result );
16
62
result = SevenPoint4 .multiply (-2 , -3 );
17
63
System .out .println ("-2 * -3 = " + result + "\n " );
18
-
19
- result = SevenPoint4 .divide (7 , 2 );
20
- System .out .println (" 7 / 2 = " + result );
21
- result = SevenPoint4 .divide (-7 , 2 );
22
- System .out .println ("-7 / 2 = " + result );
23
- result = SevenPoint4 .divide (7 , -2 );
24
- System .out .println (" 7 / -2 = " + result );
25
- result = SevenPoint4 .divide (-7 , -2 );
26
- System .out .println ("-7 / -2 = " + result );
27
-
28
- //7.6 -- I know how to code it, so I skipped it. Book's code/explanation is weird cuz they do line segments and complicate stuff.
29
- //Things to remember: ( y = mx + b) (The solution line must go through middle of squares)
64
+ try {
65
+ result = SevenPoint4 .divide (7 , 2 );
66
+ System .out .println (" 7 / 2 = " + result );
67
+ result = SevenPoint4 .divide (-7 , 2 );
68
+ System .out .println ("-7 / 2 = " + result );
69
+ result = SevenPoint4 .divide (7 , -2 );
70
+ System .out .println (" 7 / -2 = " + result );
71
+ result = SevenPoint4 .divide (-7 , -2 );
72
+ System .out .println ("-7 / -2 = " + result );
73
+ }
74
+ catch (Exception e ){
75
+ System .out .println (e .getMessage ());
76
+ }
77
+ }
30
78
31
- /*** 7.6 ***/
32
- System .out .println ("\n *** Test 7.6" );
79
+ public static void test_SevenPoint6 (){
80
+ System .out .println ("\n *** Test 7.6: Find line passing through most provided points " );
33
81
Point [] points = new Point [4 ];
34
82
points [0 ] = new Point (1 , 3 );
35
83
points [1 ] = new Point (2 , 4 );
36
84
points [2 ] = new Point (3 , 5 );
37
85
points [3 ] = new Point (13 , 7 );
38
86
39
- Line bestLine = SevenPoint6 .findBestLine (points );
40
- System .out .println ("bestLine: Slope = " + bestLine . slope + " Intercept = " + bestLine . intercept );
41
-
42
- /*** 7.7 ***/
43
- System . out . println ( " \n *** Test 7.7" );
44
- System .out .println ("\n Solution 1 " );
87
+ Line bestLine = SevenPoint6 .findBestLine (points );
88
+ System .out .println ("bestLine = " + bestLine );
89
+ }
90
+
91
+ public static void test_SevenPoint7 (){
92
+ System .out .println ("\n *** Test 7.7: find kth magic number " );
45
93
for (int i = 1 ; i <= 6 ; i ++){
46
94
System .out .println (i + ": magic number = " + SevenPoint7 .getKthMagicNumber (i ));
47
95
}
48
- System .out .println ("\n Solution 2" );
49
- for (int i = 1 ; i <= 6 ; i ++){
50
- System .out .println (i + ": magic number = " + SevenPoint7 .getKthMagicNumber2 (i ));
51
- }
52
96
}
53
97
}
0 commit comments