Skip to content

Commit c9e4bde

Browse files
committed
rearranged ch04-ch08
1 parent 8e04a1f commit c9e4bde

23 files changed

Lines changed: 167 additions & 169 deletions
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Examples from Chapter 5.
2+
* Examples from Chapter 4.
33
*/
44
public class Conditional {
55

ch04/Logarithm.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
* Demonstrates input validation using if statements.
5+
*/
6+
public class Logarithm {
7+
8+
public static void main(String[] args) {
9+
10+
// prompt for input
11+
Scanner in = new Scanner(System.in);
12+
System.out.print("Enter a number: ");
13+
14+
// check the format
15+
if (!in.hasNextDouble()) {
16+
String word = in.next();
17+
System.err.println(word + " is not a number");
18+
return;
19+
}
20+
21+
// check the range
22+
double x = in.nextDouble();
23+
if (x >= 0) {
24+
double y = Math.log(x);
25+
System.out.println("The log is " + y);
26+
} else {
27+
System.out.println("The log is undefined");
28+
}
29+
}
30+
}
31+

ch05/Buzz.java renamed to ch05/Baffle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
public class Buzz {
1+
public class Baffle {
22

33
public static void baffle(String blimp) {
44
System.out.println(blimp);
Lines changed: 13 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
/**
2-
* Examples from Chapter 6.
2+
* Examples from Chapter 5.
33
*/
4-
public class Series {
4+
public class Circle {
55

6-
public static void countup(int n) {
7-
if (n == 0) {
8-
System.out.println("Blastoff!");
6+
public static double absoluteValue(double x) {
7+
if (x < 0) {
8+
return -x;
99
} else {
10-
countup(n - 1);
11-
System.out.println(n);
10+
return x;
1211
}
1312
}
1413

@@ -21,14 +20,6 @@ public static double calculateArea2(double radius) {
2120
return Math.PI * radius * radius;
2221
}
2322

24-
public static double absoluteValue(double x) {
25-
if (x < 0) {
26-
return -x;
27-
} else {
28-
return x;
29-
}
30-
}
31-
3223
public static double distance
3324
(double x1, double y1, double x2, double y2) {
3425
double dx = x2 - x1;
@@ -68,59 +59,17 @@ public static double absoluteValue(double x) {
6859
return calculateArea(distance(xc, yc, xp, yp));
6960
}
7061

71-
/**
72-
* Tests whether x is a single digit integer.
73-
*
74-
* @param x the integer to test
75-
* @return true if x has one digit, false otherwise
76-
*/
77-
public static boolean isSingleDigit(int x) {
78-
if (x > -10 && x < 10) {
79-
return true;
80-
} else {
81-
return false;
82-
}
83-
}
84-
85-
public static boolean isSingleDigit2(int x) {
86-
return x > -10 && x < 10;
87-
}
88-
89-
public static int factorial(int n) {
90-
if (n == 0) {
91-
return 1;
92-
}
93-
int recurse = factorial(n - 1);
94-
int result = n * recurse;
95-
return result;
96-
}
97-
98-
public static int fibonacci(int n) {
99-
if (n == 1 || n == 2) {
100-
return 1;
101-
}
102-
return fibonacci(n - 1) + fibonacci(n - 2);
103-
}
104-
10562
public static void main(String[] args) {
106-
countup(3);
107-
System.out.println("Have a nice day.");
63+
64+
System.out.println("absolute value");
65+
System.out.println(absoluteValue(-2));
10866

10967
System.out.println("calculateArea");
11068
System.out.println(calculateArea(3.0));
11169

11270
System.out.println("calculateArea2");
11371
System.out.println(calculateArea2(3.0));
11472

115-
System.out.println("circleArea");
116-
System.out.println(circleArea(1.0, 2.0, 4.0, 6.0));
117-
118-
System.out.println("calculateArea with 4 doubles");
119-
System.out.println(calculateArea(1.0, 2.0, 4.0, 6.0));
120-
121-
System.out.println("absolute value");
122-
System.out.println(absoluteValue(-2));
123-
12473
System.out.println("distance");
12574
System.out.println(distance(1.0, 2.0, 4.0, 6.0));
12675

@@ -130,21 +79,12 @@ public static void main(String[] args) {
13079
System.out.println("distance3");
13180
System.out.println(distance3(1.0, 2.0, 4.0, 6.0));
13281

133-
System.out.println(isSingleDigit(2));
134-
boolean bigFlag = !isSingleDigit2(17);
135-
136-
int z = 9;
137-
if (isSingleDigit(z)) {
138-
System.out.println("z is small");
139-
} else {
140-
System.out.println("z is big");
141-
}
82+
System.out.println("circleArea");
83+
System.out.println(circleArea(1.0, 2.0, 4.0, 6.0));
14284

143-
System.out.println("factorial");
144-
System.out.println(factorial(3));
85+
System.out.println("calculateArea with 4 doubles");
86+
System.out.println(calculateArea(1.0, 2.0, 4.0, 6.0));
14587

146-
System.out.println("fibonacci");
147-
System.out.println(fibonacci(3));
14888
}
14989

15090
}

ch05/Hoopy.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
public class Hoopy {
2+
3+
public static void main(String[] args) {
4+
boolean flag1 = isHoopy(202);
5+
boolean flag2 = isFrabjuous(202);
6+
System.out.println(flag1);
7+
System.out.println(flag2);
8+
if (flag1 && flag2) {
9+
System.out.println("ping!");
10+
}
11+
if (flag1 || flag2) {
12+
System.out.println("pong!");
13+
}
14+
}
15+
16+
public static boolean isHoopy(int x) {
17+
boolean hoopyFlag;
18+
if (x % 2 == 0) {
19+
hoopyFlag = true;
20+
} else {
21+
hoopyFlag = false;
22+
}
23+
return hoopyFlag;
24+
}
25+
26+
public static boolean isFrabjuous(int x) {
27+
boolean frabjuousFlag;
28+
if (x > 0) {
29+
frabjuousFlag = true;
30+
} else {
31+
frabjuousFlag = false;
32+
}
33+
return frabjuousFlag;
34+
}
35+
36+
}

ch05/Logarithm.java

Lines changed: 0 additions & 44 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
public class Exercise {
1+
public class Wugga {
22

33
public static void zoop() {
44
baffle();

0 commit comments

Comments
 (0)