Written Assignments

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 20 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 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.

Section 9: Final practice (Thu Mar 11)

Exercises: Solve the following problem on paper and bring your sheet of paper to your section on Thursday:

  1. Inheritance. Assume that the following classes have been defined:
    	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 may write the output as a series of 3-line columns in order from left to right)

Section 8: Classes and Objects (Thu Mar 4)

Exercises: Solve the following problem on paper and bring your sheet of paper to your section on Thursday:

  1. Critters. Define a Critter class called Ant. It should always infect if an enemy is in front of it. Otherwise it should randomly choose between the three other actions (HOP, LEFT, RIGHT). Each choice should be equally likely. Its color should be blue and its string should be "A". To work on this problem, you should download and unzip the files in ass8.zip.

Section 7: Arrays (Thu Feb 25)

Exercises: Solve the following problem on paper and bring your sheet of paper to your section on Thursday:

  1. 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.

Section 6: File Input/Output (Thu Feb 18)

Exercises: Solve the following problem on paper and bring your sheet of paper to your section on Thursday:

  1. 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.

Section 5.5: Midterm practice (Thu Feb 11)

Exercises: Solve the following problem on paper and bring your sheet of paper to your section on Thursday:

  1. 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 += x - y;
    		x /= 2;
    		// Point C
    		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      

Section 5: while, Random, boolean (Thu Feb 4)

Exercises: Solve the following problems on paper and bring your sheet of paper to your section on Thursday:

  1. Self-Check 5.4: while loop mystery (p320). 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.
  2. (depends on Wednesday lecture) Programming Exercise 5.3 (p327). You can use PracticeIt to solve this problem.

Section 4: if/else, Scanner, return (Thu Jan 28)

Exercises: Solve the following problems on paper and bring your sheet of paper to your section on Thursday:

  1. Programming Exercise 4.1: fractionSum (p266).
  2. (depends on Wednesday lecture) Programming Exercise 4.10 (printTriangleType) (p268). You can use PracticeIt to solve this problem.

Section 3: parameters, graphics (Thu Jan 21)

Exercises: Solve the following problems on paper and bring your sheet of paper to your section on Thursday:

  1. 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.
  2. (depends on Wednesday lecture) Programming Exercise 3G.1 (p199). Remember that you can use PracticeIt to solve this problem.

Section 2: expressions, for loops (Thu Jan 14)

Exercises: Solve the following problems on paper and bring your sheet of paper to your section on Thursday:

  1. from Self-Check 2.2 (p106), solve the following expressions:
    	14 / 7 * 2 + 30 / 5 + 1
    	(12 + 3) / 4 * 2
    	813 % 100 / 3 + 2.4
          
  2. from Self-Check 2.3 (p106), solve the following expressions:
    	4.0 / 2 * 9 / 2
    	2.5 * 2 + 8 / 5.0 + 10 / 3
          
  3. Programming Exercise 2.6 (p112). Remember that you can use PracticeIt to solve this problem.

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
      

Section 1: basic Java, static methods (Thu Jan 7)

Exercises: Solve the following two (2) problems on paper and bring your sheet of paper to your section on Thursday:

  1. Self-Check 1.19: "Confusing" class (p45). Instead of showing the output, show the flow of execution as was done for the sample program on page 40, using indentation when one method calls another. For example, the first few lines should be:

    	  main
    	    method1
    	      println(...1)
    	    method3
    	    ...
    	

  2. Programming Exercise 1.2: Complete program--diamond shape (bottom of p47).