Data Structure
A data structure is a class that holds fields of various type as a whole structure. Then this class can be use as a single entity, i.e. in ArrayList. There are different methods to create data structure, but the one that we prefer here has this properties:
All fields should be final
value of the fields should be pass/set using one of the constructors
no set method
This is an example:
public final class MyPoint {
public final double x, y;
public MyPoint (final int p_x, final int p_y) {
x = p_x;
y = p_y;
}
public MyPoint (final int p_x) {
this(p_x, 0);
}
}
Task
Your task is exercise 3.2.31 of the text book. You need to read data file (DJIA.csv) and store each of the records into a data type called Entry. To store all records in memory, use an ArrayList of your entries. The task is to find the average of all quantities (Open, High, Low, Close, Volume, Adj. Close) over various periods of time. The first entry on each record is the date. Read two dates from the standard input stream, compute the average of all the data between the two dates, and print them to the standard output stream. Repeat this task for every pair of dates in the input.
To create a new inner class use this template:
public final class StockPrice {
static class Entry {
public final ...;
...
public Entry (...) {
...
}
}
public static void main (String[] args) {
...
}
}
You need to read dates from the input stream and store them in a Date object. Use SimpleDateFormat to parse a string of date. Because all of the dates are in the past, we set the 2 digit year start to 100 years before the current time. This is a simple code to do so:
final Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, -100);
final SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yy");
...
df.set2DigitYearStart(cal.getTime());
...
final Date d = df.parse( your_string );
...
The only difference of this task with your previous tasks is reading data from a file. To do so, simply create a Scanner object by passing a File object instead of system.in. You need to take care of the IOException that can be occur during creating of Scanner object with a File object. This is a sample code to do so:
public static void main(final String[] args) throws IOException, ParseException {
final Scanner input = new Scanner( new File ("Filename") );
...
}
The argument of the program should be the data filename. On standard input, each pair of dates should use for computing the averages. be aware that first seven tokens in the data file are the name of the column and you should skip them in your programs (read them, but do not store them). please take a look at sample input and output and write your program with the same format of IO.
Sample Input:
command: java StockPrice DJIA.csv
standard input:
16-Mar-06 17-Mar-06
15-Mar-06 17-Mar-06
12-Mar-28 15-Mar-28
29-Feb-44 12-May-71
Sample Output:
11252.96,11309.87,11214.65,11266.45,2420899968.00,11266.45
11218.56,11292.67,11175.51,11247.55,2378266624.00,11247.55
No Data
511.32,515.08,507.81,511.42,4215199.18,511.42
Notes:
use BigDecimal for all numbers
round numbers to two digits after decimal points (use scale 2 and ROUND_HALF_UP, see BigDecimal documentation of divide command)
Dates may not be in order in DJIA.cvs but on the input first date will be always before second date
use instance method compareTo of the Date class to compare dates. see API documentation.
if no data avalable for specified range or the range is wrong, write "No Data" on the output.
use static inner classes (for Entry) to construct your data type
Notes on Static vs Instance.