CSE 500 – Fall 2004

PROGRAMMING HW4


This programming assignment requires you to define 3 classes as described below. It should be sent via email to your instructor before the due date. Programs that do not compile cannot be graded.


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


Part #1 - NYSECorporation Class

INTRODUCTION

In HW 3, you wrote a GUI that allowed the user to enter information necessary to calculate and compare the market caps of 2 companies. Programs such as this are very limited. Once they are written, they can only be used in that single capacity. In order to make code re-usable and flexible for many applications, we may use the principles of object oriented programming (OOP). Using OOP we can create a class (NYSECorporation) that will store all the data about a given corporate stock, and will perform some useful functions. NYSECorporation objects would then serve as unique data types that could be used in many different ways in all sorts of programs.

CODING

Define the NYSECorporation class such that it implements Comparable and follows the requirements below. Note that this class should be built general in nature such that it can be used effectively by many different applications:

You must use complete information hiding for all data in your class.

 

DISPLAY GUIDELINES

When displaying your information, all money should be displayed using exactly 2 decimal digits. In addition, commas must be used to separate thousands, millions, etc... for any large numbers. For help in displaying your numbers properly, you may use the NumberFormat class, which automatically formats numbers according to your liking. To use this class, remember to add the following code to the very top of your program, even before the class header:

import java.text.NumberFormat;


This will make this class available to your program. The setMaximumFractionDigits and setMinimumFractionDigits methods from this class allow us to display a real number to a specified number of decimal points. Commas are automatically inserted as numbers get large. To use the class, you will call the format method to create a String representation of the number you provide in the format you desire. This method will return such a String.  The OutputDoublesDriver class below gives an example of how one might use this class to print out a number using differing precision. Try running the program below, changing the precision and see what happens.

 

import java.text.NumberFormat;
public class OutputDoublesDriver
{
    public static void main(String[] args)
    {
      double num = 12345.6789;
      System.out.println("num unformatted: " + num);
      NumberFormat nf = NumberFormat.getInstance();
      nf.setMaximumFractionDigits(1);
      nf.setMinimumFractionDigits(1);
      String formattedNum = nf.format(num);
      System.out.println("num formatted = " + formattedNum);
    }
}

 


Part #2 – StocksManager Class

CODING

Now that we have a class for representing a single stock, it is time to create a class to manage many such objects. Define the StocksManager class such that it manages any number of NYSECorporation objects. It should have the following:

You must use complete information hiding for all data in your class.


Part #3 – StockBuilder class

CODING

Define the StockBuilder class such that is executable (has a main method) and starts a GUI. It should have a StocksManager object instance variable, and the GUI should provide the following:

·        Text Area Summary – Your StockBuilder class is going to be used to gradually build and display a StocksManager object. So, your GUI should have a text area that lists all the stocks currently stored by your StocksManager object in tabular form. This means it should look like a table, with each stock taking its own row.

·        Enter Data for a Stock – provide labeled text fields (similar to HW 3) that allow a user to enter data about a single stock. See the NYSECorporation class to see what data is necessary to be entered.

·        Add stock button – when pressed, this button will extract all the information in the text fields and use it to construct a new NYSECorporation object, which will then be added to the StocksManager object. Finally, after the StocksManager object is updated, the text area should display its updated data.

·        Sort by ticker symbol button – when pressed, this button should sort the StocksManager by ticker symbol and update the display inside the text area.

·        Sort by market cap button – when pressed, this button should sort the StocksManager by market cap and update the display inside the text area.

NOTE:            You should not be performing any redundant operations inside this class. Do not repeat what you did inside your NYSECorporation or StocksManager classes here. Take advantage of the code you wrote in those classes.

NOTE:            You may add whatever helper methods to these 3 classes that you like.

 

REMEMBER TO MAKE YOUR PROGRAM ROBUST

Make sure that your program anticipates erroneous input such that it never crashes. I ill try to enter all sorts of illegal input like letters for numbers, negative stock prices, etc... to check if your program will handle such problems. Your program should handle such cases without crashing. In addition, whenever someone enters incorrect information, you should inform the user of their error (continue to use JOptionPane).