class WashMach { // Static members go with the class (shared among // all instances of the WashMach class). private static double totalSales; // total sales private static int numMachines; // total machines private String name; // which machine is it? private String contents; // what's in the machine public WashMach (String machName) { name = machName; numMachines++; } public void add (String item) { contents = item; } // Sample static method - modifies static member. public static void recordSale (double amount) { totalSales += amount; } public void printInfo () { System.out.print ("Machine " + name); System.out.println (" contains " + contents); System.out.println ("Totals: machines=" + numMachines + ", sales=$" + totalSales); // Demonstrates a static method call. TestWash3.doNothing ("junk"); } }
class TestWash3 { public static void main (String[] args) { WashMach mach1 = new WashMach ("01"); WashMach mach2 = new WashMach ("02"); mach1.add ("sock"); mach2.add ("towel"); mach1.recordSale (10.25); WashMach.recordSale (5.50); WashMach.recordSale (5.25); mach1.printInfo(); mach2.printInfo(); } // Sample static method - just prints its paramter. public static void doNothing (String data) { System.out.println ("doNothing param:" + data); } }
Machine 01 contains sock Totals: machines=2, sales=$21.0 doNothing param:junk Machine 02 contains towel Totals: machines=2, sales=$21.0 doNothing param:junk
[ CSC108 Home | Outline | Announcements | Newsgroup | Assignments | Lectures | Marks | Links ]
© Copyright 2000. All Rights Reserved.