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.
Exercises: Solve the following problem on paper and bring your sheet of paper to your section on .
public class George extends Sally { public void method2() { System.out.println("george 2"); } } public class Fred { public void method1() { System.out.println("fred 1"); } public void method2() { System.out.println("fred 2"); } public String toString() { return "fred"; } } public class Harold extends Sally { public String toString() { return "harold"; } } public class Sally extends Fred { public void method1() { System.out.println("sally 1"); } public String toString() { return "sally"; } }
Consider the following code fragment:
Fred[] elements = {new Sally(), new Fred(), new George(), new Harold()}; for (int i = 0; i < elements.length; i++) { System.out.println(elements[i]); elements[i].method1(); elements[i].method2(); System.out.println(); }
What output is produced by this code? (you can write the output as a series of rows; each row has 3 columns, the end of the for
loop starts a new row)
Exercises: Solve the following problem on paper and bring your sheet of paper to your section on .
public class ReferenceMystery3 { public static void main(String[] args) { int a = 7; int b = 9; Point p1 = new Point(2, 2); Point p2 = new Point(2, 2); addToXTwice(a, p1); System.out.println(a + " " + b + " " + p1.x + " " + p2.x); addToXTwice(b, p2); System.out.println(a + " " + b + " " + p1.x + " " + p2.x); } public static void addToXTwice(int a, Point p1) { a = a + a; p1.x = a; System.out.println(a + " " + p1.x); } }
public class Point { int x; int y; // The constructor: public void Point(int initialX, int initialY) { int x = initialX; int y = initialY; } }
Exercises: Solve the following problem on paper and bring your sheet of paper to your section on .
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.
Exercises: Solve the following problem on paper and bring your sheet of paper to your section on .
8 4 2 9 7 13 5 9your 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.125Notice 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 4the method should produce the following output:
Sum of 1 = 1 Sum of 2 = 3 Sum of 3 = 6 Sum of 4 = 10 Average = 2.5You are to exactly reproduce the format of these sample outputs. You may assume that the Scanner has at least one integer to be processed.
Exercises: Solve the following problem on paper and bring your sheet of paper to your section on .
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 |
while
, Random
, boolean
Exercises: Solve the following problems on paper and bring your sheet of paper to your section on .
while
loop mystery (page number?, PracticeIt)
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
Write out similar tables for the other three calls: mystery(48)
, mystery(40)
, mystery(64)
.
Write a method named randomX
that keeps printing lines, where each line contains a random number of x
characters between 5 and 19 inclusive, until it prints a line with 16 or more characters. For example, the output from your method might be the following. Notice that the last line has 17 x characters.
xxxxxxx xxxxxxxxxxxxx xxxxxx xxxxxxxxxxx xxxxxxxxxxxxxxxxx
if/else
, Scanner
, return
Exercises: Solve the following problems on paper and bring your sheet of paper to your section on .
fractionSum
(BJP3 p305; PracticeIt)
Write a method called fractionSum
that accepts an integer parameter n and returns as a double
the sum of the first n terms of the sequence:
1 + (1/2) + (1/3) + (1/4) + (1/5) + ...
For example, fractionSum(2)
should return 1.5
because the sum of the first 2 terms of the sequence (1 + 1/2) is 1.5
.
You may assume that the parameter n is non-negative.
printTriangleType
(BJP3 p307; PracticeIt)
Write a method called printTriangleType
that accepts three integer arguments representing the lengths of the sides of a triangle and prints what type of triangle it is. The three types are equilateral, isosceles, and scalene. An equilateral triangle has all three sides the same length, an isosceles triangle has two sides the same length, and a scalene triangle has three sides of different lengths. Here are some example calls to printTriangleType
:
printTriangleType(5, 7, 7); printTriangleType(6, 6, 6); printTriangleType(5, 7, 8); printTriangleType(12, 18, 12);
The output produced should be the following:
isosceles equilateral scalene isosceles
Exercises: Solve the following problems on paper and bring your sheet of paper to your section on .
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.
Write a complete program that generates a DrawingPanel with the following shapes and specifications:
(You don't need to include any import
statements at the top of your program.)
for
loops
Exercises: Solve the following problems on paper and bring your sheet of paper to your section on .
Solve the following expressions (see note below):
14 / 7 * 2 + 30 / 5 + 1 (12 + 3) / 4 * 2 813 % 100 / 3 + 2.4
Solve the following expressions (see note below):
4.0 / 2 * 9 / 2 2.5 * 2 + 8 / 5.0 + 10 / 3
Write for
loops to produce the following output:
1 22 333 4444 55555 666666 7777777
NOTE: For the expression problems, show how the expression is evaluated step-by-step, as is done on page 62 of the textbook. You should have a different line for each different operator that needs to be evaluated. Below is an example of what we expect:
2 + 19 % 5 - (11 * (5 / 2)) 2 + 19 % 5 - (11 * 2 ) 2 + 4 - (11 * 2 ) 2 + 4 - 22 6 - 22 -16
complete by 8:30AM,