Skip to content

Commit 6b00a8b

Browse files
committed
read through ch05
1 parent c560ff5 commit 6b00a8b

File tree

4 files changed

+153
-111
lines changed

4 files changed

+153
-111
lines changed

ch05/Conditional.java

Lines changed: 10 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,33 @@
44
* Examples from Chapter 5.
55
*/
66
public class Conditional {
7-
7+
88
public static void main(String[] args) {
99
String fruit1 = "Apple";
1010
String fruit2 = "Orange";
1111
System.out.println(fruit1.equals(fruit2));
12-
12+
1313
int x = 17;
1414
int n = 18;
15-
15+
1616
if (x > 0) {
1717
System.out.println("x is positive");
1818
}
19-
19+
2020
if (x % 2 == 0) {
2121
System.out.println("x is even");
2222
} else {
2323
System.out.println("x is odd");
2424
}
25-
25+
2626
if (x > 0) {
2727
System.out.println("x is positive");
2828
} else if (x < 0) {
2929
System.out.println("x is negative");
3030
} else {
3131
System.out.println("x is zero");
3232
}
33-
33+
3434
if (x == 0) {
3535
System.out.println("x is zero");
3636
} else {
@@ -40,118 +40,17 @@ public static void main(String[] args) {
4040
System.out.println("x is negative");
4141
}
4242
}
43+
4344
boolean evenFlag = (n % 2 == 0); // true if n is even
4445
boolean positiveFlag = (x > 0); // true if x is positive
45-
46+
4647
if (evenFlag) {
4748
System.out.println("n was even when I checked it");
4849
}
49-
50+
5051
if (!evenFlag) {
5152
System.out.println("n was odd when I checked it");
5253
}
53-
54-
System.out.println("printLogarithm");
55-
printLogarithm(3.0);
56-
57-
System.out.println("countdown");
58-
countdown(3);
59-
60-
System.out.println("countup");
61-
countup(3);
62-
63-
System.out.println("newLine");
64-
newLine();
65-
66-
System.out.println("nLines");
67-
nLines(3);
68-
69-
System.out.println("threeLine");
70-
threeLine();
71-
72-
System.out.println("displayBinary");
73-
displayBinary(23);
74-
System.out.println();
75-
76-
System.out.println("scandouble");
77-
scanDouble();
78-
79-
System.out.println("scandouble2");
80-
scanDouble2();
81-
}
82-
83-
public static void printLogarithm(double x) {
84-
if (x <= 0.0) {
85-
System.err.println("Error: x must be positive.");
86-
return;
87-
}
88-
double result = Math.log(x);
89-
System.out.println("The log of x is " + result);
90-
}
91-
92-
public static void scanDouble() {
93-
Scanner in = new Scanner(System.in);
94-
System.out.print("Enter a number: ");
95-
double x = in.nextDouble();
96-
printLogarithm(x);
97-
}
98-
99-
public static void scanDouble2() {
100-
Scanner in = new Scanner(System.in);
101-
System.out.print("Enter a number: ");
102-
if (!in.hasNextDouble()) {
103-
String word = in.next();
104-
System.err.println(word + " is not a number");
105-
return;
106-
}
107-
double x = in.nextDouble();
108-
printLogarithm(x);
109-
}
110-
111-
public static void countdown(int n) {
112-
if (n == 0) {
113-
System.out.println("Blastoff!");
114-
} else {
115-
System.out.println(n);
116-
countdown(n - 1);
117-
}
118-
}
119-
120-
public static void newLine() {
121-
System.out.println();
122-
}
123-
124-
public static void threeLine() {
125-
newLine();
126-
newLine();
127-
newLine();
128-
}
129-
130-
public static void nLines(int n) {
131-
if (n > 0) {
132-
System.out.println();
133-
nLines(n - 1);
134-
}
135-
}
136-
137-
public static void forever(String s) {
138-
System.out.println(s);
139-
forever(s);
140-
}
141-
142-
public static void countup(int n) {
143-
if (n == 0) {
144-
System.out.println("Blastoff!");
145-
} else {
146-
countup(n - 1);
147-
System.out.println(n);
148-
}
149-
}
150-
151-
public static void displayBinary(int value) {
152-
if (value > 0) {
153-
displayBinary(value / 2);
154-
System.out.print(value % 2);
155-
}
15654
}
55+
15756
}

ch05/Exercise.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
public class Exercise {
2+
3+
public static void zoop(String fred, int bob) {
4+
System.out.println(fred);
5+
if (bob == 5) {
6+
ping("not ");
7+
} else {
8+
System.out.println("!");
9+
}
10+
}
11+
12+
public static void main(String[] args) {
13+
int bizz = 5;
14+
int buzz = 2;
15+
zoop("just for", bizz);
16+
clink(2 * buzz);
17+
}
18+
19+
public static void clink(int fork) {
20+
System.out.print("It's ");
21+
zoop("breakfast ", fork) ;
22+
}
23+
24+
public static void ping(String strangStrung) {
25+
System.out.println("any " + strangStrung + "more ");
26+
}
27+
28+
}

ch05/Logarithm.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.util.Scanner;
2+
3+
public class Logarithm {
4+
5+
public static void main(String[] args) {
6+
System.out.println("printLogarithm");
7+
printLogarithm(3.0);
8+
9+
System.out.println("scandouble");
10+
scanDouble();
11+
12+
System.out.println("scandouble2");
13+
scanDouble2();
14+
}
15+
16+
public static void printLogarithm(double x) {
17+
if (x <= 0.0) {
18+
System.err.println("Error: x must be positive.");
19+
return;
20+
}
21+
double result = Math.log(x);
22+
System.out.println("The log of x is " + result);
23+
}
24+
25+
public static void scanDouble() {
26+
Scanner in = new Scanner(System.in);
27+
System.out.print("Enter a number: ");
28+
double x = in.nextDouble();
29+
printLogarithm(x);
30+
}
31+
32+
public static void scanDouble2() {
33+
Scanner in = new Scanner(System.in);
34+
System.out.print("Enter a number: ");
35+
if (!in.hasNextDouble()) {
36+
String word = in.next();
37+
System.err.println(word + " is not a number");
38+
return;
39+
}
40+
double x = in.nextDouble();
41+
printLogarithm(x);
42+
}
43+
44+
}

ch05/Recursion.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
public class Recursion {
2+
3+
public static void main(String[] args) {
4+
System.out.println("countdown");
5+
countdown(3);
6+
7+
System.out.println("countup");
8+
countup(3);
9+
10+
System.out.println("newLine");
11+
newLine();
12+
13+
System.out.println("nLines");
14+
nLines(3);
15+
16+
System.out.println("threeLine");
17+
threeLine();
18+
19+
System.out.println("displayBinary");
20+
displayBinary(23);
21+
System.out.println();
22+
}
23+
24+
public static void countdown(int n) {
25+
if (n == 0) {
26+
System.out.println("Blastoff!");
27+
} else {
28+
System.out.println(n);
29+
countdown(n - 1);
30+
}
31+
}
32+
33+
public static void newLine() {
34+
System.out.println();
35+
}
36+
37+
public static void threeLine() {
38+
newLine();
39+
newLine();
40+
newLine();
41+
}
42+
43+
public static void nLines(int n) {
44+
if (n > 0) {
45+
System.out.println();
46+
nLines(n - 1);
47+
}
48+
}
49+
50+
public static void forever(String s) {
51+
System.out.println(s);
52+
forever(s);
53+
}
54+
55+
public static void countup(int n) {
56+
if (n == 0) {
57+
System.out.println("Blastoff!");
58+
} else {
59+
countup(n - 1);
60+
System.out.println(n);
61+
}
62+
}
63+
64+
public static void displayBinary(int value) {
65+
if (value > 0) {
66+
displayBinary(value / 2);
67+
System.out.print(value % 2);
68+
}
69+
}
70+
71+
}

0 commit comments

Comments
 (0)