CS230 Data Structures, Wellesley College

Assignment 3

Due: Monday, October 6, in class

[CS230 Home Page] [Syllabus] [Assignments] [Documentation] [CS Dept.]

Background

In this assignment, you will use Input/Ouput and LinkedLists to create a system to manage stock quotes based on real historical stock data that can be obtained from the Internet.

In order for your system to support queries about stock prices, you will need to access historical stock data from the Internet. Yahoo Finance is one of several web sites from which such data can be downloaded. The following URL is a template that can be used to download stock price information for a particular stock during a given range of time:

      http://ichart.finance.yaho.com/table.csv?s=<stock-symbol>&a=<start-month>
      &b=<start-day>&c=<start-year>&d=<stop-month>&e=<stop-day>&f=<stop-year>
      &g=<frequency-code>&ignore=.csv

In this template,

For example, the URL used in the following example specifies daily stock information for Google from Dec 22, 2006 to Jan. 16, 2007:

      [btjaden@puma Stocks] java FileOps displayWebPage "http://ichart.finance.yahoo.com/table.csv?s=GOOG&a=11&b=22&c=2006&d=0&e=16&f=2007&g=d&ignore=.csv"

      Date,Open,High,Low,Close,Volume,Adj Close
      2007-01-16,507.55,513.00,503.30,504.28,7568900,504.28
      2007-01-12,501.99,505.00,500.00,505.00,4473700,505.00
      2007-01-11,497.20,501.75,496.18,499.72,7208200,499.72
      2007-01-10,484.43,493.55,482.04,489.46,5968500,489.46
      2007-01-09,485.45,488.25,481.20,485.50,5381400,485.50
      2007-01-08,487.69,489.87,482.20,483.58,4754400,483.58
      2007-01-05,482.50,487.50,478.11,487.19,6872100,487.19
      2007-01-04,469.00,483.95,468.35,483.26,7887600,483.26
      2007-01-03,466.00,476.66,461.11,467.59,7706500,467.59
      2006-12-29,462.10,464.47,459.86,460.48,2559200,460.48
      2006-12-28,467.12,468.58,462.25,462.56,3116200,462.56
      2006-12-27,460.00,468.08,459.10,468.03,4231500,468.03
      2006-12-26,456.52,459.47,454.59,457.53,2074300,457.53
      2006-12-22,457.50,458.64,452.73,455.58,3988300,455.58

The information is provided in tabular format known as comma-separated-value (CSV) format, in which each row is a line of text having columns that are separated by commas. The first line of the table lists titles for the columns and the remaining lines are the content rows of the table. In this assignment, you will focus on only the first and last columns. The first column specifies a date in the format YYYY-MM-DD1, while the last column gives the so-called adjusted closing price2 of the stock for that date. For example, on Jan. 3, 2007, the adjusted closing price of Google stock was $467.59.

Note that the information provided by Yahoo is in reverse chronological order - the more recent dates are listed earlier in the table. Also note that information is provided only for dates on which the stock exchange was open. In the above example, Dec. 22 is a Friday, and the exchange was closed for the Dec. 23/24 weekend, Christmas (Mon. Dec. 25), the Dec. 30/31 weekend, New Year's (Mon. Jan. 1 and Tues Jan. 2), the Jan. 6/7 weekend, the Jan. 13/14 weekend, and Martin Luther King day (Mon. Jan. 15).

It appears that the database of stock prices supplied by Yahoo only goes back to 1962. Yahoo ignores any dates specified before this time and any dates specified after the current date. So you can get all daily stock information for a given stock by specifying any year before 1962 as the <start-year> (e.g., 1900), any year after the current year as the <stop-year> (e.g., 2020), and d as the <frequency-code>. For example:

      [btjaden@puma Stocks] java FileOps displayWebPage "http://ichart.finance.yahoo.com/table.csv?s=GOOG&a=0&b=1&c=1900&d=0&e=1&f=2020&g=d&ignore=.csv"

      Date,Open,High,Low,Close,Volume,Adj Close
      2008-09-26,428.00,437.16,421.03,431.04,5292500,431.04
      2008-09-25,438.84,450.00,435.98,439.60,5020300,439.60
      ... many lines omitted ...
      2004-08-20,101.01,109.08,100.50,108.31,11428600,108.31
      2004-08-19,100.00,104.06,95.96,100.34,22351900,100.34

Google stock was introduced on Aug. 19, 2004, so the records don't go back any farther than this for Google.

The tasks for this assignment lead you through the construction of a simple stock quote system that uses data from the Yahoo finance site. First, copy the /home/cs230/downolad/Stocks directory on puma to your own CS230 subdirectory. This directory contains a subdirectory named SampleSolution that has several class files, which you can execute, that contain a sample solution to the assignment. In addition, the directory contains a helper class, CS230Date, which helps manage operations for dates. You should read the contract for the CS230Date class. You will be creating two new files from scratch for this assignment: StockQuote.java and StockInfo.java.

StockQuote.java

A stock quote is the price of a stock at a specified point in time. In this assignment, a stock quote will always refer to the adjusted closing price of a stock on a particular day.

In this problem, your task is to create a StockQuote class in a file StockQuote.java that you write from scratch. The StockQuote class must implement the StockQuote contract found here. Each instance of the StockQuote class simply pairs a date with a stock price on that date. (The stock-ticker symbol is not mentioned in the StockQuote instance.) Dates are represented as instances of the CS230Date class, which has already been implemented for you.

Below are some sample statements and expressions involving StockQuote instances. The notation E ⇒ V means that expression E evaluates to value V.

StockQuote q = new StockQuote(CS230Date.fromString("2007-06-15", 522.7);
q.date().toString() ⇒ "2007-06-15"
q.price() ⇒ 522.7
q.toString() ⇒ "2007-06-15      522.7"
StockQuote q2 = new StockQuote(CS230Date.fromString("2007-06-14", 502.84));
StockQuote q3 = new StockQuote(CS230Date.fromString("2007-06-15", 522.7));
StockQuote q4 = new StockQuote(CS230Date.fromString("2007-06-20", 509.97));
StockQuote q5 = new StockQuote(CS230Date.fromString("2007-06-20", 522.7));
StockQuote q6 = new StockQuote(CS230Date.fromString("2007-06-21", 531.00));
q.equals(q2) ⇒ false ; Similar if q2 is replaced by q4, q5, or q6
q.equals(q3) ⇒ true
q.compareTo(q2) ⇒ a positive integer ; similar if q2 is replaced by q4
q.compareTo(q3) ⇒ 0
q.compareTo(q5) ⇒ a negative integer ; similar if q5 is replace by q6
; From low to high, the above quotes are ordered: q2, q4, q=q3, q5, q6

In addition to implementing all methods in the StockQuote contract, you should write a main method that tests your methods.

StockInfo.java

In this problem, your task is to create a StockInfo class in a file StockInfo.java that you write from scratch. The StockInfo class must implement the StockInfo contract found here. Conceptually, each instance of the StockInfo class has two pieces of information:

  1. The stock-ticker symbol for the stock (e.g., GM or GOOG).
  2. The historical daily adjusted-closing-price quotes for the stock, as determined from the Yahoo finance site. For example, the Yahoo finance site has 1,035 quotes for Google from 2004-08-19 to 2008-09-26.

Additional information and requirements for your StockInfo class are listed below.

  • Include the following import statements at the top of StockInfo.java:
    import java.io.*;    // Allows use of BufferedReader, InputStreamReader
    import java.util.*;  // Allows use of LinkedList
    import java.net.*;   // Allows use of URL
  • In the constructor, you should read the historical stock data from the Yahoo finance site using the I/O tools described in lecture and lab on September 22-24. If an IOException is raised when trying to read from the Yahoo site, then a new RuntimeException should be thrown:
        throw new RuntimeException("*** include appropriate error message here ***");
  • You should use a LinkedList of StockQuote objects to store the quote information in a StockInfo instance. Because Yahoo supplies these in reverse chronological order, it is convenient to store them sorted in reverse chronological order as well. Don't forget that the first line of a Yahoo stock table is a list of titles of the form Date,Open,High,Low,Close,Volume,Adj Close.
  • In addition to the LinkedList that stores all of the the quote information in reverse chronological order, you may find it helpful to create a second LinkedList that has all of the same quote information, but sorted by StockQuote objects (note: the compareTo instance method of the StockQuote class will be useful here). Such a LinkedList of StockQuote objects sorted by quotes will be particularly helpful to have when implementing the methods getMinQuotes and getMaxQuotes.
  • The main method of the StockInfo class should accurately implement the main method described in the StockInfo contract. The interactive program with keyboard input from the user can be implemented using the I/O tools described in lecture and lab on September 22-24.
  • In the REPL in the main method, if the first argument entered by the user is the command max or the command min, and the second argument is a string representation of an integer, then the integer value must be determined for the second argument, which is a String. To convert a String to an integer, the class method parseInt of the Integer class is helpful. See the Integer class contract for more information.
  • Feel free to define any private helper methods that you find useful.
  • A test version of StockInfo.class is provided in the SampleSolution subdirectory of the Stocks directory. You can use it to test the behavior of the StockInfo program.

A sample transcript of the StockInfo program demonstrating its execution


[cs230@puma] java StockInfo GOOG
[StockInfo: (GOOG), 1035 quotes between 2004-08-19 and 2008-09-26]

GOOG> ???
Type one date (YYYY-MM-DD) in the range [2004-08-19, 2008-09-26]
  to get the nearest quote for that date.
Type two dates to get a listing of all quotes between those dates.
Type "max n" (where n is an integer) to get the n largest daily closing prices.
Type "min n" (where n is an integer) to get the n smallest daily closing prices.
Type "quit" to exit this interactive program.

GOOG> 2004-08-19
2004-08-19      100.34

GOOG> 2008-09-26
2008-09-26      431.04

GOOG> 2006-12-25
2006-12-22      455.58

GOOG> 2008-12-12
The date 2008-12-12 is outside the range [2004-08-19, 2008-09-26] of known quotes.

GOOG> 2004-01-01 2004-08-31
2004-08-19      100.34
2004-08-20      108.31
2004-08-23      109.4
2004-08-24      104.87
2004-08-25      106.0
2004-08-26      107.91
2004-08-27      106.15
2004-08-30      102.01
2004-08-31      102.37

GOOG> 2008-09-17 2008-10-31
2008-09-17      414.49
2008-09-18      439.08
2008-09-19      449.15
2008-09-22      430.14
2008-09-23      429.27
2008-09-24      435.11
2008-09-25      439.6
2008-09-26      431.04

GOOG> 2010-01-01 2007-09-04

GOOG> max 10
The 10 largest daily closing prices for GOOG are:
2007-11-06      741.79
2007-11-07      732.94
2007-11-05      725.65
2007-12-10      718.42
2007-12-06      715.26
2007-12-07      714.87
2007-11-02      711.25
2007-12-26      710.84
2007-10-31      707.0
2007-11-01      703.21

GOOG> min 5
The 5 smallest daily closing prices for GOOG are:
2004-09-03      100.01
2004-09-01      100.25
2004-08-19      100.34
2004-09-02      101.51
2004-09-07      101.58

GOOG> quit

[cs230@puma] 

What to turn in

On the day the assignment is due, bring to class a hardcopy of your final StockQuote.java and StockInfo.java code files. Also, drop off an electronic copy of your code file by connecting to your Stocks directory and executing the following command:

submit cs230 Stocks *.*