Directions: Give brief but precise answers to the following questions; ask if you have a question.



encapsulation - grouping related data and the methods which operate on that data

inheritance - inheriting classes get base class fields and methods automatically

polymorphism - correct like-named method chosen for related objects (see #9)





public - accessible from anywhere (from any package)

package (or friendly) - accessible from within package

protected - accessible from any inherited class or within package

private - accessible only within class



arrays in C++ may be defined statically (compile time) or dynamically (run time - via new); arrays in Java are objects and must be defined dynamically (via new); Java arrays of objects need double allocation (one for array of handles and one for array objects); Java arrays of primitives need only a single allocation





packages are a method of grouping classes for name scoping









early binding occurs during compilation while late binding occurs during execution









public class Hello { // 2 pts

public static void main(String[] args) { // 4 pts

System.out.println("Hello, Java!"); // 2 pts

} // 1 pt

} // 1 pt





public class Base { // +1 pt

public int global; // +2 pts

protected int inherited; // +2 pts

private int local; // +2 pts

} // + 1 pt



package Separate; // + 1 pt

public class Derived extends Base { // + 3 pts

int friendly; // + 2 pts

} // + 1 pt

// Wrestling.java by Dr. I. for CS 102 during Spring, 1999

// This is question #8 on exam #2

public class Wrestling extends Athlete { // +2 pts

public Wrestling(String name, int pins,

int wins, int losses) { // +5 pts

super(name);

setPins(pins); setWins(wins); setLosses(losses);

}

public int getPins() { return pins; } // +3 pts

public int getWins() { return wins; }

public int getLosses() { return losses; }

public int evaluate() { // +4 pts

return (int) (((2.0 * ((double)pins / (double)wins)) +

(1.0 * ((double)wins /

((double)wins + (double)losses)))

) / 3.0 * 100.0);

}

public String toString() { // +4 pts

return super.toString() + ",pins(" + pins +

"),wins(" + wins + "),losses(" + losses + ")";

}



private void setPins(int pins) { this.pins = pins; }

private void setWins(int wins) { this.wins = wins; }

private void setLosses(int losses)

{ this.losses = losses; } // +3 pts



private int pins; // +3 pts

private int wins;

private int losses;

} // +1 pt

Athlete[] athletes = new Athlete[MAX_ATHLETE]; // + 3 pts

athletes[0] = new Wrestling("name1", 15, 20, 2); // + 3 pts

athletes[1] = new Sport("name2", ...); // + 3 pts

athletes[2] = new Sport("name3", ...); // + 2 pts

for (int athlete = 0; athlete < 3; ++athlete) { // + 1 pt

System.out.println(athletes[athlete] + // + 3 pts

" has a value of " +

athletes[athlete].evaluate();

}

























Byte[] buffer = new Byte[80]; // + 2 pts

try { // + 1 pt

System.in.read(buffer); // + 2 pts

}

catch (IOException ioe) { // +1 pt

System.out.println("Error in read");

}

String string = new String(buffer); // + 2 pts

int inputValue = new Integer(string.trim()).intValue();

// + 2 pts