960 likes | 1.09k Views
ObjectEditor. Prasun Dewan Comp 114. ObjectEditor. Automatic user-interface generation. You only write computation code Separate object to do I/O Main just instantiates it Can replace it with own UI later. Serves as training wheels Serves to separate UI from computation. Example Class.
E N D
ObjectEditor Prasun Dewan Comp 114
ObjectEditor • Automatic user-interface generation. • You only write computation code • Separate object to do I/O • Main just instantiates it • Can replace it with own UI later. • Serves as training wheels • Serves to separate UI from computation
Example Class package bmi; publicclass ABMICalculator implements BMICalculator { publicdouble calculateBMI (double weight, double height) { return weight/(height*height); } }
Example Main Class package main; import bus.uigen.ObjectEditor; import bmi.ABMICalculator; public class ABMIDisplayer { publicstaticvoidmain(String[] args) { ObjectEditor.edit(new ABMICalculator()); } }
Location of Libraries • http://www.cs.unc.edu/~dewan/oe • Use Internet explorer (not Netscape) to download files • Library names • oe.jar • shapes.jar • oe2.jar version 2 of oe.jar • Try oe2 first and in case of bugs use oe.jar • oe.jar used by comp14 students • oe has lots of bugs! • Send me mail for workarounds bugs.
Unchanging value retyped What-if BMI Calculations
State: Data Remembered by an Object between computations BMI Spreadsheet
accesses accesses Belong to a single method Belong to all methods of an instance Instance Variables ABMICalculator Instance ABMISpreadsheet Instance calculateBMI getBMI Body Body Instance Variables Parameters local variable global variable
Identical Instances Different Instances State-less Vs State-ful Objects ~ car radios without presets ~ car radios with presets
ABMISpreadsheet publicclass ABMISpreadsheet { double height, weight; publicdouble getHeight() { return height; } publicvoid setHeight(double newHeight) { height = newHeight; } publicdouble getWeight() { return weight; } publicvoid setWeight(double newWeight) { weight = newWeight; } publicdouble getBMI() { return weight/(height*height); } } Height Weight BMI
Name: P publicclass C { Type: T Editable publicvoid setP(T newValue) { ... } Getter method Setter method } newP Violates Bean Conventions obtainP Read-Only and Editable Properties Typed, Named Unit of Exported Object State Bean public T getP() { ... } Readonly • Conventions for • humans • tools
Read-Only Editable Independent Editable Independent Read-only Dependent Properties Classification • publicclass ABMISpreadsheet { • double height; • publicdouble getHeight() { • return height; • } • publicvoid setHeight(double newHeight) { • height = newHeight; • } • double weight; • publicdouble getWeight() { • return weight; • } • publicvoid setWeight(double newWeight) { • weight = newWeight; • } • publicdouble getBMI() { • return weight/(height*height); • } • … Height Weight BMI
Calling Getter and Setter Methods • When ObjectEditor window is created • Getter method of each property called to display initial value of property • When property is changed to a new value • Setter method of property is called with new value as actual parameter • Getter method of each property is called to refresh display
Calling Getter and Setter Methods publicclass ABMISpreadsheet { double height = 1.77; publicdouble getHeight() { return height; } publicvoid setHeight(double newHeight) { height = newHeight; } double weight = 77; publicdouble getWeight() { return weight; } publicvoid setWeight(double newWeight) { weight = newWeight; } publicdouble getBMI() { return weight/(height*height); } }
publicclass ABMISpreadsheet { double height = 1.77; publicdouble getHeight() { System.out.println (“getHeight Called”); return height; } publicvoid setHeight(double newHeight) { System.out.println (“setWeight Called”); height = newHeight; } double weight = 77; publicdouble getWeight() { System.out.println (“getWeight Called”); return weight; } publicvoid setWeight(double newWeight) { System.out.println (“setWeight Called”); weight = newWeight; } publicdouble getBMI() { System.out.println (“getBMI Called”); return weight/(height*height); } } Tracing Method Calls
How will above UI change? publicclass ABMISpreadsheet { double height, weight; publicdouble getHeight() { return height; } publicvoid setHeight(double newHeight) { height = newHeight; } publicdouble getWeight() { return weight; } publicvoid setWeight(double newWeight) { weight = newWeight; } publicdouble getBMI() { return calculateBMI(weight, height); } publicdouble calculateBMI(double weight, double height) { return weight / (height*height); | } Modified ABMISpreadsheet
Editing in slow motion: return triggers setter and getter calls
publicclass ABMISpreadsheet { double height, weight; publicdouble height() { return height; } publicvoid setHeight(double newHeight) { height = newHeight; } publicdouble getWeight() { return weight; } publicvoid setWeight(double newWeight) { weight = newWeight; } publicdouble getBMI() { return calculateBMI(weight, height); } publicdouble calculateBMI(double weight, double height) { return weight / (height*height); | } Renaming getter method
publicclass ABMISpreadsheet { double height, weight; double getHeight() { return height; } publicvoid setHeight(double newHeight) { height = newHeight; } publicdouble getWeight() { return weight; } publicvoid setWeight(double newWeight) { weight = newWeight; } publicdouble getBMI() { return calculateBMI(weight, height); } publicdouble calculateBMI(double weight, double height) { return weight / (height*height); | } Reducing Access
Changing setter publicclass ABMISpreadsheet { double height, weight; publicdouble getHeight() { return height; } publicint setHeight(double newHeight) { height = newHeight; return height; } publicdouble getWeight() { return weight; } publicvoid setWeight(double newWeight) { weight = newWeight; } publicdouble getBMI() { return calculateBMI(weight, height); } publicdouble calculateBMI(double weight, double height) { return weight / (height*height); | }
An alternative class • public class AStringHistory implements StringHistory { • public staticfinalint MAX_SIZE = 50; • String[] contents = new String[MAX_SIZE]; • int size = 0; • publicint size() { return size;} • public String elementAt (int index) { return contents[index]; } • boolean isFull() { return size == MAX_SIZE; } • publicvoid addElement(String element) { • if (isFull()) • System.out.println("Adding item to a full history"); • else { • contents[size] = element; • size++; • } • } • } Variable number of dynamically created indexed properties
Write method (name does not matter to OE) Arbitrary Type (Must be Object Type to be recognized by ObjectEditor) Read methods ObjectEditor Conventions for Variable-Sized Collection • publicinterface I { • publicvoid addElement (T t); • public T elementAt (int index); • publicint size(); • }
Adding an element Name does not matter to ObjectEditor
Adding an element ObjectEditor calls all elementAt () and getter() methods after each method call
Non Public Class Constant • public class AStringHistory implements StringHistory { • staticfinalint MAX_SIZE = 50; • String[] contents = new String[MAX_SIZE]; • int size = 0; • publicint size() { return size;} • public String elementAt (int index) { return contents[index]; } • boolean isFull() { return size == MAX_SIZE; } • publicvoid addElement(String element) { • if (isFull()) • System.out.println("Adding item to a full history"); • else { • contents[size] = element; • size++; • } • } • }
Non-Public Class Constant Non constants menu
Public Instance Constant • public class AStringHistory implements StringHistory { • publicfinalint MAX_SIZE = 50; • String[] contents = new String[MAX_SIZE]; • int size = 0; • publicint size() { return size;} • public String elementAt (int index) { return contents[index]; } • boolean isFull() { return size == MAX_SIZE; } • publicvoid addElement(String element) { • if (isFull()) • System.out.println("Adding item to a full history"); • else { • contents[size] = element; • size++; • } • } • }