CPSC-102: Computing and Algorithms II
Programming Assignment 1
Summer 1998
In this programming assignment, you will demonstrate your knowledge of
material covered in CPSC-101 (and re-acquaint yourself with the Java
environment) by creating a simple database query system for
a video store.
File Input
Your program will accept a single argument on the command line, which
is a non-empty string giving the name of a file in the current
directory.
This file will contain at most 20 descriptions of movie videos.
Each video will be represented by 4 lines:
- The first line contains a string representing the
title of the movie.
- The second line contains an integer representing
the year the movie was released.
- The third line contains a integer representing
the running time of the movie (in minutes).
- The fourth line contains the string ``true'' or ``false'',
answering the following question: Is this video currently
in the store (i.e. not checked out)?
Your program will begin by reading in this information and storing it
internally in an appropriate format. (See Internal
Requirements.)
Interactive Input
Your program will then interact with the user running the
program, offering the user a menu of commands. The following
commands should be offered at this time:
- Search the database for a movie. If selected, the program should
ask the user for the title of the movie. Using that ID, the program
should print the video's information if it exists in the database, and
an appropriate message if it does not.
- Print the database. If selected, the program
should print the entire contents of the database in an
appropriate format.
- Exit the program. If selected, the program should
terminate.
Internal Requirements
As always, your program should use good style, as defined
by the CPSC-102 Style Requirements
handout.
Your program should validate input whenever possible. For
example:
- You should check that the user provides a single argument on the
command line.
- You should check that the user provides valid options during the
interactive phase of the program.
Your program should catch (and handle appropriately) all
exceptions generated by any system routines you use, as
well as any exceptions you generate yourself. (That is,
your main method should not throw any exceptions.)
Your program should be designed with modifiability in mind. We
will be revising and extending this program several times throughout
the semester; consequently, it is to your benefit to design
your program in as modular a fashion as possible. In particular:
- You should define a class Video which contains
members corresponding to a given video, and methods which deal
with books (e.g. create, print, getTitle).
- You should define a class Database which contains
members which allow one to manipulate a collection of \texttt{Video}
objects (e.g. search, print). The Database class
should never access the internal members of the Video class
directly.
- You should define a class Prog1 with a main
method which calls the Database class to perform the
required operations. The Prog1 class should never
access the members of the other classes directly.
You will be replacing these classes several times through the
semester as we learn about different data structures and techniques.
Thus, it is to your advantage to make your design as modular as
possible.
A Sample Session
Here is a sample data file which could be read by this program:
Star Trek: First Contact
1996
110
true
Citizen Kane
1941
119
true
Chariots of Fire
1981
123
false
Blues Brothers, The
1980
133
false
And here is a sample interactive session:
nova -: java Prog1 datafile
Welcome to the Kettering Video Database: Version 1.0
Current available commands:
1 --> Search for a video
2 --> Print all videos
9 --> Exit
Your choice? 2
Database:
Title: Star Trek: First Contact
Released: 1996
Runtime: 110 minutes
Available: Yes
Title: Citizen Kane
Released: 1941
Runtime: 119 minutes
Available: Yes
Title: Chariots of Fire
Released: 1981
Runtime: 123
Available: No
Title: Blues Brothers, The
Released: 1980
Runtime: 133
Available: false
End of Database.
Current available commands:
1 --> Search for a video
2 --> Print all videos
9 --> Exit
Your choice? 1
Title to find? Chariots of Fire
Title: Chariots of Fire
Released: 1981
Runtime: 123
Available: No
Current available commands:
1 --> Search for a video
2 --> Print all videos
9 --> Exit
Your choice? 1
Title to find? Titanic
The specified video could not be found.
Current available commands:
1 --> Search for a video
2 --> Print all videos
9 --> Exit
Your choice? 9
nova -:
Notice that this is a sample run; your run may operate
differently as long as it fulfills the requirements outlined above.
You should create several input files in order to test your program
thoroughly; do not assume that simply testing the program on the input
file shown above is sufficient to test your program. (What happens if
the input file is empty? What if it contains too many entries? How
will you handle these cases?)
Submitting Your Program
Before 11:59:59 p.m., Thursday, 16 July 1998 (2nd Thursday), you
must email a single file to jhuggins@kettering.edu
containing all source code files for your program, including a file
named Prog1.java containing a main method.
In addition, you must deliver to the instructor a printout of your
program files at the start of class on Friday, 17 July 1998 (2nd
Friday).
Notes
- Start Early! The intent of this program is to make sure
you are familiar with Java and our Java environment. However, there
are some new features in the program (e.g. file input,
exception handling), and some new style requirements,
that may catch you if you are not careful.
- The currently installed version of the Java Development Kit
is version 1.1.6. To make sure that you are using the correct
version, type which javac at a Unix prompt. If
the answer returned is /usr/local/lang/jdk1.1.6/bin/javac,
you are using the correct version. If this answer returned
is anything else, delete any reference to the JDK from your
.cshrc files, logout, and login again. If this does
not work, see the instructor for assistance.
-
Recall that the main method for a Java program begins
as follows:
public static void main(String[] args) ...
args is an array of strings; the elements of this array
are the command-line arguments given to this program.
args.length gives the length of the array, i.e., the
number of command-line arguments supplied to the program.
-
Input and output in Java is handled via the standard Java
classes. One convenient method for reading lines of input
from the console and from files is the following:
import java.io.*;
...
BufferedReader console =
new BufferedReader(new InputStreamReader(System.in));
BufferedReader fileInput =
new BufferedReader(new FileReader("foo.bar"));
...
String inputLine = console.readLine();
Note that FileReader throws a FileNotFoundException,
which must be caught in your program.
For more information on all of the above, see the Java API in
file:/usr/local/lang/jdk1.1.6/docs/index.html
on the Unix machines, or
http://java.sun.com/products/jdk/1.1/docs/index.html.
Jim Huggins
/ jhuggins@kettering.edu
last updated: 7 July 1998