Sections

This page lists the section topics for each week, as well as the section homework that is due at the beginning of section each week.

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 1 points for each written assignment you bring to section, and 2 points for attendance. The maximum number of section attendance/hw points that you can receive is 20. (This point total is doubled at the end of the quarter so that the section total is worth as much as one homework assignment. As a guideline, we expect you to spend around 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.

Section 9: Final practice (Thu Dec 7)

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? (write the output as a series of 3-line columns in order from left to right)

Section 8: Objects (Thu Nov 30)

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

  1. Self-Check 8.4 (reference mystery, p578). The following program produces 4 lines of output. Write each line of output below as it would appear on the console.
    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.
  2. Self-Check 8.18 (Object Constructors, p580). The constructor in the following class definition has two major problems. What are they? Describe the issues, and write a working version of the constructor:
    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.

Section 7: Arrays (Thu Nov 16)

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

  1. Chapter 7, Self-Check Problem #28: array simulation (p520-521). Write the final contents of each array after the method is finished executing. Show your work by writing the arrays' initial contents and then crossing out elements as their values change.

    public static void mystery5(int[] nums) {
       for (int i = 0; i < nums.length - 1; i++) {
          if (nums[i] > nums[i + 1]) {
             nums[i + 1]++;
          }
       }
    }
    
    a. {8}
    b. {14, 7}
    c. {7, 1, 3, 2, 0, 4}
    d. {10, 8, 9, 5, 5}
    e. {12, 11, 10, 10, 8, 7}
  2. Chapter 7, Self-Check Problem #10: max (p517). Write a method named max as described. Find the prompt and check your answer on Practice-It.

Section 6: File input/output (Thu Nov 9)

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

  1. Self-Check 6.12 (file processing, p433). What would be the output from the following code when it is run on the 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");
    
  2. Self-Check 6.13 (file processing, p433). What would be the output from the code in the previous exercise if the calls to hasNextLine and nextLine were replaced by calls to hasNext and next, respectively?
  3. Self-Check 6.14 (file processing, p434). What would be the output from the code in the previous exercise if the calls to hasNextLine and nextLine were replaced by calls to hasNextInt and nextInt, respectively? How about hasNextDouble and nextDouble?