Agenda • • • • Re-cap object-oriented design Static attribute and static method Difference between Array and Vector Prepare problem set 3 – Walk through Problem 1 – Explain Problem 2 A 5-step guideline for object-oriented design • Step 1: read the problem statement and identify classes – Classes are nouns – Each class contains a set of attributes and methods • Step 2: identify the attributes of each class – – – – Attributes are nouns as well Is an attribute private or public? Should it be static? If an attribute is private, should we provide its access methods (getX() and setX())? 1 A 5-step guideline for object-oriented design (continue) • Step 3: Write the constructor – The purpose of a constructor is to create an instance of the class using the values provided by the user (through the constructor arguments) – It must be public with NO return type – A class can have more than one constructors – A class decides which constructor to call by the by the number of arguments passed to the constructor • Step 4: Define special methods – – – – Scope Return type Name Arguments and their data types A 5-step guideline for object-oriented design (continue) • Step 5: Create the user class – Only the user class contains the main() function – The primary purpose of the user class is to interface with the user (e.g. take input) and manage the interactions among classes public static void main(String[] args) { String text= JOptionPane.showInputDialog("Enter highest floor: "); int maxFloor = Integer.parseInt(text); text= JOptionPane.showInputDialog("Enter lowest floor: "); int minFloor = Integer.parseInt(text); Elevator one= new Elevator(maxFloor, minFloor , "One"); Elevator two= new Elevator(maxFloor, minFloor , "Two"); Elevator three= new Elevator(maxFloor, minFloor, "Three"); Controller master= new Controller(one, two, three); ………………… } 2 Static Attribute and Static Method • Every time you instantiate an instance of a class, the computer creates a copy of all the variables • However, regardless of how many objects you have created, there is only 1 copy of the static variables in the memory int A; int B; Static int C; class Dummy Constructors Methods A=1 B=1 A=2 B=2 d1 Constructors Methods d2 Constructors Methods A=3 B=3 d3 Constructors Methods Array vs. Vecor Array Vector Length Fixed Flexible Data type of elements Must be the same Can be different Create int[] Arr = new int[4] Vector Vr = new Vector(); Assign value Arr[0] = 4; Vr.addElement(p0); Access element Arr[2] Vr.elementAt(2) Length Arr.length Vr.size() 3 Static Attribute and Static Method (continue) • Since each class only has 1 copy of static variables, they should be referred to by the class name , NOT the object name System.out.println(Dummy.C); • The same is true for static method class Pipe { public static void setRoughness (double r) { …….} } class Test { public static void main (string[] args) { Pipe.setRoughness (9.8); } } Problem set 3, Problem 1 • Two classes: Fluid and Pipe Fluid Attributes: String name; double density; Constructors Two arguments Methods printData () Pipe Attributes: String name; double length; double diameter; static double roughness; final static double gravity = 9.8; Constructors Three arguments Methods printData () static setRoughness(…) static printStatistics() • We are ready to create them 4 Problem set 3, Problem 1 • Functionality of the main() function of the user class – Create an array of 3 Fluids Fluid[] substance = new Fluid[3]; substance[0] = new Fluid(“water”, 1.0E3); – Create a Vector of 3 Pipes Vector network = new Vector (); Pipe p0 = new Pipe(“AB”, 50.0, 0.5); network.addElement(p0); Is there a better way to do it? – Set the roughness of the Pipe class Pipe.setRoughness(0.0001); – Output the data Problem set 3, Problem 2 • Pipe class Attribute Regular D X L X z X r X v q Static Final X(1.0) X R X f X g = 9.8 X h X(.00001) Pb Calculated X X 5 Problem set 3, Problem 2 • Output (“water”, 100000, 1, 0.00001) Pipe AB Reynolds number: 500000.0 Friction: 0.013030745525640063 Pressure at inlet: 100000.0 Pressure at outlet: 100328.462723718 Pressure drop: 328.4627237179957 Volume flow rate: 0.19634954084936207 Pipe BC …………………….. Problem set 3, Problem 2: one solution • Design challenge: pressure calculation • Pipes and Fluids are separate classes; however, the pressure calculation depends on both Pipes, Fluids, and the input pressure from the upstream pipe. This means that pressures in the pipes must be calculated in a particular order. • Suggestion: have pipes include a references to their output pipes (using a Vector, for example) • Think about pipes as being “empty” (no flow of fluid yet), or “full” (with fluid flowing) 6 Problem set 3, Problem 2: one solution class Pipe { private Vector outflows; /* set via addOutflow() */ public void startFlow(Fluid f, double v, double inputPressure) { /* calculate pressure here */ ... /* calculate pressure of all outflows */ for (int i=0; i<outflows.size(); i++) { Pipe outPipe = (Pipe)outflows.elementAt(i); outPipe.startFlow(f, v, outputPressure); } } ... } Call startFlow() on first pipe of network only to calculate the pressures at all pipes in the network. Problem set 3, Problem 2: one solution • This is just one particular design, many others are possible • This solution delegate all pressure calculations to the startFlow() method, which just needs to be called once 7