Java Advanced Programming |
Exercise 1 |
Using the classes in the "java.util" package
Program a text-based application that reads in date/value information from a text file, whose name is given as a command-line parameter. First create a public class "DateValueVector" that extends the Java class "Vector" and contains a main-function which (without error checking for command line parameters) could look like this:
public static void main(String[] argv) { DateValueVector dvv = new DateValueVector(); dvv.readFromFile(argv[0]); dvv.print(); System.out.println("Days: " + (dvv.getNbrDays() + 1)); System.out.println("Maximum value: " + dvv.getMaxValue()); }
Also define a public class called "DateValueObject" that has two public fields - "date" (object of class Date) and "value" (a double value). Objects of this class are used for storing the date/value pairs that have been read in.
The class "DateValueVector" should contain the following public methods:
public void readFromFile(String fname) // Reads date/values from file public double getMaxValue() // Returns maximum value. public int getNbrDays() // Number of days between first and last day public int getNbrDaysBetween(DateValueObject d1, DateValueObject d2) // Number of days between two dates public void print() // Print out date/value pairs
Program output using the data in the included file "Exc1DateValues.txt":
C:\DateValueReader>java DateValueVector Exc1DateValues.txt Fri Jan 01 00:00:00 CET 1999 50.0 Tue Jan 05 00:00:00 CET 1999 55.0 Tue Jan 12 00:00:00 CET 1999 51.0 Mon Jan 18 00:00:00 CET 1999 54.0 Sat Jan 23 00:00:00 CET 1999 59.0 Mon Jan 25 00:00:00 CET 1999 57.0 Sat Jan 30 00:00:00 CET 1999 56.0 Tue Feb 02 00:00:00 CET 1999 52.0 Sun Feb 07 00:00:00 CET 1999 49.0 Days: 38 Maximum value: 59.0
Hints:
Use the "StringTokenizer" class for separating the date and the value in the line that you read in.
Use classes "Date", "DateFormat"and "Calendar" for date-related operations.
Watch out with operating-system and country dependency for dates ('.' or '/' as separators).
Reference application included in directory "Exc1Ref".
To be returned:
Mail the Java source code files to Kary.Framling@hut.fi