Each week we will assign a written homework assignment to be turned in and discussed in section. These are meant as "warm up" problems to get you thinking about the topics we cover that week. It will be graded for effort, not for whether or not you have the right answers. You will receive 3 points for each written assignment you bring to section, up to a maximum of 18 points. The points are for the combination of completing the assignment and attending section. You won't get any points for just attending section or just doing the written assignment. As a guideline, we expect you to spend about 20 to 30 minutes on each written assignment. If you find yourself spending much more than that, then you can stop working and let your TA know that you ran out of time.
You will not be graded on whether you have a perfect solution, but on whether you have demonstrated a reasonable effort (a good guideline is that we expect you to put in half an hour of work). Therefore please show some work that demonstrates how you got the answer rather than just writing the answer by itself.
Exercises: Solve the following problems on paper and bring your sheet of paper to your section on Thursday:
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); } }You can use PracticeIt to solve this problem. If you do, be sure to write your answer on a sheet of paper.
public class Point { int x; int y; // The constructor: public void Point(int initialX, int initialY) { int x = initialX; int y = initialY; } }You can use PracticeIt to solve this problem. If you do, be sure to write your answer on a sheet of paper.
Exercises: Solve the following problem on paper and bring your sheet of paper to your section on Thursday:
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 problems on paper and bring your sheet of paper to your section on Thursday:
For the next several questions, consider a file
called readme.txt
that has the following contents:
6.7 This file has several input lines. 10 20 30 40 test
readme.txt
file?
Scanner input = new Scanner(new File("readme.txt")); int count = 0; while (input.hasNextLine()) { System.out.println("input: " + input.nextLine()); count++; } System.out.println(count + " total");
hasNextLine
and nextLine
were replaced
by calls to hasNext
and next
, respectively?
hasNextLine
and nextLine
were replaced
by calls to hasNextInt
and nextInt
,
respectively? How about hasNextDouble
and nextDouble
?
while
,
Random
, boolean
, Midterm review
(Thu July 20)
Exercises: Solve the following three (3) Self-Check problems on paper and bring your sheet of paper to your section on Thursday:
Chapter 5,
Self-Check Problem #3: while
loop mystery (p372-73).
public static void mystery(int x) { int y = 1; int z = 0; while (2 * y <= x) { y = y * 2; z++; } System.out.println(y + " " + z); }Write the output of the first three (3) calls --
mystery(1)
,
mystery(6)
, mystery(19)
. Show your work by
writing each value that each variable has as the code is running.
For each 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 table
look like this:
mystery(1); mystery(6); x y z x y z _____________ _____________ 1 1 0 6 1 0 ... ...
Random
: Write code that generates a
random...
integer between 0 and 10 inclusive.
integer between 11 to 99 inclusive.
odd integer (not divisible by 2) between 50 and 99 inclusive.
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 |
if/else
, Scanner
, return
(Thu July 13)
Exercises: Solve the following problems on paper and bring your sheet of paper to your section on Thursday.
fractionSum
, p309).
Remember that you can use PracticeIt to solve
this
problem. If you do, either print what you end up with so that you
can turn it in or copy the code to your paper.
printTriangleType
, p311).
Remember that you can use PracticeIt to solve
solve this
problem. If you do, either print what you end up with so that you
can turn it in or copy the code to your paper.
Exercises: Solve the following two (2) problems on paper and bring your sheet of paper to your section on Thursday. Note that MysteryTouch has been modified.
public class MysteryTouch { public static void main(String[] args) { String head = "shoulders"; String knees = "toes"; String elbow = "head"; String eye = "eyes and ears"; String ear = "eye"; touch(ear, elbow); touch(eye, ear); } public static void touch(String elbow, String ear) { System.out.println("touch your " + ear + " to your " + elbow); } }Make a table that shows what value is being passed to
elbow
and
ear
for both calls and then indicate the output produced by
the program.for
loops
(Thu June 29)
Exercises: Solve the following problems on paper and bring your sheet of paper to your section on Thursday. For expressions problems, show your work (see note below).
14 / 7 * 2 + 30 / 5 + 1
4 * 3 / 8 + 2.5 * 2
4 + 1 + 9 + "." + (-3 + 10) + 11 / 3
for (int i = 1; i <= 2; i++) { for (int j = 1; j <= 3; j++) { for (int k = 1; k <= 4; k++) { System.out.print("*"); } System.out.print("!"); } System.out.println(); }
For the expression 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 in the section on "Precedence" in chapter 2 of the textbook (pages 70-73). For example:
2 + 19 % 5 - 11 * (5 / 2)
2 + 19 % 5 - 11 * 2
2 + 4 - 11 * 2
2 + 4 - 22
6 - 22
-16
Complete the introductory survey if you haven't done so already.