CIS 161 Final Exam: Winter 2004
Problem One:
- Create a Transformer interface.
This interface should have
one method: call the method transform(). The method should return an int array.
- Create an abstract class that uses your Transformer interface.
Call this
class ATransformer. This class should have constructor that accepts an
integer array and stores a copy of it in an instance field. (Note: To copy it,
make a new array the same size and copy each element from the old one to the
new one.) Also add a zero() method to the class that will put a zero into
each element of the array and return void. Also implement a toString() method in
this class that will simply put each value of the array on a line by itself in
the result string. (Add a "\n" between each element of the array.)
- Create two subclasses of your ATransformer class.
Call the first class Incrementer. The constructor of this class
accepts an int value, store
this value in an instance variable called increment (remember the super
class also expects to get something that must be part of this class
constructor). Implement the
transform() method of this class to add the value of increment to each element
of the array. Return the array reference.
The second class should be called Doubler. (What does the constructor
require?) In this class implement the
transform method to double the elements of the array. However, don't
modify the original array, create a new array for the doubled values and
return a reference to this new array.
- Create a class called Driver. Add a main method to this class. Write code
to demonstrate polymorphic use of your two classes Incrementer and Doubler.
Make several small int arrays to use (actually only one is required) and
then create a list of some sort to add your new objects to. Create the
objects, add them to the list, and then finally iterate over the list
calling methods appropriately. When the program runs the output should
demonstrate what is happening.
Problem Two
What design principle is violated by the init() method in the
following code? What would you do to fix it?
public class ExamFileA {
private int value;
private String name;
public void init() {
value = 1;
name = "No name";
System.out.println("System Version 0.01");
System.out.println("CyberCoder 103X35");
System.out.println(new java.util.Date());
}
}
Problem Three
Given the classes and packages below, answer the questions following the
graphic.

- In class Q what instance variables are available from class A?
- In class C what instance variables are available from class A?
- In class C what instance variables are available from class B?
Problem Four
Describe the difference between a checked and an unchecked
exception.
Problem Five
Look up the Integer.parseInt() method in the API to find what type of
exception it throws.
Given the code below, make the following changes:
- Add a try / catch block to the getInt() method to trap
for bad user input. If the user inputs bad data for an int, use the
jop.showMessageDialog() method to say "Try that again."
- Put the above code in a loop structure to repeat until good input has
been entered for an int.
- Create a main method that demonstrates how a user of the class would
call the method to input an int. Remember a typical user of this class can
not directly call the getInt() method. (Try to complete this part even if
you can not finish the other parts.)
public class IntInput {
private static javax.swing.JOptionPane jop =
new javax.swing.JOptionPane();
private IntInput() {}
public static int getInt() {
int value;
String input = jop.showInputDialog(null, "int?");
value = Integer.parseInt(input);
return value;
}
}
Problem Six
Describe the concept of Coupling as discussed in your text. Give
at least one example from the book.