CSE 373, Winter, 2016 -- University of Washington |
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.
Develop a Java program BrushUpJava.java that defines a class BrushUpJava with a main method that does the following:
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.
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 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).
In this assignment, you can earn 40 points. (Each of the two parts is worth 20 points.)
The extra guidelines at the end of Part I were added at 2:20 PM on January 7.