Skip to content

Commit cf6b3fe

Browse files
committed
final solutions to chapter 5 exercises
1 parent ee0a529 commit cf6b3fe

9 files changed

Lines changed: 145 additions & 1 deletion

File tree

‎.vscode/launch.json‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch via NPM",
11+
"runtimeExecutable": "npm",
12+
"runtimeArgs": [
13+
"run-script",
14+
"debug"
15+
],
16+
"port": 9229
17+
},
18+
{
19+
"type": "node",
20+
"request": "launch",
21+
"name": "Launch Program",
22+
"program": "${file}"
23+
}
24+
]
25+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Q #3: "rattle"
2+
Q #4: ik
3+
rattle
4+
ping zoop
5+
boo-wa-ha-ha
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
public class BottlesOfBeer {
2+
public static void beerBottles(int num) {
3+
if(num == 0) {
4+
System.out.println("No bottles of beer on the wall,");
5+
System.out.println("no bottles of beer,");
6+
System.out.println("ya' cant take one down, ya can't pass it around,");
7+
System.out.println("'cause thre are no more bottles of beer on the wall!");
8+
} else {
9+
System.out.println(num + " bottles of beer on the wall,");
10+
System.out.println(num + " bottles of beer,");
11+
System.out.println("ya' take one down, ya' pass it around,");
12+
System.out.println(num + " bottles of beer on the wall.");
13+
beerBottles(num -1);
14+
}
15+
}
16+
public static void main(String[] args) {
17+
beerBottles(99);
18+
}
19+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.Scanner;
2+
import java.util.Random;
3+
4+
public class NumberGuess {
5+
public static void main(String[] args) {
6+
Random random = new Random();
7+
int randomNumber = random.nextInt(100) + 1;
8+
findUserNumber(randomNumber);
9+
}
10+
public static void findUserNumber(int randomNumber) {
11+
System.out.println("Please Enter a number between 0 and 100 ");
12+
Scanner in = new Scanner(System.in);
13+
int userNumber = in.nextInt();
14+
System.out.printf("Your number is: %d\n", userNumber);
15+
checkDifference(userNumber, randomNumber);
16+
}
17+
18+
public static void checkDifference(int userNumber, int randomNumber) {
19+
if(userNumber == randomNumber) {
20+
System.out.println("Your number is correct!");
21+
}
22+
else if(userNumber > randomNumber) {
23+
System.out.println("Your number is too high, guess again!");
24+
findUserNumber(randomNumber);
25+
} else {
26+
System.out.println("Your number is too low, guess again!");
27+
findUserNumber(randomNumber);
28+
}
29+
}
30+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public class OneStatement {
2+
public static void main(String[] args){
3+
positiveSingleDigit(4);
4+
}
5+
public static void positiveSingleDigit(int num) {
6+
if(num > 0 && num < 10){
7+
System.out.println("Positive single digit number.");
8+
}
9+
}
10+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class checkFermatExercise {
2+
public static void checkFermat(int a, int b, int c, int n) {
3+
int aPower = (int) Math.pow(a, n);
4+
int bPower = (int) Math.pow(b, n);
5+
int cPower = (int) Math.pow(c, n);
6+
7+
if(n > 2 && aPower + bPower == cPower) {
8+
System.out.println("Holy smokes, Fermat was wrong!");
9+
} else {
10+
System.out.println("No, that doesn't work");
11+
}
12+
}
13+
public static void main(String[] args) {
14+
checkFermat(15, 2, 3, 4);
15+
}
16+
}
17+

‎ch04/PrintTime.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ public static void main(String[] args) {
1212
printTime(hour, minute);
1313
}
1414

15-
}git
15+
}
16+

‎ch05/Countup.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class Countup {
2+
public static void main(String[] args) {
3+
System.out.println("Countup beginning");
4+
countup(3);
5+
}
6+
public static void countup(int n) {
7+
if (n == 0) {
8+
System.out.println("Blastoff!");
9+
} else {
10+
countup(n - 1);
11+
System.out.println(n);
12+
}
13+
}
14+
}

‎npm-debug.log‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
0 info it worked if it ends with ok
2+
1 verbose cli [ 'C:\\Program Files\\nodejs\\node.exe',
3+
1 verbose cli 'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
4+
1 verbose cli 'run-script',
5+
1 verbose cli 'debug' ]
6+
2 info using npm@3.10.10
7+
3 info using node@v6.11.2
8+
4 verbose stack Error: ENOENT: no such file or directory, open 'c:\Users\Chris\Documents\Think Java\ThinkJavaCode\package.json'
9+
4 verbose stack at Error (native)
10+
5 verbose cwd c:\Users\Chris\Documents\Think Java\ThinkJavaCode
11+
6 error Windows_NT 6.3.9600
12+
7 error argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run-script" "debug"
13+
8 error node v6.11.2
14+
9 error npm v3.10.10
15+
10 error path c:\Users\Chris\Documents\Think Java\ThinkJavaCode\package.json
16+
11 error code ENOENT
17+
12 error errno -4058
18+
13 error syscall open
19+
14 error enoent ENOENT: no such file or directory, open 'c:\Users\Chris\Documents\Think Java\ThinkJavaCode\package.json'
20+
15 error enoent ENOENT: no such file or directory, open 'c:\Users\Chris\Documents\Think Java\ThinkJavaCode\package.json'
21+
15 error enoent This is most likely not a problem with npm itself
22+
15 error enoent and is related to npm not being able to find a file.
23+
16 verbose exit [ -4058, true ]

0 commit comments

Comments
 (0)