COM1204 Object-Oriented Design -- Spring 2001 Java source code, three class files, for a simple demonstration project. Created 3/31/2001 by Professor R. P. Futrelle, Northeastern University. Course pages at: http://www.ccs.neu.edu/home/futrelle/teaching/com1204sp2001/ Pages for this demo at: http://www.ccs.neu.edu/home/futrelle/teaching/com1204sp2001/demo1/ *********************************************************************** File Phone.java -- /** * Class Phone in demo1
* Receives a dial from a User. * * @author RP Futrelle * @version 1.0 */ public class Phone { public static void Main(String[] args) { // empty for now } /** * Number being dialed */ public int numberDialed = -1; /** * For now, step() doesn't try to handle calls. */ public void step() { System.out.println("Step Phone: My number dialed is " + numberDialed); } /** * Important access function.
Phone can be dialed. * @param phoneNumber The number dialed by the user. */ public void dial(int phoneNum) { numberDialed = phoneNum; System.out.println("My owner dialed me with " + phoneNum); } } *********************************************************************** File Simulation.java -- /** * Class Simulation * Sets up and runs the phone system. * * @author RP Futrelle * @version 1.0 */ public class Simulation { /** * This initializes the system and steps it a few times. */ public static void main(String[] args) { Simulation mainSim = new Simulation(); // a singleton mainSim.init(); // Initialize simulation mainSim.run(3); // Step all 3 times. } /** * For now, simply creates one user and one phone. * Later there will be collections of users and phones. */ public void init() { onlyPhone = new Phone(); onlyUser = new User(); onlyUser.giveItPhone(onlyPhone); // user needs a phone } public Phone onlyPhone; public User onlyUser; /** * Steps all system components multiple times. * @param nTimes The number of cycles the system is stepped. */ public void run(int nTimes) { for(int i=0; i < nTimes; i++){ onlyUser.step(); onlyPhone.step(); } } } *********************************************************************** File User.java -- /** * Class User in demo1
* Will dial a call when stepped.
* Must be given a phone first. * * @author RP Futrelle * @version 1.0 */ public class User { public static void Main(String[] args) { // empty for now } /** * This is the user's phone. */ public Phone myPhone; /** * Use this to assign a phone to this user. */ public void giveItPhone(Phone p) { myPhone = p; } /** * For now, step() just dials a number. */ public void step() { System.out.println("Step User: I will dial " + 333); myPhone.dial(333); } }