CS 202 - Computer Science II - Fall 2005
Lab 4
Loyola College >
Department of Computer Science >
Dr. James Glenn >
CS 202 >
Labs >
Lab 4
Due
Monday, October 31st at 11:59pm.
Projects submitted after the due date will be assessed a 20% penalty per day.
Projects will not be accepted more than four days late.
Objectives
Introduction
In 1979, Atari introduced the high score table with its Asteroids
arcade game
1. The high score table showed the initials of the players
with the ten highest scores since the machine had been turned on.
This appeal to people's vanity encouraged players to spend more
quarters in an effort to achieve some small measure of fame.
Assignment
Complete the given code in the HighScores class so it
displays a high score table that is read from
a file. Your code will have to create JLabels to hold the data read
and add those labels to the appropriate panels.
You will have to
- Open the file for input.
- Write a loop that reads until it reaches the end-of-file. The
loop should:
- create a label to hold the name just read
and add that label to namesPanel,
- read the line containing the date and score,
- create and use a tokenizer to break apart that line,
- create labels for the date and score and add them to
rightPanel (note that dates are given in seconds since
January 1, 1970, and that they are longs), and
- read the next line (the next name) from the file.
- Finally, your code should close the file.
You will also need to add the appropriate import statements, and
use try and catch to handle any exceptions
that are thrown by the code you wrote.
Files
You should start with the given code for
HighScores.java.
Here is a sample high scores file.
Advice
You can start by writing code that displays the names only to make sure
you are opening the file correctly and that your try
and catch blocks are correct. This may be easier if
you assume that the file will always contain 10 high scores.
Once that works, you can add code that uses a StringTokenizer
to handle the date and score.
Finally, modify your loop so it reads until the end of the file instead
of always reading 10 entries. Check your modification editing the file
so it contains more than 10 entries and edit it again so it contains
fewer than 10 entries.
Extra Credit
In main, use a JFileChooser to allow the user to select
what file to open. The
Java 1.4.2 documentation
has good sample code for using JFileChoosers.
Exercises
To be done individually.
- If the data for one entry (name, date, and score) were stored in the
file on a single line, how would you break that line apart? That
is, how
could you tell where the name ends and the date and score begin
(taking into account short names like Madonna and long names like
Juan Antonio de la Cruz)?
- Explain what would have to be changed in the current code if
you did not want to handle exceptions in the HighScores
constructor.
- If we were to develop a class to model a high score table,
what data fields would it contain (that is, what would be required
to store in memory all of the data that is being displayed by
the application)?
Submissions
Submit the source code (.java files) for HighScores.
1 The History of Video Games,
http://www.gamespot.com/gamespot/features/video/hov/index.html,
visited Oct. 28, 2004.