Skip to content

Commit 27663f6

Browse files
committed
Add Ch 14 practice problems
1 parent 9e30ab7 commit 27663f6

File tree

6 files changed

+404
-0
lines changed

6 files changed

+404
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.codefortomorrow.advanced.chapter14.practice;
2+
import java.io.*;
3+
4+
/*
5+
The file “numbers.txt” has 100 random numbers
6+
(one on each line). Use file i/o to find the
7+
average of these numbers. Write “Average: ”
8+
and display the average on the next (101st)
9+
line of the file.
10+
11+
Hint: Just like BufferedReader and FileReader,
12+
there are BufferedWriter and FileWriter classes
13+
that allow you to add full words to files,
14+
instead of just bytes. You should do a quick
15+
Google search to explore these classes first,
16+
but just to get you started:
17+
18+
new FileWriter(“fileName.txt”, true)
19+
20+
will create an instance of FileWriter, and
21+
the “true” puts it in append mode, so you
22+
start writing to the end of the file instead
23+
of writing over the existing content from the beginning.
24+
*/
25+
26+
public class Average {
27+
public static void main(String[] args) {
28+
29+
// Add code here.
30+
}
31+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.codefortomorrow.advanced.chapter14.practice;
2+
3+
/*
4+
Create a class called BankAccount (a separate
5+
class from your main program) that contains a
6+
private “balance” attribute for the amount of
7+
money in the account.
8+
9+
The user should be able to set the original
10+
balance upon initialization (through the constructor).
11+
The class should have deposit and withdraw methods
12+
that add and subtract money from the account.
13+
There should also be a getter method to access
14+
the balance, since the attribute is private.
15+
16+
Create at least 2 instances of BankAccount.
17+
Deposit, withdraw, and display the final balance of each.
18+
19+
Create a NotEnoughMoneyException class that is a
20+
checked exception. The withdraw method should throw
21+
this exception if the user tries to withdraw an
22+
amount that is greater than their current balance.
23+
Handle this exception by displaying the difference
24+
between the balance and the amount to be withdrawn,
25+
and do this exception handling in the main program
26+
where you have created instances of BankAccount.
27+
*/
28+
29+
public class Bank {
30+
31+
public static void main(String[] args) {
32+
33+
// Your code here.
34+
}
35+
}
36+
37+
// Add other classes here.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
35
2+
94
3+
16
4+
38
5+
82
6+
87
7+
52
8+
60
9+
86
10+
59
11+
72
12+
7
13+
60
14+
56
15+
63
16+
11
17+
67
18+
77
19+
67
20+
82
21+
28
22+
13
23+
100
24+
92
25+
85
26+
69
27+
75
28+
6
29+
83
30+
100
31+
76
32+
51
33+
13
34+
84
35+
59
36+
30
37+
21
38+
72
39+
15
40+
28
41+
15
42+
95
43+
87
44+
61
45+
80
46+
22
47+
45
48+
9
49+
73
50+
85
51+
85
52+
2
53+
90
54+
75
55+
60
56+
80
57+
91
58+
21
59+
80
60+
41
61+
82
62+
27
63+
48
64+
43
65+
37
66+
52
67+
78
68+
21
69+
54
70+
20
71+
15
72+
49
73+
42
74+
89
75+
63
76+
24
77+
10
78+
62
79+
44
80+
63
81+
51
82+
34
83+
36
84+
21
85+
2
86+
49
87+
64
88+
77
89+
34
90+
80
91+
7
92+
83
93+
51
94+
46
95+
11
96+
25
97+
59
98+
96
99+
21
100+
65
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.codefortomorrow.advanced.chapter14.solutions;
2+
import java.io.*;
3+
4+
/*
5+
The file “numbers.txt” has 100 random numbers
6+
(one on each line). Use file i/o to find the
7+
average of these numbers. Write “Average: ”
8+
and display the average on the next (101st)
9+
line of the file.
10+
11+
Hint: Just like BufferedReader and FileReader,
12+
there are BufferedWriter and FileWriter classes
13+
that allow you to add full words to files,
14+
instead of just bytes. You should do a quick
15+
Google search to explore these classes first,
16+
but just to get you started:
17+
18+
new FileWriter(“fileName.txt”, true)
19+
20+
will create an instance of FileWriter, and
21+
the “true” puts it in append mode, so you
22+
start writing to the end of the file instead
23+
of writing over the existing content from the beginning.
24+
*/
25+
26+
public class Average {
27+
public static void main(String[] args) {
28+
File file = new File("numbers.txt");
29+
30+
// try with resources block, which closes streams automatically
31+
try(BufferedReader in = new BufferedReader(
32+
new FileReader(file)); BufferedWriter out = new BufferedWriter(
33+
new FileWriter(file, true))) {
34+
35+
// read the numbers (String input) and find average
36+
double average = 0;
37+
String contentLine = in.readLine();
38+
while (contentLine != null) {
39+
average += Integer.parseInt(contentLine);
40+
contentLine = in.readLine();
41+
}
42+
average /= 100.;
43+
44+
// append average to end of file
45+
out.write("Average: " + average);
46+
} catch (IOException e) {
47+
e.printStackTrace();
48+
}
49+
}
50+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.codefortomorrow.advanced.chapter14.solutions;
2+
3+
/*
4+
Create a class called BankAccount (a separate
5+
class from your main program) that contains a
6+
private “balance” attribute for the amount of
7+
money in the account.
8+
9+
The user should be able to set the original
10+
balance upon initialization (through the constructor).
11+
The class should have deposit and withdraw methods
12+
that add and subtract money from the account.
13+
There should also be a getter method to access
14+
the balance, since the attribute is private.
15+
16+
Create at least 2 instances of BankAccount.
17+
Deposit, withdraw, and display the final balance of each.
18+
19+
Create a NotEnoughMoneyException class that is a
20+
checked exception. The withdraw method should throw
21+
this exception if the user tries to withdraw an
22+
amount that is greater than their current balance.
23+
Handle this exception by displaying the difference
24+
between the balance and the amount to be withdrawn,
25+
and do this exception handling in the main program
26+
where you have created instances of BankAccount.
27+
*/
28+
29+
public class Bank {
30+
31+
public static void main(String[] args) {
32+
BankAccount account1 = new BankAccount(500);
33+
BankAccount account2 = new BankAccount(1000);
34+
35+
account1.deposit(200);
36+
withdraw(account1, 300);
37+
System.out.println("Account 1: " + account1.getBalance());
38+
39+
account2.deposit(200);
40+
withdraw(account2, 1500);
41+
System.out.println("Account 2: " + account2.getBalance());
42+
}
43+
44+
public static void withdraw(BankAccount account, int amount) {
45+
try {
46+
account.withdraw(amount);
47+
} catch (NotEnoughMoneyException e) {
48+
int diff = Math.abs(amount - account.getBalance());
49+
System.out.println(e.toString());
50+
}
51+
}
52+
}
53+
54+
55+
class BankAccount {
56+
private int balance;
57+
58+
public BankAccount(int balance) {
59+
this.balance = balance;
60+
}
61+
62+
public void deposit(int amount) {
63+
balance += amount;
64+
}
65+
66+
public void withdraw(int amount) throws NotEnoughMoneyException {
67+
if(amount > balance)
68+
throw new NotEnoughMoneyException(
69+
"Bank balance is short $" + Math.abs(balance - amount));
70+
balance -= amount;
71+
}
72+
73+
public int getBalance() {
74+
return balance;
75+
}
76+
}
77+
78+
79+
class NotEnoughMoneyException extends Exception {
80+
81+
public NotEnoughMoneyException() {}
82+
83+
public NotEnoughMoneyException(String message) {
84+
super(message);
85+
}
86+
}

0 commit comments

Comments
 (0)