Tutorial 5 Lecturer: Ken Jackson Tutor: Andria Hunter ========== Friday Tutorial Section: 980206 @1pm (MP102) Tuesday Tutorial Section: 980210 @6pm (RW142) CSC 108, Spring 1998 Tutorial notes, T5 ================================================================== Tutorial Topics: - section 4.7, CD Collection - section 4.10, Purchase Power - section 4.11, Storm Applet Note: There are a few small bugs in CD Collection. The fifth line of code should be: CD_Collection music = new CD_Collection (5, 59.69); and the eighth line should be: music.add_cds (2, 24.73); Trace the execution of the program and explain how all the parts work together to produce the final result. SECTION 4.7: CD COLLECTION ========================== CD COLLECTION OUTPUT *************** Number of CDs: 11 Value of collection: $134.75 Average cost per CD: $12.25 *************** Number of CDs: 17 Value of collection: $202.47 Average cost per CD: $11.91 /* Class CD_Collection represents a collection of compact discs, */ /* storing the number of discs and their combined value. */ class CD_Collection { private int num_cds; private double value_cds; // Creates a CD_Collection object with the specified // initial number of CDs and their total value. public CD_Collection (int initial_num, double initial_val) { num_cds = initial_num; value_cds = initial_val; } // Adds the specified number of CDs to the collection and // updates the value. public void add_cds (int number, double value) { num_cds = num_cds + number; value_cds = value_cds + value; } // Prints a report on the status of the CD collection. public void print() { System.out.println ("***************"); System.out.println ("Number of CDs: " + num_cds); System.out.println ("Value of collection: $" + value_cds); System.out.println ("Average cost per CD: $" + average_cost()); } // Computes and returns average cost of a CD in this collection. private double average_cost () { return value_cds / num_cds; } } // class CD_Collection /* Class Tunes contains the main driver of a program that */ /* demonstrates the instantiation and use of a programmer- */ /* defined class. */ class Tunes { // Instantiates an object to monitor the value of a // collection of musical CDs. public static void main (String[] args) { CD_Collection music = new CD_Collection (5, 59.69); music.add_cds (1, 10.99); music.add_cds (3, 39.34); music.add_cds (2, 24.73); music.print(); music.add_cds (2, 20.82); music.add_cds (4, 46.90); music.print(); } } // class Tunes SECTION 4.10: PURCHASE POWER ============================ PURCHASE POWER OUTPUT Manager Jim is ordering more beans Manager Bob is ordering more franks Manager Bob is ordering more franks Manager Jim is ordering more beans Manager Bob is ordering more franks Manager Bob is ordering more franks Manager Jim is ordering more beans Manager Bob is ordering more franks Manager Bob is ordering more franks Manager Jim is ordering more beans Manager Bob is ordering more franks Manager Bob is ordering more franks Manager Jim is ordering more beans Manager Bob is ordering more franks Manager Bob is ordering more franks Manager Jim is ordering more beans Manager Bob is ordering more franks Manager Bob is ordering more franks Manager Jim is ordering more beans Manager Bob is ordering more franks Manager Bob is ordering more franks Manager Jim is ordering more beans Manager Bob is ordering more franks Manager Bob is ordering more franks Manager Jim is ordering more beans Manager Bob is ordering more franks Manager Bob is ordering more franks Manager Jim is ordering more beans Manager Bob is ordering more franks Manager Bob is ordering more franks Manager Jim is ordering more beans Manager Bob is ordering more franks Manager Bob is ordering more franks Manager Jim is ordering more beans Manager Bob is ordering more franks /* Class Purchase_Power demonstrates the interaction of several */ /* objects representing store managers and the items of stock */ /* that they manage. */ class Purchase_Power { private final static int MAX_SALES = 100; private static int sales = 0; private static Manager jim = new Manager ("Jim"); private static Manager bob = new Manager ("Bob"); private static Stock_Item beans = new Stock_Item (jim, "beans"); private static Stock_Item franks = new Stock_Item (bob, "franks"); // Loops until a specific number of sales are reached. // The stock items are bought in different quantities to // show different restocking needs. public static void main (String[] args) { while (sales < MAX_SALES) { if (beans.buy (2)) sales = sales + 1; if (franks.buy (5)) sales = sales + 1; } } } // class Purchase_Power /* Class Manager represents one store manager with the */ /* ability to reorder stock items. */ class Manager { private String name; // Creates a Manager object with the specified name. public Manager (String id) { name = id; } // Orders more stock by calling the replenish method of the // specified Stock_Item object, and prints a message // indicating the transaction. public void order_stock (Stock_Item out_of_stock_item) { System.out.println ("Manager " + name + " is ordering more " + out_of_stock_item.brand()); out_of_stock_item.replenish (10); } } // class Manager /* Class Stock_Item represents one type of stock item with a */ /* particular quantity on hand in the inventory. Stock_Item */ /* objects hold a reference to the Manager object that is */ /* responsible for reordering the item as needed. */ class Stock_Item { private int inventory = 0; private String name; private Manager product_buyer; // Creates a new Stock_Item object with the specified // controlling Manager and product name. public Stock_Item (Manager controller, String product_name) { name = product_name; product_buyer = controller; } // Returns the name of the product. public String brand () { return name; } // Attempts to purchase the specified amount of the // stock item. If there is not enough in inventory, the // item is restocked by invoking the order_stock method // of the appropriate Manager object. public boolean buy (int amount) { boolean success = false; if ( amount <= inventory ) { inventory = inventory - amount; success = true; } else product_buyer.order_stock (this); return success; } // Adds the indicated amount of this stock item to the // inventory. public void replenish (int amount) { inventory = inventory + amount; } } // class Stock_Item SECTION 4.11: STORM APPLET ========================== /* Storm Class */ /* Applet to draw raindrops that keep changing size and position */ import java.applet.Applet; import java.awt.Graphics; import java.awt.*; import java.util.Random; public class Storm extends Applet { private final int MAX_COUNT = 5000; private final int BUSY_WAIT = 500; private final int APPLET_SIZE = 200; private Random position = new Random(); private Raindrop drop1; // references for 5 Raindrop objects private Raindrop drop2; private Raindrop drop3; private Raindrop drop4; private Raindrop drop5; private Graphics page; // The graphics page // The init method is called once when the applet is // first loaded into memory. (initialization code) public void init() { drop1 = new Raindrop(); // create 5 Raindrop objects drop2 = new Raindrop(); drop3 = new Raindrop(); drop4 = new Raindrop(); drop5 = new Raindrop(); show(true); page = getGraphics(); } // The start method is called every time the applet is // viewed by a browser. (code to implement rain effect) public void start() { int count = 1; int wait; setBackground(new Color(192,192,192)); //while (count < MAX_COUNT) while (true) { check_drop (drop1); check_drop (drop2); check_drop (drop3); check_drop (drop4); check_drop (drop5); count = count + 1; draw (page); wait = 0; while (wait < BUSY_WAIT) { wait = wait + 1; } } } public void check_drop (Raindrop drop) { if (drop.visible()) { drop.ripple(); } else { int x = Math.abs (position.nextInt() % APPLET_SIZE) + 1; int y = Math.abs (position.nextInt() % APPLET_SIZE) + 1; drop.set_position (x, y); } } public void draw (Graphics page) { // Clear the applet page.setColor(getBackground()); page.fillRect (0, 0, APPLET_SIZE, APPLET_SIZE); page.setColor(getForeground()); // Draw all the drops drop1.draw (page); drop2.draw (page); drop3.draw (page); drop4.draw (page); drop5.draw (page); } } // class Storm /* The Raindrop class represents a single raindrop, and */ /* contains variables that store its current position. */ class Raindrop { private final int MAX_RIPPLE = 30; private final int RIPPLE_STEP = 2; private static Random new_size = new Random(); private int current_size = 0; private int visible_size = 0; private int x = 1, y = 1; public boolean visible() { return current_size < visible_size; } public void set_position (int x_position, int y_position) { x = x_position; y = y_position; visible_size = Math.abs (new_size.nextInt() % MAX_RIPPLE) + 1; current_size = 1; } public void ripple() { x = x - RIPPLE_STEP/2; y = y - RIPPLE_STEP/2; current_size = current_size + RIPPLE_STEP; } public void draw (Graphics page) { //page.drawRect (x, y, current_size, current_size); page.drawOval (x, y, current_size, current_size); } } // class Raindrop