Final Exam

CMSC 220 - I

Spring 2002


Problems :


1.

Below is the source code of the event-driven Applet class Clicks. When executing, this applet will display two buttons (initially in red and blue, respectively) and then count the number of clicks on them. On every click, the current number of clicks will be reported on the corresponding button's label, and the button's color will alternate between the red and green for the left button and between the blue and cyan for the right button. Below are the snapshots of the screens produced by this applet :

Change the applet's code appropriatelly so that:
(i)    a third middle button is introduced in between the two,
(ii)   the third button's label reports on the total number of clicks
(iii)   in addition, the third button always assumes the color of the button just clicked on (after this button changed it).

Below are the snapshots of the screens produced by your applet (solution to this problem) :

Attachment :

/* File: Clicks.java     */

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class Clicks extends Applet implements ActionListener{

  private Button  leftButton = new Button("Not Clicked On Yet");
  private Button  rightButton = new Button("Not Clicked On Yet");
  private int clickCountLeft = 0, clickCountRight = 0;

  public void init() {
     //Add leftButton to the Applet
     leftButton.setBackground(Color.red);
     add(leftButton);
     //Register Clicks as a listener for leftButton
     leftButton.addActionListener(this);

     //Add rightButton to the Applet
     rightButton.setBackground(Color.blue);
     add(rightButton);
     //Register Clicks as a listener for rightButton
     rightButton.addActionListener(this);
  }
 

  public void actionPerformed(ActionEvent e) {

    //Determine which button was clicked on
    if (e.getSource() == leftButton) {
          //Change color after each press
         if (leftButton.getBackground() == Color.red)
                 leftButton.setBackground(Color.green);
         else
                  leftButton.setBackground(Color.red);
         clickCountLeft++;
         leftButton.setLabel("Click count: " + clickCountLeft);
         // declare that the layout is no longer valid
         leftButton.invalidate();
    } else if (e.getSource() == rightButton) {
         //Change color after each press
         if (rightButton.getBackground() == Color.blue)
                  rightButton.setBackground(Color.cyan);
         else
                  rightButton.setBackground(Color.blue);
         clickCountRight++;
         rightButton.setLabel("Click count: " + clickCountRight);
         // declare that the layout is no longer valid
         rightButton.invalidate();
    }

    // have the applet re-do the layout.
    validate();

    }

} // class Clicks
 
 

(45 (=15+15+15)  points)

2.

(i)    Show the semi-equivalence of the two loops by rewrite the  for loop below using a while loop instead :

    for (int i=0; i<ZZZ; i++)
        students[i] = getString ("Please, enter the name for the student #" + Integer.toString(i+1) + ": ");

Assume that all appropriate arrangements for the students Stringarray (i.e. dimensioning, space allocation, etc...) were done properly before this statement.

(ii)    What would the value of the variable ZZZ have to be set to, in order to properly get the String values in every element of the array students?

(iii)    What is the meaning of i += 2; statement and what would the loop above do if  i++ of the header of the loop was replaced by i += 2; ?
 

(28 (=10+9+9)  points)

3.

        Explain briefly :
    (a)    Difference between elements of Java basic data types (int, float, char, ...) and objects of Java classes ?
    (b)    Difference between arrays of elements of Java basic data types (int, float, char, ...) and arrays of objects of Java classes ?
    (c)     The basic steps in creating, space alloacation and elements' initialization for arrays of elements of Java basic data types (int, float, char, ...) and then for arrays of objects of Java classes. Illustrate your explanation by an example.

(27 (=9+9+9)  points)