Sections
Each week you will complete problem(s) to turn in at your section. These problems will earn you up to 2 out of your 3 section participation points for the week. The other point is awarded for being present in your section and participating in the discussion.
You will not be graded on whether you have a perfect solution, but on whether you have demonstrated effort. Therefore please show some work that demonstrates how you got the answer rather than just writing the answer by itself. We will be somewhat lenient about exactly how the work is shown.
Our intention is that these problems would take you up to 30 minutes each week. If you find yourself taking significantly more than this, you may stop your work and write that you worked for 30 minutes. If you have made significant progress, we will still give you credit for your work.
Section 9: Final practice
(Thu June 6)
Exercises: Solve the following one (1) Self-Check problem on paper and bring your sheet of paper to your section on Thursday:
-
Chapter 9, Self-Check Problem #16 (#13 2nd E.): Inheritance/Polymorphism Mystery p641 (p621 2nd E.). In addition to writing the answer, also show your work by turning in a table of the methods and their output, similar to the one shown on page 603 (585 2nd E.).
Note: the client code uses a for-each loop, described on page 457 (443 2nd E.), to iterate over the array of objects. This is equivalent to the following for loop:
for (int i = 0; i < ponds.length; i++) {
Pond p = ponds[i];
...
}
You are NOT required to know the for-each loop syntax.
Section 8: Classes and objects
(Thu May 30)
Exercises: Solve the following three (3) Self-Check problems on paper and bring your sheet of paper to your section on Thursday:
-
Chapter 8, Self-Check #2: what is an object? p564 (p548 2nd E.). Answer the question in your own words.
-
Chapter 8, Self-Check #18 (#15 2nd E.): problems with constructor p566 (p549 2nd E.). Write the two problems with the constructor shown, and then show a corrected version that does not contain these two problems.
-
Chapter 8, Self-Check #21 (#18 2nd E.): copy constructor p566 (p549 2nd E.). Write a Point
constructor that accepts the parameter shown. You don't need to turn in the whole Point
class, just a written copy of your idea for the constructor code.
May 21 2013 12:30 PM
Section 7: Arrays
(Thu May 23)
Exercises: Solve the following problem on paper and bring
your sheet of paper to your section on Thursday:
-
Array Simulation. You are to simulate the execution of
a method that manipulates an array of integers. Consider the following
method:
public static void mystery(int[] list) {
for (int i = 1; i < list.length - 1; i++) {
if (list[i] > list[i - 1]) {
list[i + 1] = list[i - 1] + list[i + 1];
}
}
}
Below are a list of specific lists of integers. You are to
indicate what values would be stored in the list after
method mystery
executes if the given integer list is passed
as a parameter to mystery
.
{2, 4}
{1, 2, 3}
{2, 2, 2, 2, 2}
{1, 2, 2, 2, 2}
{2, 4, 6, 8}
Show your work by writing the array's initial contents and then crossing
out elements and writing new values as they change.
May 14 2013 12:30 PM
Section 6: File Input/Output
(Thu May 16)
Exercises: Solve the following problem on paper and bring
your sheet of paper to your section on Thursday:
-
Token Based Processing. Write a static method
processData that takes as a parameter a Scanner holding a sequence of
integers and that reports each of the cumulative sums of the sequence
along with the average of the numbers. For example, if the Scanner
contains the following data:
8 4 2 9 7 13 5 9
your method should produce the following output:
Sum of 1 = 8
Sum of 2 = 12
Sum of 3 = 14
Sum of 4 = 23
Sum of 5 = 30
Sum of 6 = 43
Sum of 7 = 48
Sum of 8 = 57
Average = 7.125
Notice that the various lines of output report the sum including just the
first number, then including the first two numbers, then including the
first three numbers, and so on, up to the sum including all numbers. The
final line of output reports the average of the sequence of numbers.
Notice that this is the average of the numbers themselves, not the
average of the cumulative sums.
The amount of output will vary depending upon how many numbers are in the
sequence. For example, if the Scanner contains the following values:
1 2 3 4
the method should produce the following output:
Sum of 1 = 1
Sum of 2 = 3
Sum of 3 = 6
Sum of 4 = 10
Average = 2.5
You are to exactly reproduce the format of these sample outputs. You may
assume that the Scanner has at least one integer to be processed.
May 7 2013 12:30 PM
Section 5.5: Midterm practice
(Thu May 9)
Exercises: Solve the following problem on paper and bring
your sheet of paper to your section on Thursday:
-
Assertions. You will identify various assertions as
being either always true, never true or sometimes true/sometimes false at
various points in program execution. The comments in the method below
indicate the points of interest.
public static int mystery(int x) {
int y = 1;
int z = 0;
// Point A
while (x > y) {
// Point B
z = z + x - y;
x = x / 2;
// Point C
y = y * 2;
// Point D
}
// Point E
return z;
}
Copy the table below onto a sheet of paper and fill it in with the words
ALWAYS, NEVER or SOMETIMES.
|
x > y |
z > 0 |
y % 2 == 0 |
Point A |
|
|
|
Point B |
|
|
|
Point C |
|
|
|
Point D |
|
|
|
Point E |
|
|
|
April 30 2013 12:00 PM
Section 5: while
, Random
, boolean
(Thu May 2)
Exercises: Solve the following problems on paper and bring
your sheet of paper to your section on Thursday:
-
Self-Check 5.4:
while
loop mystery
(p369 3rd Edition, p359 2nd Edition). For each method call, make a table showing the values that
x
and y
have as you execute
the while
loop for that particular call. For example, for
the first two calls, the tables look like this:
mystery(19); mystery(42);
x y x y
-------- --------
19 0 42 0
21 1
You are to write out the tables for the other three cals. This problem is
included in PracticeIt, but PracticeIt doesn't ask for the tables (just the
final output). But you can still use PracticeIt to see
this problem.
-
(depends on Wednesday lecture) Programming Exercise 5.4 (p378 3rd Edition, p369 2nd Edition). You can use PracticeIt
to
solve this problem.
April 23 2013 4:30 PM
Section 4: if/else
, Scanner
, return
(Thu April 24)
Exercises: Solve the following problems on paper and bring
your sheet of paper to your section on Thursday:
-
Programming Exercise 4.1:
fractionSum
(p296 2nd edition, p305 3rd edition). You can use PracticeIt to solve
this
problem.
-
Programming Exercise 4.9 (2nd edition) / 4.12 (3rd edition):
printTriangleType
(p298 2nd edition, p307 3rd edition). You can use PracticeIt
to solve this problem.
April 15 2013 4:30 PM
Section 3: parameters, graphics
(Thu April 18)
Exercises: Solve the following two (2) problems on paper and bring
your sheet of paper to your section on Thursday:
-
Parameter Mystery, Consider the following program:
public class Params {
public static void main(String[] args) {
int x = 15;
int y = 2;
int z = 9;
mystery(x, y, z);
mystery(z, x, y);
mystery(y, z, x);
mystery(x - z, z, z);
}
public static void mystery(int x, int y, int z) {
System.out.println("The " + x + " monkeys ate " + (y + z) + " bananas");
}
}
Make a table that shows what value is being passed to each of x and y and
z for each of the four calls and then indicate the output produced by
the program.
-
Programming Exercise 3G.1
Remember that you can use PracticeIt to
solve this problem.
Section 2: Expressions, for
loops
(Thu April 11)
Exercises: Solve the following three (3) Self-Check problems on paper and bring your sheet of paper to your section on Thursday:
-
Chapter 2, Self-Check Problem #2, solve expressions (a) - (e); from
2 + 3 *
... through (18 - 7) *
...
-
Chapter 2, Self-Check Problem #3, solve expressions (a) - (c); from
4.0 / 2 *
... through 12 / 7 *
...
-
Chapter 2, Self-Check Problem #23, reading for loop code
For the first two problems, please show some work rather than just writing the answer. Write out sub-expressions as you compute their values, and circle or underline operands to show precedence, as is done on page 69-70 of the textbook. You may use a calculator if you want, though one shouldn't be necessary for these problems. For example:
2 + 19 % 5 - 11 * (5 / 2)
2 + 19 % 5 - 11 * 2
2 + 4 - 11 * 2
2 + 4 - 22
6 - 22
-16