Sections

Each week problem(s) will be assigned to you that are due at the beginning of section. Completion of the weekly problems will earn you 5 participation points for the week. You must attend section to earn credit for the weekly section problems.

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.

Section 9: Final (Thu June 4th)

Exercises: Solve the following one (1) Self-Check problem on paper and bring your sheet of paper to your section on Thursday:

  1. Inheritance/Polymorphism Mystery. In addition to writing the answer, also show your work by turning in a table of the methods and their output, similar to the one shown in lecture.

    public class Bay extends Lake {
       public void method1() {
          System.out.print("Bay 1 ");
          super.method2();
       }
    
       public void method2() {
          System.out.printn("Bay 2 ");
       }
    }
    
    public class Pond {
       public void method1() {
          System.out.print("Pond 1 ");
       }
    
       public void method2() {
          System.out.print("Pond 2 ");
       }
    
       public void method3() {
          System.out.print("Pond 3 ");
       }
    }
    
    public class Ocean extends Bay {
       public void method2() {
          System.out.print("Ocean 2 ");
       }
    }
    
    public class Lake extends Pond {
       public void method3() {
          System.out.print("Lake 3 ");
       }
    }
    

    What output is produced by the following code fragment?

    Pond[] ponds = {new Ocean(), new Pond(), new Lake(), new Bay()};
    for (int i = 0; i < ponds.length; i++) {
       ponds[i].method1();
       System.out.println();
       ponds[i].method2();
       System.out.println();
       ponds[i].method3();
       System.out.println();
    }
    

Section 8: Classes and objects (Thu May 28)

Exercises: Solve the following three (3) Self-Check problems on paper and bring your sheet of paper to your section on Thursday:

  1. Chapter 8, Self-Check #2: What is an object? How is an object different from a class? (p564). Answer the question in your own words.

  2. Chapter 8, Self-Check #4: Reference mystery (p564). What is the output of the following program?

    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);
       }
    }
    
  3. Chapter 8, Self-Check #18: problems with constructor (p566). Write the two problems with the constructor shown, and then show a corrected version that does not contain these two problems.

    public void Point(int initialX, int initialY) {
       int x = initialX;
       int y = initialY;
    }
    

Section 7: Arrays (Thu May 21)

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

  1. Chapter 7, Self-Check Problem #28: array simulation (p509). 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]++;
          }
       }
    }
    
  2. Chapter 7, Self-Check Problem #10: max (p506). Write a method named max as described. Find the prompt and check your answer on Practice-It.

  3. Style Practice: Consider the following method, which takes an array that is always length 4 as a parameter and returns the sum and max value of the array.
    public static int[] sumAndMax(int[] array) {
       int sum = 0;
       sum += array[0];
       sum += array[1];
       sum += array[2];
       sum += array[3];
       int max = array[0];
       max = Math.max(array[1], max);
       max = Math.max(array[2], max);
       max = Math.max(array[3], max);
       int[] results = new int[2];
       results[0] = sum;
       results[1] = max;
       return results;
    }
    

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

Section 6: File input/output (Thu May 14)

Exercises: Solve the following four (4) Self-Check problems on paper and bring your sheet of paper to your section on Thursday:

  1. Chapter 6, Self-Check Problems #12, #13, and #14: File processing (p429). For each of the three problems, write the output from the code shown. You do not need to show your work; just the answer.

    For these three 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
    
    #12: What would be the output from the follwing code when it is run on the readme.txt file?
    Scanner input = new Scanner(new File("readme.txt"));
    int count = 0;
    while (intput.hasNextLine()) {
       System.out.println("input: " + input.nextLine());
       count++;
    }
    System.out.println(count + " total");
    

    #13: What would be the ouput from the code in the previous exercise if the calls to hasNextLine and nextLine were replaced by calls to hasNext and next, respectively?

    #14: 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?

  2. Style Practice: Consider the following method which takes a Scanner as a parameter and returns the sum of the values in a file until the number 42 is seen. Assume the file contains a sequence of integers such as
      12 68 56 73 5 27 ...
    
    Method:
      public static int returnSumUntil42(Scanner input) {   
         boolean seen42 = false;
         int total = 0;
         while (input.hasNext()) {
            int 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.5: Midterm practice

Exercises: Solve the following problem on paper and bring your sheet of paper to your section on Thursday, May 7th.

  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 = 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 5: while, Random, boolean (Thu Apr 30)

Exercises: Solve the following four (4) Self-Check problems on paper and bring your sheet of paper to your section on Thursday:

  1. Chapter 5, Self-Check Problem #14: boolean expressions (p371-2 problems c, d, and k.) Consider the following variable declarations:

       int x = 27;
       int y = -1;
       int z = 32;
       boolean b = false;
    

    What is the value of the following boolean expressions?

      c. (x > y) && (y > z)
      d. (x == y) || (x <= z)
      k. (z < x) == false
    
  2. Chapter 5, Self-Check Problem #3: while loop mystery (p368-9).

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

  4. Style Practice: Consider the following piece of code, which randomly generates numbers until a non-prime number is generated. Assume that a Random object named rand and a method called isPrime that takes a number and returns whether the number is prime have already been defined.
      int total = 0;
      int number = 1; // dummy value, not prime
      while (isPrime(number) == true) {
         int number = rand.nextInt(20);
         System.out.println("Number generated: " + number);
         if (isPrime(number) == true) {
            System.out.println("Yay! it's a prime");
            total++;
         } else {
            System.out.println("You generated " + total + " primes in a row");
         }
      }
    

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

Section 4: if/else, Scanner, return (Thu April 23)

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

  1. Programming Exercise 4.1: fractionSum (p305). Find the prompt and check your answer on Practice-It.
  2. Programming Exercise 4.12: printTriangleType (p307). Find the prompt and check your answer on Practice-It.
  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 April 16th)

Exercises: Solve the following three (3) problems on paper and bring your sheet of paper to your section on Thursday. If you aren't able to finish in 30 minutes, be sure to at least look at the Style Practice (problem 3) before section.

  1. Parameter Mystery, Consider the following program:
      public class ParameterMystery {
         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);
            touch(head, "knees " + knees);
         }  
    
         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 each of the three calls and then indicate the output produced by the program.
  2. Programming Exercise 3G.1 (p223) You can check your work for this problem using Practice-It.
  3. Style Practice: Consider the following piece of code
       public static void main(String[] args) {
          int result = 0;
          nthfactorial(5, result);
       }
    // Calculates the factorial
       public static void nthfactorial(int n, int result) {
          result = 1;
          for (int i = 1; i <= n; i++) {
             result = result * i;
          }
          System.out.println(result);
       }
    

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

Section 2: Expressions, for loops (Thu April 9th)

Exercises: Solve the following five (5) Self-Check problems on paper and bring your sheet of paper to your section on Thursday:

  1. Chapter 2, Self-Check Problem #2 (b) (p119), solve the following expression (see note below):
    	14 / 7 * 2 + 30 / 5 + 1
          
  2. Chapter 2, Self-Check Problem #3 (d) (p120), solve the following expression (see note below):
    	4 * 3 / 8 + 2.5 * 2
          
  3. Chapter 2, Self-Check Problem #4 (i) (p120), solve the following expression (see note below):
            4 + 1 + 9 + "." + (-3 + 10) + 11 / 3
        
  4. Chapter 2, Self-Check Problem #28 (p125), What is the output of the following sequence of loops?
          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();
          }
        
  5. Style Practice: Consider the following program
       public static void slash() {
          for (int a = 1; a <= SIZEOFFIGURE; a++) {
             for (int b = 1; b <= -1 * a + SIZEOFFIGURE; b++) {
                System.out.print("+");
             }
             for (int c = 1; c <= 1; c++) {
                System.out.print("/");
             }
             for (int d = 1; d <= a - 1; d++) {
                System.out.print("+");
             }
             System.out.println("");
          }
       }
    

    and SIZEOFFIGURE is a class constant declared earlier in the program. When SIZEOFFIGURE is 4, this method produces the following output:

    +++/
    ++/+
    +/++
    /+++
    

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

For the first three 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 on page 71-72 of the textbook. You may use a calculator if you want, though one shouldn't be necessary for these problems. For example:

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 Apr 2)

Complete by 8:30AM, Thursday, April 2, 2015