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)



composition - where one object "has an" instance of another object within it

inheritance - where one object "is a" type of the other object

an automobile has an engine; an automobile is a motorized vehicle



an abstract class is a class in which at least one method has only a prototype given (not its definition); an interface is a prototype description of a promise to implement one or more methods in a class; both contain some prototype(s) (and not the definitions); an abstract class can form the basis for a single inheritance hierarchy while interfaces provide a form of "multiple inheritance" where a class can implement one or more interfaces.



public class JavaWinApp extends JFrame

public class JavaApplet extends Japplet



init is used when an applet is created; start is used when an applet moves into sight; paint is used when an applet needs special painting; stop is used when an applet moves out of sight; and destroy is used when an applet is being unloaded.



Usually during the applet's init or the application's constructor, the button's addActionListener method is passed an object of a class which implements the interface ActionListener. This class must implement the method actionPerformed which actually handles the event when it occurs.



P1.add(b1,"North");

p2.add(b2, "North"); p2.add(b3, "South"); p1.add(p2, "Center");

P1.add(b4, "South");



String text = counter.getText();

int value = Integer.parseInt(text); ++value;

counter.setText("" + value);

Both kinds of inner classes must be within another class declaration. The named inner class will appear as a normal class but within another class' declaration. An anonymous inner class appears inside another class' declaration but without the word class and any name; the braces of the body of the class where one would not ordinarily expect one is the key. The class file for a named inner class is simply the outer class name, a dollar sign, and the inner class name dot class. The class file for an anonymous inner class will get it name from the name of the enclosing class dollar number dot class where dollar is the dollar sign and number is a unqiue sequence number.



The try and catch blocks are used for exception handling. The try block contains statements which may cause an exception and other surrounding statements. The catch block contains the exception handling code. Between the catch work and the catch block is a parenthesized list of parameters for the catch block. The try block consists of the word try following by a block (a group of statements enclosed in braces). The catch block consists of the word catch followed by parameters in braces followed by a block.



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

// This is exam 3, question 11

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;



public class CounterExample extends JFrame implements ActionListener {

public CounterExample() {

setTitle("CounterExample"); setSize(200, 100);



label = new JLabel("counter");

counter = new JTextField("0", 3);

increment = new JButton("increment");

panel = new JPanel();

increment.addActionListener(this);



panel.add(label); panel.add(counter); panel.add(increment);

Container contentPane = getContentPane();

contentPane.add(panel, "Center");

}



public void actionPerformed(ActionEvent evt) {

String text = counter.getText();

int value = Integer.parseInt(text);

counter.setText("" + (value + 1));

}



public static void main(String[] args)

{ JFrame frame = new CounterExample();

frame.show();

}



private JLabel label;

private JTextField counter;

private JButton increment;

private JPanel panel;

}





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

// This is question #12 on exam #3

import java.util.*;



public class Sport extends Athlete {

public Sport(String name, int wins, int losses) {

super(name);

setWins(wins); setLosses(losses);

}

public int getWins() { return wins; }

public int getLosses() { return losses; }

public int evaluate() {

return (int) ((double)wins / ((double)wins + (double)losses) * 100.0);

}

public static Sport getSportAthlete() {

Inputs input = new Inputs();

System.out.print("Name? "); String name = input.readString();

System.out.print("Wins? "); int wins = input.readInt();

System.out.print("Losses? "); int losses = input.readInt();

return new Sport(name, wins, losses);

}

public static Sport getSportAthlete(String input) {

String delimiter = ":";

StringTokenizer st = new StringTokenizer(input, delimiter);

String name = st.nextToken();

int wins = Integer.parseInt(st.nextToken());

int losses = Integer.parseInt(st.nextToken());

return new Sport(name, wins, losses);

}

public String toString() {

return super.toString() + "),wins(" + wins + "),losses(" + losses + ")";

}

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

private void setLosses(int losses) { this.losses = losses; }



private int wins; private int losses;

}