Tutorial 8 Lecturer: Steve Bellantoni Tutor: Andria Hunter ========== 981116 @10am (RW229) @1pm (EM108) CSC108, Fall 1998 Tutorial notes, T8 ================================================================== Topics ------ - discuss Toronto Stock Exchange Example program. - answer any related questions. Overview -------- In lecture over the past week interfaces, abstract functions, abstract classes, inheritance, and overriding have been discussed. See lecture notes on Bellantoni's web pages for more info. Instructor Bellantoni has developed several examples concerning Matrix objects. The goal of tutorial #8 is to make sure everyone understands the topics listed above. Note that the diagrams in Appendix O are actually pictures of class heirarchies. Toronto Stock Exchange Example ------------------------------ When a trader sends an order to the Toronto Stock Exchange, it is a BUY, SELL, or CANCEL order. Each order has an order number. We would like to maintain a list of orders and print them out. Note that the toString function is actually defined in Object; it is overridden in Order and again in the subclasses. Some questions to think about: - Can we instantiate the Order class to create an object from this class? - What would happen if the BuyOrder class did not have a toString method? How would objects from this class be printed? - Would the program still work if the protected variable in the Order class was made private/public instead? - Why was a static variable used in the order class? - How do we know which of the two constructors will be used when a new CancelOrder object is instantiated? - What would happen if the BuyOrder method did not have an isGood() method? -------------------------------------------------------------------- // This is a class that is used to test the Order parent class, // and the three subclasses (BuyOrder, SellOrder, and CancelOrder). public class ProcessOrder { // The main method creates five objects from the various // classes listed above. It then prints each object. public static void main (String[] args) { System.out.println ("Toronto Stock Exchange Orders"); System.out.println ("============================="); System.out.println ("\nCreating BuyOrder IBM..."); BuyOrder a = new BuyOrder ("IBM", 10, 100); System.out.println (a); System.out.println ("\nCreating SellOrder TD..."); SellOrder b = new SellOrder ("TD", 20, 500); System.out.println (b); System.out.println ("\nCreating BuyOrder TDK..."); BuyOrder c = new BuyOrder ("TDK", 5, 80); System.out.println (c); System.out.println ("\nCreating CancelOrder TD..."); CancelOrder d = new CancelOrder (b); System.out.println (d); System.out.println ("\nCreating CancelOrder IBM..."); CancelOrder e = new CancelOrder (1); System.out.println (e); //System.out.println ("\nCreating Order object..."); //Order f = new Order (); //System.out.println (f); } } -------------------------------------------------------------------- // This class represents a generic order. It is the superclass // of the BuyOrder, SellOrder, and CancelOrder classes. abstract class Order { protected int orderNumber; private static int latestOrderNumber = 0; // Construct new Order object public Order() { orderNumber = ++latestOrderNumber; } // print current object public void print() { System.out.println (toString()); } // return the integer order number public int getOrderNumber() { return orderNumber; } // return String that represents this object public String toString() { return "Order #" + orderNumber; } // define abstract method which will be implemented // by any class that extends this class abstract public boolean isGood(); } -------------------------------------------------------------------- // This class represents a purchase order class BuyOrder extends Order { private String symbol; private int numShares; private double bidPrice; // Construct new BuyOrder object public BuyOrder( String symbol, int numShares, double bidPrice) { super(); this.symbol = symbol; this.numShares = numShares; this.bidPrice = bidPrice; } // return String that represents this object public String toString() { return super.toString() + " Buy " + numShares + " " + symbol + " @ " + bidPrice; } // return true if the current order is valid public boolean isGood() { return symbol.length() > 0 && symbol.indexOf(' ') == -1 && bidPrice > 0.0 && numShares > 0 ; } } -------------------------------------------------------------------- // This class represents a sale order class SellOrder extends Order { private String symbol; private int numShares; private double askPrice; // Construct new SellOrder object public SellOrder( String symbol, int numShares, double askPrice) { super(); this.symbol = symbol; this.numShares = numShares; this.askPrice = askPrice; } // return String that represents this object public String toString() { return super.toString() + " Sell " + numShares + " " + symbol + " @ " + askPrice; } // return true if the current order is valid public boolean isGood() { return symbol.length() > 0 && symbol.indexOf(' ') == -1 && askPrice > 0.0 && numShares > 0 ; } } -------------------------------------------------------------------- // This class represents a cancellation order class CancelOrder extends Order { private int cancellationOrderNumber; // Two different ways to constuct a CancelOrder object CancelOrder( Order toBeCancelled) { super(); cancellationOrderNumber = toBeCancelled.getOrderNumber(); } CancelOrder( int cancellationOrderNumber) { super(); this.cancellationOrderNumber = cancellationOrderNumber; } // return String that represents this object public String toString() { return super.toString()+" Cancel#"+cancellationOrderNumber; } // return the cancellation order number public int getCancellationOrderNumber() { return cancellationOrderNumber; } // return true if the current order is valid public boolean isGood() { return cancellationOrderNumber > 0 && cancellationOrderNumber != this.orderNumber; } } -------------------------------------------------------------------- PROGRAM OUTPUT: Toronto Stock Exchange Orders ============================= Creating BuyOrder IBM... Order #1 Buy 10 IBM @ 100.0 Creating SellOrder TD... Order #2 Sell 20 TD @ 500.0 Creating BuyOrder TDK... Order #3 Buy 5 TDK @ 80.0 Creating CancelOrder TD... Order #4 Cancel#2 Creating CancelOrder IBM... Order #5 Cancel#1 --------------------------------------------------------------------