CSE 500 – Fall 2004

PROGRAMMING HW5


This programming assignment requires you to write code in as many classes as you see fit, but understand that you will be graded on the strength of your design as well as implementation. By that I mean you should use Object Oriented design principles we've discussed this semester to carefully construct your classes such that they are re-usable, easy to understand, and extensible. You may use any data structures you like for storing and manipulating your data.


ATTENTION: Any changes made to this document from the original posting are denoted in blue.

 

NOTE: I have changed the Dow Jones Web site link where you should extract your data. The page is in a nearly identical format, though there are some slight changes. The new, shorter link is:

·        http://djindexes.com/mdsidx/index.cfm?event=showComponentWeights&rptsymbol=DJI&sitemapid=20

 

 


What will your program do?

In this assignment, you will create an application with a Graphical User Interface (GUI) that serves three main functions:

1.      Read and display date of data – It will get the date from a Web page where you will read the necessary data for this assignment and display this date inside your GUI.

2.      Computation of Dow Jones Industrial Average – It should compute and present the DJIA based on the most recent numbers (as retrieved from the Web).

3.      Textual Dow Jones Component Summary – It should present a textual summary of the DJIA components based on the most recent numbers.


Introduction to URLs & URLConnections

In this assignment you will have to read an HTML file on the Web and parse it to extract information important for your program. To do so, you should use the java.net.URL & java.net.URLConnection classes to connect to a Web server, and then use the java.IO.BufferedReader class to read an HTML file from the server line by line. Using these classes is quite straightforward. For example, to print all the contents of this HW file to the console window, you would use the following algorithm:

1.                  Construct a valid URL object providing the Web location of the file to be read

2.                  Open a connection for the constructed URL to be represented by a URLConnection object

3.                  Get an input stream from the URLConnection and use it to construct a textual-reading stream object (BufferedReader)

4.                  Read text line by line from the stream, printing as you go. This part uses the BufferedReader object.

In your program, you would use a similar algorithm every time you wish to fetch information about the Dow components, the difference being that in Step 4, instead of simply printing the text, you would examine each line to find particular information you wish to extract. You may find the String and StringTokenizer classes useful for this purpose. BufferedReader & StringTokenizer are both Iterators of sorts, BufferedReader reads a line of text at a time in order, while StringTokenizer reads chunks of a line of text in order. Using the algorithm above, we could write the code below. Try running this program as an example such that you understand how to use these classes:

import java.io.*;

import java.net.*;

 

public class WebPageRetriever

{

  public static void main(String[] args)

  {

    String path = "http://www.cs.sunysb.edu/~cse500/Assignments/hw5.html";

    URL url;

    URLConnection connect;

    InputStream is;

    BufferedReader br;

 

    try

    {

      url = new URL(path);

      connect = url.openConnection();

      is = connect.getInputStream();

      br = new BufferedReader(new InputStreamReader(is));

 

      String inputLine = br.readLine();

      while (inputLine != null)

      {

        System.out.println(inputLine);

        inputLine = br.readLine();

      }

    }

    catch(IOException ioe) { ioe.printStackTrace(); }

  }

}


Introduction to The Dow Jones Industrial Average

 

What is the Dow Jones Industrial Average (DJIA)?

In case you’ve been living in a cave, the Dow Jones Industrial Average is one of the oldest (since 1896) and perhaps the most well known financial index in the world. Its purpose is to serve as an indicator of the health of the US financial markets as a whole.

 

What are the DJIA components?

To calculate the DJIA score, the stock prices of 30 carefully selected large market cap (blue-chip) stocks are used. These 30 companies represent about 1/5 of the approximately $8 trillion market value of all US stocks, so though few companies are used in the calculation, it does reflect a large portion of the market. These 30 stocks are periodically changed, in fact recently AT&T, International Paper, and Eastman Kodak were booted out of the Dow calculation and replaced with American International Group, Pfizer Inc, and Verizon Communications. For a complete list of the 30 Dow components, go to dowjones.com.

 

How is the DJIA calculated?

The calculation is actually quite simple (albeit stupid in my humble opinion). It is done by summing the stock prices of all the Dow components and then dividing this sum by a number known as the Dow Divisor. The current Dow Divisor, which you may assume is a constant for your program, is 0.13561241. This number changes every time Dow components are replaced, split, or consolidated to normalize the score. So for the Dow at the close of trading on Thursday December 2nd 2004, the Dow would be:

(  79.91 (price per share of 3M)

+ 33.43 (price per share of Alcoa)

+ …

+ 27.62 (price per share of Disney))/ 0.13561241 = 10,166.38

 

Why is this calculation strange?

It is a stock price weighted score. It doesn’t take market cap into account. So, even though Microsoft has a greater market cap than 3M, since 3M shares sell for 3 times as much on a per share basis, it has 3 times as great an impact on the Dow as Microsoft does. This has led to much criticism of the Dow as being an inaccurate reflection of the market. Those making such claims would prefer the Standard and Poor’s 500 (S & P 500) index, where influence on the index score coincides with market cap. For more information on the Dow, refer to investopedia.com.

 

Where can we get the “most recent numbers”, a.k.a. the information about the Dow components from the end of the previous day’s trading?

The dowjones.com site posts such numbers daily. It’s a long, ugly URL, but the HTML on the page is nice and predictable, which is important for your hw. To make use of the page, we will have to do some parsing of that Web document to extract the necessary information. Of course, first we have to know a thing or two about the structure of the page and its HTML code. To look at the source code for aforementioned Web page, from Internet Explorer go to the View menu and select Source. This will open the Web page as a text file in Notepad (or some other text editor). Now look at the source code. Specifically, look at where the data about the Dow components appears. From this table, each time your application runs it must extract:

·        Date – The date corresponding to the DOW data we are using (to be found in Web page).

·        Company – The name of each DOW component.

·        Ticker – The ticker symbol for each DOW component

·        Primary Group – The business sector for each DOW component. For example, McDonald’s is part of the Restaurant group.

·        US $ Close – The price per share for each DOW component at closing time for the last trading day.

NOTE:            Your program must extract the information above every time your program runs. Hard-coding any of this information will be reason for not receiving credit. Keep in mind you have no idea what date I will run your program, yet it should work every time. Once extracted, you may place the data in any objects and data structures you like.

ADVICE – To figure out how to extract information from this HTML document, you’re going to have to look at it and find certain patterns that you can use to your advantage. If you look through the tables in raw HTML for the Dow components, you will find patterns in the way data is listed. Patterns exist because these table rows were generated automatically using the same formatting, which you can then anticipate when looking to extract information.


Your GUI

Your GUI should use Java's Swing component libraries and the AWT graphics and event programming libraries. You may layout your GUI however you like, but you must make sure it is neat and orderly. As stated earlier, your program must do the following:

  • Read and display date of data: Your program should read the date from the source site and display it inside your GUI at all times.
  • Computation of Dow Jones Industrial Average: Your program should display the DJIA inside your GUI at all times. Obviously you will have to first read all the data from the Web page to compute this number.

GUI Tips – To display your date and DJIA you can use a few different approaches. JLabels, text drawing using AWT graphics, or even uneditable text fields would all be valid, depending on your preference.

  • Textual Dow Jones Component Summary: your program should provide a textual summary of your data, in this case, your Dow components. This summary should be provided inside your GUI, not the Java console window (don't use System.out). Your summary should include:
      • Headings for all columns of data
      • Columns listing data for each Dow component:
        • Company name for each component
        • Ticker symbol for each component
        • Industry (Primary Group) for each component
        • Price Per Share for each component

NOTE: All data displayed in your summary table should be neatly aligned.

GUI Tips – To display the summary of the Dow components, you may use a simple scrollable JTextArea. Since this information is for display purposes only, make sure your text area is not editable.

GUI ALTERNATIVE – Note that JTextAreas display plain text only and that they use a single font and color (of your choice) for all text within. To display text with multiple formatting, you would have to use a JEditorPane, and place HTML formatted text within using the setText method. Before you do so, you’ll have to specify that your JEditorPane object is using HTML code by calling setContentType("text/html") on your editor pane object. This is purely optional, I would expect most of you to use JTextAreas.

·        FINALLY – As I mentioned earlier, you may layout your components however you like, but try to be neat and user friendly.