Java for Programmers: Assignment One
Fall Quarter 2003
Prof. Jeff Sonstein


Background

Given a program TestHarness.java which looks like this:


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

public class TestHarness extends Applet {

  private RandomNumbers myCurve;
  private TextArea myMessages = new TextArea();
  private String statusString = "Random Numbers in a Gaussian Distribution";

  public TestHarness() {
    setLayout( new BorderLayout() );
    add( myMessages, BorderLayout.CENTER );
    add( new Label( statusString ), BorderLayout.SOUTH );
    // add( new MyButton( myMessages ), BorderLayout.SOUTH ); 
    myCurve = new RandomNumbers();
    int counter = 1;
    for ( Enumeration x = myCurve.getNumbers(); x.hasMoreElements(); ) {
      myMessages.append( counter + ":  " + (Double)x.nextElement() + "\n" );
      counter++;
    }
  }

  public static void main( String[] args ) {
    Frame myFrame = new Frame ( "TestHarness.java" );
    myFrame.setSize( 300, 300 );
    myFrame.add( new TestHarness() );
    myFrame.addWindowListener( new WindowAdapter() {
      public void windowClosing( WindowEvent e ) {
        System.exit( 0 );
      }
    } );
    myFrame.show();
  }

}
      

Basic Assignment

30 points

Create a new class called RandomNumbers which meets the following specifications:

  1. it must be called RandomNumbers (yes, upper/lower case does matter)
  2. it must have a private constant of type int which is set to 100
    (hint: declare something as a constant by using the keywords static final)
  3. it must have a private instance variable which is a java.util.Vector
    (hint: look in the javadocs you were supposed to have downloaded and installed at the entry for java.util.Vector)
  4. it must have a public method which is called setNumbers() which uses a java.util.Random object to fill the Vector with a Gaussian distribution of numbers (exactly as many as the constant)
    (hint: look in the javadocs you were supposed to have downloaded and installed at the entry for java.util.Random)
  5. it must have a public method which is called getNumbers() which returns a java.util.Enumeration of the contents of the Vector
    (hint for future work: look at the constructor for TestHarness to see how it uses a concrete instance of the interface Enumeration)
  6. it must have a no-arguments constructor which calls its own setNumbers() method
  7. I should be able to compile your randomNumbers.java file using any JDK from 1.1.8 on
    [in other words, don't use anything the Javadocs say is newer than version 1.1]
  8. Your solution to this problem should use the AWT, not Swing
  9. I should be able to to run it successfully with the supplied TestHarness file [see above]
  10. I should see a different set of exactly one hundred random numbers appear in a scrollable display component, one number per line

Extra Credit Assignment

10 points

Create a new class called MyButton which can replace the Label used in the class TestHarness, and which meets the following specifications:

  1. It should display a meaningful prompt-string on the Button
  2. Its constructor should get a reference to the TextArea in TestHarness
  3. It should contain its own RandomNumbers object
  4. When you press the Button it should clear the TextArea, it should get a new set of random numbers, and it should put the new set of random numbers into the TextArea
  5. I should be able to
    comment out the following line in testHarness.java: and uncomment the line which reads: and (with no other changes) recompile and run it successfully

How to Submit Your Work

All homework solutions should be submitted through the myCourses site. Please follow this process to submit your work:

  1. Log on to the myCourses site
  2. Select 714 from the list of classes you are signed up for
  3. Select "FILES" from the menu to the left
  4. Select the "POST FILE" button at the top
  5. If you are turning in more than one file, then please select the "Post Multiple Files" button at the top
  6. Select my name from the pull-down list marked "Post File To:"
  7. Please put your last name and the assignment number in the field marked "Notes"
  8. Use the button marked "Browse…" to find and select the file (or files) which you wish to turn in
  9. When you are ready to turn in the files, select the button marked "Submit"

What Files to Submit


Due Date/Time: Monday, 15 December 2003 [not later than midnight, East Coast U.S. time]

last updated: 5 December 2003
Prof. Jeff Sonstein