CSE 373, Winter, 2016 -- University of Washington

Assignment A1: Java Warmup

Overview

In this assignment you'll write two Java programs. In the first one, you'll reacquaint yourself with your Java tools and perhaps learn a new one, such as the Eclipse Java IDE, if you are not already using it. In the second program, you'll see what goes into a simple graphical user interface, so that you'll be ready to delve more deeply into GUI construction and modification in later assignments.

Part I: Brush Up Java

Develop a Java program BrushUpJava.java that defines a class BrushUpJava with a main method that does the following:

  1. prints out a message (using System.out.println) consisting of the string "This program was developed by " and then your name.
  2. prints out "having UWNetID " and then your UWNetID.
  3. creates an instance of class Random using seed 0.
  4. generates a list of 10 random numbers (each in the range 0 to 99), using that instance.
  5. prints out the string "The first 10 random numbers: ".
  6. prints out the list of 10 random numbers, with each successive pair of numbers separated by a comma and a space ", ".
  7. sorts the numbers into nondecreasing order.
  8. prints out the string "The (sorted) first 10 random numbers: ".
  9. prints the numbers in their sorted order, using the same format as in step e.
  10. print out "The last three digits of my student number: " followed by the last three digits of your student number.
  11. reseeds the random number generator (creating a new instance of Random), using the last three digits of your student number).
  12. again generates a list of 10 random numbers, but now using the new Random instance.
  13. prints out the string "Ten new random numbers: ".
  14. prints out the new list, unsorted, in the same format as before.
  15. sorts the new list, as before.
  16. prints out the string "Now sorted: ".
  17. prints the sorted new list, as before.

Here are some additional guidelines (in response to questions and suggestions): You should have the program print out each requested item on a new line (except the lists of 10 numbers, in which all 10 numbers come on the same line). You do NOT have to implement a sorting method for this assignment. You may simply call a Java library method to sort the elements of your sequence.

Part II: GUI Time

First, create a Java project in your IDE (Eclipse is recommended), with the title RectangleAreaCalculator. Then, in its source folder place a copy of the file RectangleAreaCalculator.java. Get this program to run. This will give you a baseline from which to design and implement your own small program that includes a Java GUI. Read the webpage at Udemy.com which explains the program and provides links to various resources that might be helpful if there are things that are not clear.

Create a new project with a Java class called TimedResponseGame. Using a similar approach to that in RectangleAreaCalculator, this class should be a subclass of JFrame, and it should end up containing a GUI with two buttons. You don't need any text boxes, but you will need some JLabel instances. One button should have a green background and a textual label that says "Start". The other button should have a red background and a textual label that says "Stop". The layout for this GUI is up to you, but it should not look messy or have textual labels that cannot be easily read by the user.

Measure the time taken by the user between the clicking of the Start button and the Stop button. The following code gives and example of how to compute elapsed time in Java. You will need to write your own code that uses the System.nanoTime method and various methods of the TimeUnit class. For example, you'll need to convert to milliseconds rather than seconds.

import java.util.concurrent.TimeUnit;

public class ElapsedTime {

public static void main(String... args) throws InterruptedException {
    long startTime = System.nanoTime();
    Thread.sleep(1000 * 10);
    long difference = System.nanoTime() - startTime;
    System.out.println("Total execution time: " +
        String.format("%d min, %d sec",
            TimeUnit.NANOSECONDS.toHours(difference),
            TimeUnit.NANOSECONDS.toSeconds(difference) -
            TimeUnit.MINUTES.toSeconds(TimeUnit.NANOSECONDS.toMinutes(difference))));
    }
}
//(This code snippet comes from:
//http://memorynotfound.com/calculating-elapsed-time-java/)

The user should try to click the Stop button exactly 3 seconds after clicking the Start button. Compute the number of milliseconds between those two events and assign that value to a variable elapsedMilliseconds. Then compute the difference (in milliseconds) between the ideal time (3000 milliseconds) and the user's elapsedMilliseconds. Take the absolute value of this and assign it to a variable millisecondsOff. Also, determine whether the user clicked early or late and assign either True or False to a boolean variable wasEarly. Finally, if the value of millisecondsOff is less than or equal to 100, display, on a JLabel within the GUI, a message of the form "Nice! You were only x milliseconds y." where x is the value of millisecondsOff and y is either "early" or "late". If the value of millisecondsOff is greater than 100, display a similar message, but without the "Nice!" and "only" text. For example, it might be "You were 257 milliseconds late."

Turn-in

Turn in your two source files (BrushUpJava.java and TimedResponseGame) through our Catalyst CollectIt dropbox. This assignment is due by midnight on Monday, January 11. Late penalties apply (see the policies page for details).

Grading

In this assignment, you can earn 40 points. (Each of the two parts is worth 20 points.)

Last Updated

The extra guidelines at the end of Part I were added at 2:20 PM on January 7.