CS 102 - Java Programming      Exam 1      Spring, 2000      (100 total points)

Directions: Give brief but precise answers to the following questions.

1. Name the three types of Java programs.  Distinguish among them in terms of explicit inheritance (i.e., what they extend) and method(s) needed for “Hello, world!”.  (10 pts)

console app - no explicit inheritance - main method.
windows app - Frame - (constructor), paint, and main methods.
applet app - Applet - paint method.
2. Write a Java applet named ColorApplet with a blue background and a red foreground containing one Button field called b which says “Java” on it.  (10 pts)
import java.applet.*;
import java.awt.*;

public class ColorApplet extends Applet {
 Button b = new Button("Java");
 
 public void init() {
  setBackground(Color.blue);
  setForeground(Color.red);
  add(b);
 }
}

3. Write a Java applet called CircleInSquare which contains a 100 pixel square at 50,50 with a 30 pixel circle in the center of the square.  (10 pts)
import java.applet.*;
import java.awt.*;

public class CircleInSquare extends Applet {
 public void paint(Graphics g) {
  g.drawRect(50, 50, 100, 100);
  g.drawOval(85, 85, 30, 30);
 }
}

4. From the top of the Java class hierarchy, show the relationship among the Applet, Button, Frame, and Menu classes.  (10 pts)
Object -> Component -> Container -> Panel -> Applet
                    -> Button
                                 -> Window -> Frame
       -> MenuComponent -> MenuItem -> Menu
5. Write a Java applet called BigHello which contains “Hello, Java!” starting at 50,50 in a 36 pixel high, BOLD, “Serif” font called f.  (10 pts)
import java.applet.*;
import java.awt.*;

public class BigHello extends Applet {
 Font f = new Font("Serif", Font.BOLD, 36);
 
 public void paint(Graphics g) {
  g.setFont(f);
  g.drawString("Hello, Java!", 50, 50);
 }
}

6. Write only the Java method which arranges four already defined buttons called lt, lb, rl, and rr using two already defined panels l and r such that the first two buttons are vertical on the left and the second two buttons are horizontal on the right.  (10 pts)
 public void init() {
  l.setLayout(new GridLayout(2,1));
  l.add(lt);  l.add(lb);
  r.setLayout(new GridLayout(1,2));
  r.add(rl);  r.add(rr);
  setLayout(new GridLayout(1,2));
  add(l);  add(r);
 }
7. Name and describe the three layout classes.  (5 pts)
FlowLayout - lays out components from left to right, top to bottom;
BorderLayout - lays out components in specified north (top), south (bottom), west (left), east (right), and center positions;
GridLayout - lays out components in equal sized locations of a specified size rectangular grid (from left to right, top to bottom).
8. Describe how to create radio buttons in Java (be specific).  (5 pts)
Starting with defined Checkbox objects and a CheckboxGroup object, use the setCheckboxGroup method of Checkbox to add each Checkbox object to the CheckboxGroup object.  Adding Checkbox objects to the CheckboxGroup may also be done while creating each Checkbox object.
9. From a method call, describe how to determine to what class the method belongs.  (5 pts)
From a method call of the form object.method(), the method belongs to object or one of the classes up its inheritance tree;
from a method call of the form Class.method(), the method belongs to Class;
from a method call of the form method(), it belongs to the current object or one of the classes up its inheritance tree.
10. Describe the following statement a precisely as you can: (5 pts)
        Rectangle r = new Rectangle(new Point(10,20), new Dimension(30,40));
A Rectangle object called r is being created.  Two anonymous objects, one a Point object and the other a Dimension object are created and passed as parameters to r's constructor.  The Point object has two values, 10 and 20, passed to its constructor (an x value of 10 and a y value of 20).  The Dimension object has two values, 30 and 40, passed to its constructor (a width of 30 and a height of 40).
11. Explain what “this” is in Java and how and where it may be used.  (5 pts)
This is the name of the current object.  In method calls of the form method() (i.e., without object. in front), this. is understood and may be added in front of the method call.
12. Give a widget class example of an abstract class and describe its purpose.  (5 pts)
Both Component and Container are abstract classes.  Each is intended as "a repository of methods and data" and "can never be instantiated into an object".  Although TextComponent is technically not an abstract class (just a constructor-less class), I will accept it also for the same kind of purpose (i.e., "factoring out" common methods and data).
13. Describe the similarities and differences between Java Choice and List objects.  (5 pts)
Both Choice and List objects contain String items.  For a Choice object, only the currently selected item is displayed whereas for a List object, a predefined number of the items are displayed with the selected item(s) highlighted.  When the Choice object is clicked, its items are shown with the currently selected item highlighted.  If necessary, the List object is scrollable.
14. Describe the hierarchy relationship between the MenuItem and Menu classes and why this is the way it is.  (5 pts)
The Menu class inherits from the MenuItem class.  This allows a MenuItem to also itself be a Menu.
15. (EXTRA)  Write only the method which creates a PopupMenu called action which contains items “Start”, “Stop”, and “Pause”.  (+5 pts)
public void init() {
 PopupMenu action = new PopupMenu();
 action.add("Start");
 action.add("Stop");
 action.add("Pause");
}
The following Java applet shows the common use of a popup menu
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class ActionMenu extends Applet implements MouseListener {
 PopupMenu action = new PopupMenu();
 public void init() {
  action.add("Start");
  action.add("Stop");
  action.add("Pause");
  add(action);
  addMouseListener(this);
 }
 // next five methods must be implemented for a MouseListener interface
 public void mouseClicked(MouseEvent e) {
 }
 public void mouseEntered(MouseEvent e) {
 }
 public void mouseExited(MouseEvent e) {
 }
 public void mousePressed(MouseEvent e) {
 }
 public void mouseReleased(MouseEvent e) {
  if (e.isPopupTrigger()) {
   action.show(e.getComponent(), e.getX(), e.getY());
  }
 }
}

16. (EXTRA)  Write a good question with the correct answer which your really expected on this test.  (+5 pts)
Hopefully the above questions are examples of good questions with the correct answers that you expected on this test.  Answers to this question probably should come the review topics (or possibly something you thought I left out of those review topics).