Building Java Programs, 4th edition

Lab: Final Exam Practice

Except where otherwise noted, the contents of this document are Copyright 2013 Stuart Reges and Marty Stepp.

lab document created by Marty Stepp, Stuart Reges and Whitaker Brand

Lab goals

Goals for this problem set:

Exercise : array simulation practice-it

Consider the following method:

public static void arrayMystery(int[] a) {
    for (int i = 1; i < a.length - 1; i++) {
        a[i] = a[i - 1] - a[i] + a[i + 1];
    }
}

In the left-hand column below are specific lists of integers. Indicate in the right-hand column what values would be stored in the list after method mystery executes if the integer list in the left-hand column is passed to it as a parameter.

{42, 42}
{42, 42}
{6, 2, 4}
{6, 8, 4}
{7, 7, 3, 8, 2}
{7, 3, 8, 2, 2}
{4, 2, 3, 1, 2, 5}
{4, 5, 3, 4, 7, 5}
{6, 0, -1, 3, 5, 0, -3}
{6, 5, 9, 11, 6, 3, -3}

Exercise : reference mystery practice-it

What four lines of output are produced by the following program?

public class ReferenceMystery {
    public static void main(String[] args) {
        int y = 1;
        int x = 3;
        int[] a = new int[4];
        mystery(a, y, x);                                             // 2 3 [0, 0, 17, 0][^0-9,]+
        System.out.println(x + " " + y + " " + Arrays.toString(a));   // 3 1 [0, 0, 17, 0][^0-9,]+
        x = y - 1;
        mystery(a, y, x);                                             // 1 0 [17, 0, 17, 0][^0-9,]+
        System.out.println(x + " " + y + " " + Arrays.toString(a));   // 0 1 [17, 0, 17, 0][^0-9,]+
    }

    public static void mystery(int[] a, int x, int y) {
        if (x < y) {
            x++;
            a[x] = 17;
        } else {
            a[y] = 17;
        }
        System.out.println(x + " " + y + " " + Arrays.toString(a));
    }
}

Exercise : reference mystery practice-it

The reference mystery problem on the next slide depends on the following definition of a BasicPoint class:

public class BasicPoint {
    int x;
    int y;

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

Reference mystery, continued practice-it

The following program produces 5 lines of output. Write each line of output as it would appear on the console.

   
public class ReferenceMystery {
    public static void main(String[] args) {
        BasicPoint p = new BasicPoint(11, 22);
        int[] a = {33, 44};
        int n = 55;

        System.out.println(p.x + "," + p.y + " " + Arrays.toString(a) + " " + n); // 11,22 [33, 44] 55[^0-9,]+
        mystery(p, a, n);                                                         // 44,22 [44, 77] 0[^0-9,]+
        System.out.println(p.x + "," + p.y + " " + Arrays.toString(a) + " " + n); // 44,22 [44, 77] 55[^0-9,]+

        a[0] = a[1];
        p.x = p.y;

        mystery(p, a, n);                                                         // 55,22 [88, 77] 0[^0-9,]+
        System.out.println(p.x + "," + p.y + " " + Arrays.toString(a) + " " + n); // 55,22 [88, 77] 55[^0-9,]+
    }

    public static int mystery(BasicPoint p, int[] a, int n) {
        n = 0;
        a[0] = a[0] + 11;
        a[1] = 77;
        p.x = p.x + 33;
        System.out.println(p.x + "," + p.y + " " + Arrays.toString(a) + " " + n);
        return n;
    }
}

Exercise : inheritance mystery practice-it

Assume the following classes have been defined:

public class A extends B {
    public void method2() {
        System.out.println("a 2");
    }
}
public class D extends B {
    public void method1() {
        System.out.println("d 1");
    }
}
public class C {
    public String toString() {
        return "c";
    }

    public void method1() {
        System.out.println("c 1");
    }

    public void method2() {
        System.out.println("c 2");
    }
}
public class B extends C {
    public String toString() {
        return "b";
    }

    public void method2() {
        System.out.println("b 2");
    }
}

continued on the next slide...

Exercise - inheritance mystery

b
c 1
a 2
b
c 1
b 2
c
c 1
c 2
b
d 1
b 2

Consider the code below that uses these classes.
Write each line of its output in the boxes at right.

C[] elements = {new A(),
                new B(),
                new C(),
                new D()};
for (int i = 0; i < elements.length; i++) {
    System.out.println(elements[i]);
    elements[i].method1();
    elements[i].method2();
}

Exercise : printStrings practice-it

Write a method called printStrings that takes as a parameter a Scanner holding a sequence of integer/string pairs and that prints to System.out one line of output for each pair with the given string repeated the given number of times.

You should solve this problem in Practice-It!

Exercise : isAllEven practice-it

Write a method called isAllEven that takes an array of integers as a parameter and that returns whether or not all of the values are even numbers (true for yes, false for no).

You should solve this problem in Practice-It!

Exercise : longestSortedSequence practice-it

Write a method called longestSortedSequence that accepts an array of integers as a parameter and that returns the length of the longest sorted (nondecreasing) sequence of integers in the array.

You should solve this problem in Practice-It!

Exercise : Hyena

Write a class Hyena that extends the Critter class, along with its movement behavior. All unspecified aspects of Hyena use the default behavior.

A Hyena object moves in a rectangular pattern looking for food, walking NORTH, then EAST, then SOUTH, then WEST. Each time the hyena walks an entire rectangle, it starts the rectangle pattern over again but with a rectangle 1 step wider than before. The general pattern is as follows:

Solve this program in jGRASP using the CritterMain simulator.

Exercise : Hyena revisited practice-it

Modify your Hyena class from the previous problem to add eating behavior. If the hyena encounters food at any point during its movement pattern, it eats the food and starts the pattern over, lengthening the rectangular pattern by 1 in the process. For example:

Solve this program in jGRASP with CritterMain, then test it using the Practice-it link above.

Exercise : array simulation practice-it

Consider the following method:

public static void mystery(int[] list) {
    for (int i = 0; i < list.length; i++) {
        list[i] = i * list[i];
    }
}

In the left-hand column below are specific lists of integers. Indicate in the right-hand column what values would be stored in the list after method mystery executes if the integer list in the left-hand column is passed to it as a parameter.

{}
{}
{7}
{0}
{3, 2}
{0, 2}
{5, 4, 3}
{0, 4, 6}
{2, 4, 6, 8}
{0, 4, 12, 24}

Exercise : reverseLines practice-it

Write a method called reverseLines that takes a Scanner containing an input file as a parameter and that echoes the input file to System.out with each line of text reversed.

You should solve this problem in Practice-It!

Exercise : append practice-it

Write a method called append that accepts two integer arrays as parameters and that returns a new array that contains the result of appending the second array's values at the end of the first array.

You should solve this problem in Practice-It!

Exercise : Shark practice-it

Shark objects should alternate between moving to the north and south as follows: first move 1 step north, then 2 steps south, then 3 steps north, then 4 steps south, then 5 steps north, then 6 steps south, and so on, each time moving one farther than previously.

Solve this program in jGRASP with CritterMain, then test it using the Practice-it link above.

Exercise : array simulation practice-it

Consider the following method:

public static void arrayMystery(String[] a) {
    for (int i = 0; i < a.length; i++) {
        a[i] += a[a.length - 1 - i];
    }
}

In the left-hand column below are specific lists of strings. Indicate in the right-hand column what values would be stored in the list after method mystery executes if the string list in the left-hand column is passed to it as a parameter.

{"a", "b", "c"}
{"ac", "bb" ,"cac"}
{"a", "bb", "c", "dd"}
{"add", "bbc", "cbbc", "ddadd"}
{"z", "y", "142", "w", "xx"}
{"zxx", "yw", "142142", "wyw", "xxzxx"}