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

Section 9: Final Part A (Thu August 18)

Note: This written assignment is OPTIONAL but is here in case you need extra section points. You can't get more than 18 section points, so if you've already attended 6 sections, then you don't need to do this!

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 August 11)

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 August 4)

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 July 28)

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?
  4. Style Practice. Consider a method takes a Scanner as a parameter and that returns the sum of the values in the Scanner until the number 42 is seen, not including the 42 and any other numbers that come after it in the sum. Assume the Scanner contains a sequence of integers such as the following.
    12 68 56 73 5 27 42 83 19 7 317 519
    
    For the input above, the method would add up the first 6 numbers and return 241. Below is one version of the method.
    public static int returnSumUntil42(Scanner input) {   
       boolean seen42 = false;
       int total = 0;
       int num = 0;
       while (input.hasNext() == true) {
          num = input.nextInt();
          if (num == 42) {
             seen42 = true;
          }
          if (seen42 == false) {
             total += num;
          }
       }
       return total;
    }
    

    This code would receive full external correctness, but not full internal correctness. List all style issues that you can find.

Section 5: Midterm practice, while, Random, boolean (Thu July 21)

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

  1. Self-Check 5.4 (while loop mystery, p373). 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 calls. 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.
  2. Programming Exercise 5.4 (randomX, p382). 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.
  3. 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      

Section 4: if/else, Scanner, return (Thu July 14)

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

  1. (try after Wed lecture) Programming Exercise 4.1 (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.
  2. Programming Exercise 4.12 (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.
  3. Style Practice. Consider the following piece of code, which prompts a user about the number of friends they have and prints a corresponding message. Assume the console scanner has been declared somewhere earlier in the program.
    System.out.print("How many friends do you have? ")
    double friends = console.nextDouble();
    if (friends < 50) {
       System.out.println("You are friends with " + friends / 7000000000 + " percent of the world.");
       System.out.println("You need to get more friends!");
    } else if (friends < 250) {
       System.out.println("You are friends with " + friends / 7000000000 + " percent of the world.");
       System.out.println("You have an average number of friends."); 
    } else if (friends >= 250) {
       System.out.println("You are friends with " + friends / 7000000000 + " percent of the world."); 
       System.out.println("Whoa there! You have a lot of friends.");
    }
    

    This code would receive full external correctness, but not full internal correctness. List all style issues that you can find.

Section 3: parameters, graphics (Thu July 7)

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);
        }
    
        public static void mystery(int a, int b, int c) {
            System.out.println("The " + a + " monkeys ate " + (b + c) + " bananas");
        }
    }
    
    Make a table that shows what value is being passed to each of a, b, and c for each of the three calls and then indicate the output produced by the program.
  2. Style Practice. Consider the following program:
    // Jenny Kang
    // TA: Michelle Yun
    public class drawFigure {
        public static int Width = 10;
        public static void main(String[] args) {
            for (int i = 1; i <= 5; i++) {
                int s = 2 * i - 1;
                drawline(s, i, Width);
            }
        }
      // draws a line
        public static void drawline(int s, int i, int w) {
            System.out.print("|");
            for (i = 1; i <= w - s; i++) {
                System.out.print(" ");
               }
               for (i = 1; i <= s; i++) {
                  System.out.print("*");
                }
             System.out.print("|");
           System.out.println();
        }
    }
    

    This program would receive full external correctness for producing this output:

    |         *|
    |       ***|
    |     *****|
    |   *******|
    | *********|
    

    This program would not receive full internal correctness. List all style issues that you can find.

  3. Programming Exercise 3G.1 (MickeyBox, p227). 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.

Section 1: Basic Java, static methods (Thu June 23)

Complete the introductory survey if you haven't done so already.