Skip to content

Commit 911e9bb

Browse files
committed
Add instructions, comments to Average and Print Evens
Clarified instructions, added main method for students to test their methods. Also deleted method blocks so that students can practice declaring methods. Also deleted JavaDoc comments to let students practice writing JavaDoc comments.
1 parent 0aa888c commit 911e9bb

File tree

4 files changed

+92
-29
lines changed

4 files changed

+92
-29
lines changed
Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
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+
23+
// Write your average method here
1624
}
Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
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+
21+
// Write your printEvens method here
1522
}

src/com/codefortomorrow/intermediate/chapter11/solutions/Average.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
package com.codefortomorrow.intermediate.chapter11.solutions;
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

19+
public static void main(String[] args) {
20+
// test the average method
21+
System.out.println(average(5, 4)); // prints 4.5
22+
System.out.println(average(0, 10)); // prints 5.0
23+
System.out.println(average(1.5, 3.75)); // prints 2.625
24+
}
25+
526
/**
6-
* Difficulty: 1
7-
*
827
* Returns the average of two doubles
928
* @param a the first double
1029
* @param b the second double
11-
* @return average
30+
* @return the average of two doubles
1231
*/
1332
public static double average(double a, double b) {
1433
return (a + b) / 2;
Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,52 @@
11
package com.codefortomorrow.intermediate.chapter11.solutions;
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

17+
public static void main(String[] args) {
18+
// test printEvens method
19+
printEvens(5); // prints 0 2 4 6 8
20+
printEvens(10); // prints 0 2 4 6 8 10 12 14 16 18
21+
printEvens(0); // prints nothing
22+
}
23+
524
/**
6-
* Difficulty: 2
7-
*
8-
* Print the first n even integers
9-
* (consider 0 an even number)
10-
* @param n number of even integers to print
25+
* Prints the first n even integers
26+
* @param n number of even integers to print
1127
*/
1228
public static void printEvens(int n) {
13-
int evens = 0;
14-
int i = 0;
29+
int evens = 0; // number of evens printed
30+
int i = 0; // current number
1531
while (evens < n) {
32+
// if current number is even, print it
1633
if (i % 2 == 0) {
1734
System.out.print(i + " ");
1835
evens++;
1936
}
2037
i++;
2138
}
39+
System.out.println(); // move cursor to next line
40+
41+
// alternate solution using for loop
42+
// int numberOfEvensPrinted = 0;
43+
// for (int num = 0; numberOfEvensPrinted < n; num++) {
44+
// // if the number is even, print it
45+
// if (num % 2 == 0) {
46+
// System.out.print(num + " ");
47+
// numberOfEvensPrinted++;
48+
// }
49+
// }
50+
// System.out.println(); // move cursor to next line
2251
}
2352
}

0 commit comments

Comments
 (0)