Skip to content

Commit d061ae5

Browse files
authored
Merge pull request #10 from code-for-tomorrow/exercises
Exercises for Ch. 10-11
2 parents 8c778fd + 46475d0 commit d061ae5

File tree

18 files changed

+1069
-37
lines changed

18 files changed

+1069
-37
lines changed

src/com/codefortomorrow/intermediate/chapter10/practice/RandomNumbers.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ To call the method, type randInt(7, 77) and assign it to
1818
and min to Integer.MAX_VALUE when you first initialize them.
1919
This ensures that when you iterate through the
2020
randomly generated numbers in your array, the max and min
21-
are updated correctly.
22-
(You can also set max to 6 and min to 78.)
21+
are updated correctly. (You can also set max to 6 and min to 78.)
22+
23+
Note: Interested in seeing how the randInt() method works? You can read
24+
this Stack Overflow response: https://bit.ly/38IkeY2
2325
*/
2426

2527
public class RandomNumbers {
@@ -33,8 +35,7 @@ public static void main(String[] args) {
3335
* in the range [min, max]
3436
* @param min minimum random integer
3537
* @param max maximum random integer
36-
* @return a random integer in the range
37-
* [min, max]
38+
* @return a random integer in the range [min, max]
3839
*/
3940
public static int randInt(int min, int max) {
4041
return min + (int) (Math.random() * ((max - min) + 1));

src/com/codefortomorrow/intermediate/chapter10/solutions/RandomNumbers.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ To call the method, type randInt(7, 77) and assign it to
1818
and min to Integer.MAX_VALUE when you first initialize them.
1919
This ensures that when you iterate through the
2020
randomly generated numbers in your array, the max and min
21-
are updated correctly.
22-
(You can also set max to 6 and min to 78.)
21+
are updated correctly. (You can also set max to 6 and min to 78.)
22+
23+
Note: Interested in seeing how the randInt() method works? You can read
24+
this Stack Overflow response: https://bit.ly/38IkeY2
2325
*/
2426

2527
public class RandomNumbers {
@@ -67,8 +69,7 @@ public static void main(String[] args) {
6769
* in the range [min, max]
6870
* @param min minimum random integer
6971
* @param max maximum random integer
70-
* @return a random integer in the range
71-
* [min, max]
72+
* @return a random integer in the range [min, max]
7273
*/
7374
public static int randInt(int min, int max) {
7475
return min + (int) (Math.random() * ((max - min) + 1));
Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
package com.codefortomorrow.intermediate.chapter11.practice;
22

3+
/*
4+
Code a method called average that takes
5+
two double parameters, a and b.
6+
The method should return the average
7+
of the two doubles.
8+
9+
Test if your average method works in the
10+
main method by calling it at least 3 times
11+
with different arguments and printing the results.
12+
13+
Bonus points if you write a JavaDoc comment for
14+
the average() method.
15+
*/
16+
317
public class Average {
418

5-
/**
6-
* Difficulty: 1
7-
*
8-
* Returns the average of two doubles
9-
* @param a the first double
10-
* @param b the second double
11-
* @return average
12-
*/
13-
public static double average(double a, double b) {
14-
return 0.0; // TODO: Fix!
19+
public static void main(String[] args) {
20+
// Test your average method here
1521
}
22+
// Write your average method here
1623
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.codefortomorrow.intermediate.chapter11.practice;
2+
3+
/*
4+
Some websites impose certain rules for passwords.
5+
Write a method that checks whether a string is a valid password.
6+
7+
Suppose the password rules are as follows:
8+
■ A password must have at least eight characters.
9+
■ A password consists of only letters and digits.
10+
■ A password must contain at least two digits.
11+
12+
Write a program that prompts the user to enter a password and displays
13+
Valid Password if the rules are followed or Invalid Password otherwise.
14+
To do this, use a method called isValid which returns true if the password
15+
is valid and false otherwise.
16+
17+
You may also find it helpful to write a method called
18+
isAlphanumeric which returns true if the given string
19+
is only made up of letters and digits.
20+
21+
You may also find it helpful to write a method called
22+
getNumberOfDigits which returns the number of digits
23+
in a given string.
24+
25+
You may also find it helpful to use the Character.isDigit(char)
26+
and Character.isDigit(char) methods. You can read more about them here:
27+
https://www.tutorialspoint.com/java/java_characters.htm
28+
29+
If you don't want to use those methods, you can also use regular
30+
expressions (aka regex) and the matches(regex) String method.
31+
You can read about that here:
32+
https://www.tutorialspoint.com/java/java_string_matches.htm
33+
https://www.vogella.com/tutorials/JavaRegularExpressions/article.html
34+
35+
Bonus points for writing JavaDoc comments for the methods.
36+
37+
Adapted from Exercise 6.18, Introduction to Java Programming (Comprehensive),
38+
10th ed. by Y. Daniel Liang
39+
*/
40+
41+
public class CheckPassword {
42+
43+
public static void main(String[] args) {
44+
// test checkPassword method here
45+
}
46+
// write checkPassword method here
47+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package com.codefortomorrow.intermediate.chapter11.practice;
2+
3+
/*
4+
Credit card numbers follow certain patterns. A credit card number
5+
must have between 13 and 16 digits. It must start with:
6+
■ 4 for Visa cards
7+
■ 5 for Master cards
8+
■ 37 for American Express cards
9+
■ 6 for Discover cards
10+
11+
In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card
12+
numbers. The algorithm is useful to determine whether a card number is entered
13+
correctly or whether a credit card is scanned correctly by a scanner. Credit card
14+
numbers are generated following this validity check, commonly known as the
15+
Luhn check or the Mod 10 check, which can be described as follows
16+
(for illustration, consider the card number 4388576018402626):
17+
18+
1. Double every second digit from right to left. If doubling of a digit results in a
19+
two-digit number, add up the two digits to get a single-digit number.
20+
4388576018402626
21+
2 * 2 = 4
22+
2 * 2 = 4
23+
4 * 2 = 8
24+
1 * 2 = 2
25+
6 * 2 = 12 (1 + 2 = 3)
26+
5 * 2 = 10 (1 + 0 = 1)
27+
8 * 2 = 16 (1 + 6 = 7)
28+
4 * 2 = 8
29+
30+
2. Now add all single-digit numbers from Step 1.
31+
4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37
32+
33+
3. Add all digits in the odd places from right to left in the card number.
34+
6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38
35+
36+
4. Sum the results from Step 2 and Step 3.
37+
37 + 38 = 75
38+
39+
5. If the result from Step 4 is divisible by 10, the card number is valid; otherwise,
40+
it is invalid. For example, the number 4388576018402626 is invalid, but the
41+
number 4388576018410707 is valid.
42+
43+
Write a program that prompts the user to enter a credit card number as a long
44+
integer. Display whether the number is valid or invalid. Design your program to
45+
use the following methods:
46+
47+
Returns true if the card number is valid
48+
public static boolean isValid(long number)
49+
50+
Gets the result from Step 2
51+
public static int sumOfDoubleEvenPlace(long number)
52+
53+
Returns this number if it is a single digit, otherwise,
54+
return the sum of the two digits
55+
public static int getDigit(int number)
56+
57+
Returns sum of odd-place digits in number
58+
public static int sumOfOddPlace(long number)
59+
60+
Returns true if the digit d is a prefix for number
61+
public static boolean prefixMatched(long number, int d)
62+
63+
Returns the number of digits in d
64+
public static int getSize(long d)
65+
66+
Returns the first k number of digits from number. If the
67+
number of digits in number is less than k, return number.
68+
public static long getPrefix(long number, int k)
69+
70+
Here are sample runs of the program:
71+
72+
Enter a credit card number: 4388576018410707
73+
4388576018410707 is valid
74+
75+
Enter a credit card number: 4388576018402626
76+
4388576018402626 is invalid
77+
78+
Bonus points if you can also print to the user
79+
what type of card the number is, and for writing JavaDoc comments.
80+
81+
Note: If you need more card numbers to test, you can look here:
82+
https://www.simplify.com/commerce/docs/testing/test-card-numbers
83+
(or simply search "test credit card numbers" on Google).
84+
85+
Note: The practice template has method stubs written for you.
86+
Simply delete the default return values and start coding!
87+
88+
Adapted from Exercise 6.31, Introduction to Java Programming (Comprehensive),
89+
10th ed. by Y. Daniel Liang
90+
*/
91+
92+
public class CreditCard {
93+
94+
public static void main(String[] args) {
95+
// write code here
96+
}
97+
98+
/** Returns true if the card number is valid */
99+
public static boolean isValid(long number) {
100+
return false;
101+
}
102+
103+
/** Returns the sum of the doubled even-place digits, from right to left */
104+
public static int sumOfDoubleEvenPlace(long number) {
105+
return 0;
106+
}
107+
108+
/**
109+
* Returns the given number if it is a single digit,
110+
* otherwise return the sum of the two digits
111+
*/
112+
public static int getDigit(int n) {
113+
return 0;
114+
}
115+
116+
/** Returns the sum of the odd-place digits, from right to left */
117+
public static int sumOfOddPlace(long number) {
118+
return 0;
119+
}
120+
121+
/** Return true if d is a prefix for a number */
122+
public static boolean prefixMatched(long number, int d) {
123+
return false;
124+
}
125+
126+
/** Returns the number of digits in the given number */
127+
public static int getSize(long number) {
128+
return 0;
129+
}
130+
131+
/**
132+
* Returns the first k number of digits from number.
133+
* If the number of digits in number is less than k,
134+
* returns number.
135+
*/
136+
public static long getPrefix(long number, int k) {
137+
return 0;
138+
}
139+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.codefortomorrow.intermediate.chapter11.practice;
2+
3+
/*
4+
Write a method called isPrime which returns
5+
true if the given integer is prime and false otherwise.
6+
Bonus points for writing JavaDoc comments for the method.
7+
8+
Test your isPrime method in the main method by
9+
calling it at least 3 times and printing the result.
10+
11+
Solution from Listing 6.7, Introduction to Java Programming (Comprehensive),
12+
10th ed. by Y. Daniel Liang
13+
*/
14+
15+
public class IsPrime {
16+
17+
public static void main(String[] args) {
18+
// test isPrime method here
19+
}
20+
// write isPrime method here
21+
}
Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
package com.codefortomorrow.intermediate.chapter11.practice;
22

3+
/*
4+
Code a method printEvens that takes an integer parameter n.
5+
The method should print the first n even numbers to the console.
6+
(Note that 0 is considered an even number.)
7+
8+
Test if your printEvens method works in the main method
9+
by calling it at least 3 times with different arguments.
10+
11+
Bonus points if you write a JavaDoc comment
12+
for the printEvens method.
13+
*/
14+
315
public class PrintEvens {
416

5-
/**
6-
* Difficulty: 1
7-
*
8-
* Print the first n even integers
9-
* (consider 0 an even number)
10-
* @param n number of even integers to print
11-
*/
12-
public static void printEvens(int n) {
13-
// write code here
17+
public static void main(String[] args) {
18+
// Test your printEvens method here
1419
}
20+
// Write your printEvens method here
1521
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.codefortomorrow.intermediate.chapter11.practice;
2+
3+
/*
4+
Write a method that computes the sum of the digits
5+
in an integer. Use the following method header:
6+
7+
public static int sumDigits(long n)
8+
9+
For example, sumDigits(234) returns 9 (because 2 + 3 + 4).
10+
11+
Hint: Use the % operator to extract digits,
12+
and the / operator to remove the extracted digit.
13+
For instance, to extract 4 from 234, use 234 % 10 (= 4).
14+
To remove 4 from 234, use 234 / 10 (= 23).
15+
16+
Use a loop to repeatedly extract and remove the digit
17+
until all the digits are extracted.
18+
19+
Write a test program that prompts the user to enter an integer and
20+
displays the sum of all its digits.
21+
22+
Bonus points for writing a JavaDoc comment for the sumDigits method.
23+
24+
Adapted from Exercise 6.2, Introduction to Java Programming (Comprehensive),
25+
10th ed. by Y. Daniel Liang
26+
*/
27+
28+
public class SumDigits {
29+
30+
public static void main(String[] args) {
31+
// test sumDigits here
32+
}
33+
// write sumDigits method here
34+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.codefortomorrow.intermediate.chapter11.practice;
2+
3+
/*
4+
Write a class that contains the following two methods:
5+
6+
Convert from Celsius to Fahrenheit
7+
public static double celsiusToFahrenheit(double celsius)
8+
9+
Convert from Fahrenheit to Celsius
10+
public static double fahrenheitToCelsius(double fahrenheit)
11+
12+
The formula for the conversion is:
13+
fahrenheit = (9.0 / 5) * celsius + 32
14+
celsius = (5.0 / 9) * (fahrenheit – 32)
15+
16+
Bonus points for writing JavaDoc comments for both methods.
17+
18+
Write a test program that asks the user for a temperature
19+
in Fahrenheit and prints out the Celsius conversion. Then
20+
ask the user for a temperature in Celsius and print out
21+
the Fahrenheit conversion.
22+
23+
Adapted from Exercise 6.8, Introduction to Java Programming (Comprehensive),
24+
10th ed. by Y. Daniel Liang
25+
*/
26+
27+
public class Temperature {
28+
29+
public static void main(String[] args) {
30+
// Test your methods here
31+
}
32+
// Write your methods here
33+
}

0 commit comments

Comments
 (0)