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:
-
it must be called RandomNumbers
(yes, upper/lower case does matter)
-
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)
-
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)
-
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)
-
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)
-
it must have a no-arguments constructor
which calls its own setNumbers() method
-
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]
-
Your solution to this problem should use the AWT, not Swing
-
I should be able to to run it successfully with the supplied TestHarness file
[see above]
-
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:
-
It should display a meaningful prompt-string on the Button
-
Its constructor should get a reference to the
TextArea in TestHarness
-
It should contain its own RandomNumbers object
-
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
-
I should be able to
comment out the following line in testHarness.java:
- add( new Label( statusString ), BorderLayout.SOUTH );
and uncomment the line which reads:
- add( new myButton( myMessages ), BorderLayout.SOUTH );
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:
- Log on to the myCourses site
- Select 714 from the list of classes you are signed up for
- Select "FILES" from the menu to the left
- Select the "POST FILE" button at the top
-
If you are turning in more than one file, then please select the
"Post Multiple Files" button at the top
- Select my name from the pull-down list marked "Post File To:"
-
Please put your last name and the assignment number in the
field marked "Notes"
-
Use the button marked "Browse…" to find and
select the file (or files) which you wish to turn in
-
When you are ready to turn in the files, select the button marked
"Submit"
What Files to Submit
- Just submit the files which you write
-
Just submit your source code (the files whose
names end in ".java")
-
Please do not turn in archived files
(just turn in the source code as plain text files, please)
Due Date/Time: Monday, 15 December 2003 [not later than midnight, East Coast U.S. time]
last updated: 5 December 2003
Prof. Jeff Sonstein