University of Washington, CSE 142

Lab 10: 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

Basic lab instructions

Today's lab

Goals for today:

Exercise : Expressions practice-it

For each expression in the left-hand column, indicate its value in the right-hand column. Be sure to list a constant of appropriate type (e.g., 7.0 rather than 7 for a double, Strings in quotes).

12/5 + 8/4
4
2.5 * 2 + 17/4
9.0
41 % 15 % 7 + 17 % 3
6
21/2 + "7 % 3" + 17 % 4
"107 % 31"
46/3/2.0/3 * 4/5
2.0

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 : 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 : 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 : 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 : removeShorterStrings practice-it

Write a method called removeShorterStrings that takes an ArrayList of strings as a parameter and that removes from each successive pair of values the shorter string in the pair.

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 : 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!

If you finish them all...

If you finish all the exercises, try out our Practice-It web tool. It lets you solve Java problems from our Building Java Programs textbook.

You can view an exercise, type a solution, and submit it to see if you have solved it correctly.

Choose some problems from the book and try to solve them!