Date post: | 22-Dec-2015 |
Category: |
Documents |
View: | 215 times |
Download: | 2 times |
COMP 114
Weekly Recitation
October 10, 2003
Know What’s Happening Monday?
The first examfirst exam!
So What’s Happening Today?
We’ll review an old midtermold midterm!
With a Special Bonus…
...specifically for Kimberly’s section!
Flowers!
OK, a flower.
Right, now back to work.
Question 1A
Explain the advantages of writing modular programs.
Modular programs are easier to read, debug, and extend since they deal with smaller parts.
Question 1B
Explain the difference between an interface and a class.
Interfaces can have only method headers, must be implemented by other classes, and cannot be directly instantiated.
Question 1C
Why are interfaces useful?
Program Evolution: We can replace an obsolete class with a newer one implementing the same interface.
Polymorphic Code: By implementing multiple interfaces we can reuse code in differing parts of the program.
Question 2
Consider the following code:package oo;public interface I { public void print();}package oo;public class C1 implements I { protected static int i = 0; protected int j = 0; public C1(int k) {i = k; j =
k;} public void print () { System.out.println (i); System.out.println (j); }}
package oo;public class C2 extends C1 { public C2 (int k) {super
(k);}; public void print()
{ System.out.println(j); };}package oo;class M { public static void main
(String args[]) { I i1 = new C1(1); I i2 = new C2(2); i1.print(); i2.print(); }}
Question 2A
What are the class and instance variables?package oo;public interface I { public void print();}package oo;public class C1 implements I { protected static int i = 0; protected int j = 0; public C1(int k) {i = k; j =
k;} public void print () { System.out.println (i); System.out.println (j); }}
package oo;public class C2 extends C1 { public C2 (int k) {super
(k);}; public void print()
{ System.out.println(j); };}package oo;class M { public static void main
(String args[]) { I i1 = new C1(1); I i2 = new C2(2); i1.print(); i2.print(); }}
Question 2B
Name the “is-a” relationships in this code.package oo;public interface I { public void print();}package oo;public class C1 implements I { protected static int i = 0; protected int j = 0; public C1(int k) {i = k; j =
k;} public void print () { System.out.println (i); System.out.println (j); }}
package oo;public class C2 extends C1 { public C2 (int k) {super
(k);}; public void print()
{ System.out.println(j); };}package oo;class M { public static void main
(String args[]) { I i1 = new C1(1); I i2 = new C2(2); i1.print(); i2.print(); }}
Question 2C
What is the output of this program?package oo;public interface I { public void print();}package oo;public class C1 implements I { protected static int i = 0; protected int j = 0; public C1(int k) {i = k; j =
k;} public void print () { System.out.println (i); System.out.println (j); }}
package oo;public class C2 extends C1 { public C2 (int k) {super
(k);}; public void print()
{ System.out.println(j); };}package oo;class M { public static void main
(String args[]) { I i1 = new C1(1); I i2 = new C2(2); i1.print(); i2.print(); }}
Question 3
package ex;import java.util.NoSuchElementException;public class AnEmptyEnumerationException extends
NoSuchElementException {};package ex;import java.util.Vector;import java.util.Enumeration;import java.util.NoSuchElementException;class AnExceptionThrower { public static void main (String args[]) { Vector v = new Vector (); // create
empty vector try {
printFirstAndSecondElements(v.elements()); } catch (NoSuchElementException nsee) { System.out.println("Missing first or
second vector element."); } v.addElement("hello");
printFirstAndSecondElements(v.elements()); }
public static void printFirstAndSecondElements(Enumeration elements) throws AnEmptyEnumerationException, NoSuchElementException {
try {
System.out.println(elements.nextElement().toString());
} catch (NoSuchElementException nsee) { System.out.println("Enumeration is
empty"); throw new
AnEmptyEnumerationException(); } System.out.println
(elements.nextElement().toString()); }}
What output does this program produce?
You don’t have to memorize the system error message, but you should write that one will show up onscreen.
Question 4
Find the syntax errors:package err;public interface I { public int read();}package err;public abstract class C1
implements I { private int i;}package err;import java.io.IOException;public class C2 extends C1 { protected int j; public C2 (int k) {super (k);
j = k; i = k;}; public int read() throws
IOException { return System.in.read(); };
}
package err;public class ErrorsMain { int i = 0; public static void main(String
args[]) { C1 c1 = new C2(1); C2 c2 = new C1(); i = System.in.read(); }}
• C1 implements I but doesn’t offer int read()
• ErrorsMain.main should have throws IOException
• C2 can’t call C1(int) since none exists• C2 can’t access i since it’s private• C1() can’t create a C2• C1 is abstract and can’t be instantiated• i is nonstatic and can’t be mentioned in
main(String[])
Question 5
Improve the following code. You may not remove external interfaces, but may add to or enhance existing ones.
package decr;public class ADecrementableInt { int i; public ADecrementableInt (int newVal) {i
= newVal;} public void decrementInt() { i = i - 1;}}package decr;import java.util.Vector;public class ADecrementableVector { Vector v; public ADecrementableVector (Vector
newVal) {v = newVal;} // removeElementAt(index) throws
ArrayIndexOutOfBoundsException if there is no element at index
public void decrementVector() {v.removeElementAt(0);}
}
package main;import decr.ADecrementableInt;import decr.ADecrementableVector;import java.util.Vector;class ADecrementer { public static void main (String args[])
{ ADecrementableVector
decrementableVector = new ADecrementableVector(new Vector());
ADecrementableInt decrementableInt = new ADecrementableInt (0);
doDecrement (decrementableVector); doDecrement (decrementableInt); } public static void doDecrement (Object
o) { if (o instanceof ADecrementableInt) ((ADecrementableInt)
o).decrementInt(); else if (o instanceof
ADecrementableVector) ((ADecrementableVector)
o).decrementVector(); }}
Question 5
So what should we do with this mess?Create a common Decrementable
interface; get rid of instanceof checksMake ADecrementableInt.i protectedThrow a more sensible exception when
decrementing an empty vector
Question 5
The final code:package decrement;public interface Decrementable { public void decrement() throws
ADecrementException;}package decrement;public class ADecrementException extends Exception
{ };package decrement;public class ADecrementableInt implements
Decrementable { protected int i; public ADecrementableInt (int newVal) {i =
newVal;} public void decrement () { i = i - 1;}}package decrement;import java.util.Vector;public class AdecrementableVector implements
Decrementable { protected Vector v; public ADecrementableVector (Vector newVal) {v =
newVal;} // removeElementAt(index) throws
ArrayIndexOutOfBoundsException if there is no element at index
public void decrement () throws ADecrementException { try { v.removeElementAt(0); } catch (ArrayIndexOutOfBoundsException aibe) { System.out.println (“Trying to decrement
empty vector”); throw new ADecrementException(); }}package main;import decrement.ADecrementableInt;import decrement.ADecrementableVector;import decrement.Decerementable;import java.util.Vector;class ADecrementer { public static void main (String args[]) { ADecrementableVector decrementableVector = new
ADecrementableVector(new Vector()); ADecrementableInt decrementableInt = new
ADecrementableInt (0); doDecrement (decrementableVector); doDecrement (decrementableInt); } public static void doDecrement (Decrementable d) { try { d.decrement(); catch (ADecrementException de) { System.out.println(“Decrement Underflow”);
}}
Question 6
Code a class, AScoresTable, that defines a table of names and scores. Its constructor expects an enumeration in which instances of names and scores alternate. A name is represented by a String instance and a score by an Integer instance. Both String and Integer are existing Java classes. (Recall that Integer is the wrapper class provided by Java for the primitive type int.) Thus, the constructor expects a sequence of zero or more (String, Integer) pairs. It constructs from this sequence a table consisting of two vectors, names and scores, ensuring that the String and Integer of the ith pair in the enumeration are stored in the ith elements of names and scores, respectively. Thus, assuming the enumeration {“Joe Doe” 50 “Jane Smith” 60}, it constructs the vectors {“Joe Doe” “Jane Smith”} and {50 60}. In case of a parsing error, it throws AParsingException to its caller.
A partial implementation of the class is given below. Complete the class by implementing the method setNamesAndScores(Enumeration). You may use recursion or a loop.
package scores;public class AParsingException extends
java.io.IOException {};package scores;import java.util.Vector;import java.util.Enumeration;public class AScoreTable { Vector names = new Vector(); Vector scores = new Vector(); public AScoreTable (Enumeration enumeration)
throws AParsingException {setNamesAndScores (enumeration);} //you need to implement the following method public void setNamesAndScores (Enumeration
enumeration) throws AParsingException { while (enumeration.hasMoreElements()) try { names.addElement((String)
enumeration.nextElement()); scores.addElement((Integer)
enumeration.nextElement()); } catch (ClassCastException cce) { throw new AParsingException(); } catch (NoSuchElementException nsee) { throw new AParsingException() } }}
Question 1. Or Question 7.
Consider the following two classes, ASingleQuotedString and ADoubleQuotedString:public class ASingleQuotedString { protected String string; public ASingleQuotedString (String theString) {string = theString;}; public String getString() {return string;} public String getQuotedString() {return '\'' + string + '\'';};}public class ADoubleQuotedString { protected String string; public ADoubleQuotedString (String theString) {string = theString;}; public String getString() {return string;} public String getQuotedString() {return '\"' + string + '\"';}}Factor the code in these classes by defining a new abstract class, AnAbstractQuotedString; and
two new concrete subclasses, AConcreteSingleQuotedString, and AConcreteDoubleQuotedString. The two concrete subclasses should have the same behavior as the two original classes and you should factor out as many methods and as much code as possible into the abstract class. In the process of factoring you should try to maintain strong encapsulation, that is, not access variables declared in other classes. You are free to implement new methods.
Question 1. Or Question 7. Part 2.
public abstract class AnAbstractQuotedString {
protected char quote; protected String string; protected
AnAbstractQuotedString(String s, char q) {
string = s; quote = q; } public String getString() { return string; } public String
getQuotedString() { return quote + string +
quote; }}
public class ASingleQuotedString extends AnAbstractQuotedString {
public ASingleQuotedString(String s) {
super(s, '\''); }}
public class ADoubleQuotedString extends AnAbstractQuotedString {
public ADoubleQuotedString(String s) {
super(s, '\"'); }}
Epilogue
Any questions about the test?Any comments about Program 3?