Object Editor Prasun Dewan Comp 114 Object Editor
Object. Editor Prasun Dewan Comp 114
Object. Editor • • 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; public class ABMICalculator implements BMICalculator { public double calculate. BMI (double weight, double height) { return weight/(height*height); } }
Example Main Class package main; import bus. uigen. Object. Editor; import bmi. ABMICalculator; public class ABMIDisplayer { public static void main(String[] args) { Object. Editor. edit(new ABMICalculator()); } }
ABMICalculator UI
ABMICalculator UI
Adding a library in JBuilder
Adding a library in JBuilder
Adding a library in JBuilder
Adding a library in JBuilder
Adding a library in JBuilder
Adding a library in JBuilder
Adding a library in JBuilder
Location of Libraries • http: //www. cs. unc. edu/~dewan/oe • Use Internet explorer (not Netscape) to download files • Library names – oe. jar – shapes. jar • • • oe 2. jar version 2 of oe. jar Try oe 2 first and in case of bugs use oe. jar used by comp 14 students oe has lots of bugs! Send me mail for workarounds bugs.
Location of Libraries
What-if BMI Calculations Unchanging value retyped
BMI Spreadsheet State: Data Remembered by an Object between computations
Instance Variables ABMICalculator Instance ABMISpreadsheet Instance calculate. BMI Body accesses Parameters get. BMI accesses Body Instance Variables Belong to a single method Belong to all methods of an instance local variable global variable
State-less Vs State-ful Objects Identical Instances ~ car radios without presets Different Instances ~ car radios with presets
ABMISpreadsheet public class ABMISpreadsheet { Height double height, weight; public double get. Height() { return height; } public void set. Height(double new. Height) { height = new. Height; Weight } public double get. Weight() { return weight; } public void set. Weight(double new. Weight) { weight = new. Weight; BMI } public double get. BMI() { return weight/(height*height); } }
Read-Only and Editable Properties Typed, Named Unit of Exported Object State public class C { Bean Type: T public T get. P() {. . . } public void set. P(T new. Value) {. . . } } Violates Bean Conventions Name: P Readonly Editable Getter method Setter method Conventions for new. P obtain. P • humans • tools
Properties Classification public class ABMISpreadsheet { double height; public double get. Height() { return height; Height } public void set. Height(double new. Height) { height = new. Height; } double weight; public double get. Weight() { return weight; Weight } public void set. Weight(double new. Weight) { weight = new. Weight; } public double get. BMI() { BMI return weight/(height*height); } … Read-Only Editable Independent Read-only Dependent
Calling Getter and Setter Methods • When Object. Editor 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 public class ABMISpreadsheet { double height = 1. 77; public double get. Height() { return height; } public void set. Height(double new. Height) { height = new. Height; } double weight = 77; public double get. Weight() { return weight; } public void set. Weight(double new. Weight) { weight = new. Weight; } public double get. BMI() { return weight/(height*height); } }
Tracing Method Calls public class ABMISpreadsheet { double height = 1. 77; public double get. Height() { System. out. println (“get. Height Called”); return height; } public void set. Height(double new. Height) { System. out. println (“set. Weight Called”); height = new. Height; } double weight = 77; public double get. Weight() { System. out. println (“get. Weight Called”); return weight; } public void set. Weight(double new. Weight) { System. out. println (“set. Weight Called”); weight = new. Weight; } public double get. BMI() { System. out. println (“get. BMI Called”); return weight/(height*height);
Actual Trace
Modified ABMISpreadsheet public class ABMISpreadsheet { double height, weight; public double get. Height() { return height; } public void set. Height(double new. Height) { height = new. Height; } public double get. Weight() { return weight; } public void set. Weight(double new. Weight) { weight = new. Weight; } How will above public double get. BMI() { return calculate. BMI(weight, height); UI change? } public double calculate. BMI(double weight, double height) { return weight / (height*height); | }
Properties + Class Menu
Editing in slow motion : Initial value
Editing in slow motion: text string edited
Editing in slow motion: return triggers setter and getter calls
Renaming getter method public class ABMISpreadsheet { double height, weight; public double height() { return height; } public void set. Height(double new. Height) { height = new. Height; } public double get. Weight() { return weight; } public void set. Weight(double new. Weight) { weight = new. Weight; } public double get. BMI() { return calculate. BMI(weight, height); } public double calculate. BMI(double weight, double height) { return weight / (height*height); | }
Object. Editor does not know it is getter
Reducing Access public class ABMISpreadsheet { double height, weight; double get. Height() { return height; } public void set. Height(double new. Height) { height = new. Height; } public double get. Weight() { return weight; } public void set. Weight(double new. Weight) { weight = new. Weight; } public double get. BMI() { return calculate. BMI(weight, height); } public double calculate. BMI(double weight, double height) { return weight / (height*height); | }
Height is not a readable property
Changing setter public class ABMISpreadsheet { double height, weight; public double get. Height() { return height; } public int set. Height(double new. Height) { height = new. Height; return height; } public double get. Weight() { return weight; } public void set. Weight(double new. Weight) { weight = new. Weight; } public double get. BMI() { return calculate. BMI(weight, height); } public double calculate. BMI(double weight, double height) { return weight / (height*height);
Height is not a writeable property
An alternative class public class AString. History implements String. History { public static final int MAX_SIZE = 50; String[] contents = new String[MAX_SIZE]; int size = 0; public int size() { return size; } public String element. At (int index) { return contents[index]; } boolean is. Full() { return size == MAX_SIZE; } public void add. Element(String element) { if (is. Full()) System. out. println("Adding item to a full history"); else { contents[size] = element; size++; } } } Variable number of dynamically created indexed properties
Object. Editor Conventions for Variable -Sized Collection Write method (name does not matter to OE) public interface I { public void add. Element (T t); public T element. At (int index); public int size(); } Arbitrary Type (Must be Object Type to be recognized by Object. Editor) Read methods
Initial State
Adding an element Name does not matter to Object. Editor
Adding an element
Adding an element Object. Editor calls all element. At () and getter() methods after each method call
Adding another element
Adding another element
Public Class Constants
Public Class Constants
Non Public Class Constant public class AString. History implements String. History { static final int MAX_SIZE = 50; String[] contents = new String[MAX_SIZE]; int size = 0; public int size() { return size; } public String element. At (int index) { return contents[index]; } boolean is. Full() { return size == MAX_SIZE; } public void add. Element(String element) { if (is. Full()) 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 AString. History implements String. History { public final int MAX_SIZE = 50; String[] contents = new String[MAX_SIZE]; int size = 0; public int size() { return size; } public String element. At (int index) { return contents[index]; } boolean is. Full() { return size == MAX_SIZE; } public void add. Element(String element) { if (is. Full()) System. out. println("Adding item to a full history"); else { contents[size] = element; size++; } } }
Public Instance Constant Considered part of displayable instance state. Public instance variables also displayed. Breaking of encapsulation is visible in the display!
Mixing static and dynamic properties
Mixing static and dynamic properties
Mixing static and dynamic properties
Another UI
Another UI
Another UI
New Class (edit) public class AString. History implements String. History { public static final int MAX_SIZE = 50; String[] contents = new String[MAX_SIZE]; int size = 0; public int size() { return size; } public String element. At (int index) { return contents[index]; } boolean is. Full() { return size == MAX_SIZE; } public void add. Element(String element) { if (is. Full()) System. out. println("Adding item to a full history"); else { contents[size] = element; size++; } } }
New Class (edited) public class AString. History implements String. History { public static final int MAX_SIZE = 50; String[] contents = new String[MAX_SIZE]; int size = 0; public int size() { return size; } public String element. At (int index) { return contents[index]; } boolean is. Full() { return size == MAX_SIZE; } String new. String = “”; public String get. New. String() {return new. String; } public void set. New. String(String new. Val) { add. Element(new. Val); new. String = new. Val; } …. }
New Class public class AString. History implements String. History { String new. String = ""; public void set. New. String(String new. Val ) { new. String = new. Val; add. Element(new. String); } public String get. New. String() { return new. String; } public static final int MAX_SIZE = 50; String[] contents = new String[MAX_SIZE]; int size = 0; public int size() { return size; } public String element. At (int index) { return contents[index]; } boolean is. Full() { return size == MAX_SIZE; } public void add. Element(String element) { … } }
Uninitialized properties public class AString. History implements String. History { String new. String; public void set. New. String(String new. Val ) { new. String = new. Val; add. Element(new. String); } public String get. New. String() { return new. String; } public static final int MAX_SIZE = 50; String[] contents = new String[MAX_SIZE]; int size = 0; public int size() { return size; } public String element. At (int index) { return contents[index]; } boolean is. Full() { return size == MAX_SIZE; } public void add. Element(String element) { … } }
Uninitialized properties
Non-public size() public class AString. History implements String. History { String new. String; public void set. New. String(String new. Val ) { new. String = new. Val; add. Element(new. String); } public String get. New. String() { return new. String; } public static final int MAX_SIZE = 50; String[] contents = new String[MAX_SIZE]; int size = 0; int size() { return size; } public String element. At (int index) { return contents[index]; } boolean is. Full() { return size == MAX_SIZE; } public void add. Element(String element) { … } }
Non-public size() Object. Editor discovers only one fixed property
“Recursion” in Object. Editing package main; import bus. uigen. Object. Editor; public class An. Obect. Editor. Displayer { public static void main(String[] args) { Object. Editor. edit(new Object. Editor()); } }
Object. Editor Properties
Object. Editor Properties and Commands
Newly instantiated and displayed object
Bean Methods Getters and setters shown in separate menu Usually invoked implicitly as side effect of editing
“Recursion” in Object. Editing package main; import bus. uigen. Object. Editor; public class An. Obect. Editor. Displayer { public static void main(String[] args) { Object. Editor. edit(new Object. Editor()); } } Application-independent! Object. Editor has a main method that does this automatically!
Running Object. Editor java bus. uigen. Object. Editor = Object. Editor. edit(new Object. Editor()) java bus. uigen. Object. Editor C = Object. Editor. edit (new C())
Running Object. Editor Main
Running Object. Editor Main
Running Object. Editor Main
Running Object. Editor Main
Running Object. Editor Main
Object parameters
Object parameters
Object parameters
Object parameters
Object parameters
Object parameters
Object parameters
Object parameters
Object parameters
Object parameters
Object parameters
Object parameters
Object parameters
Constructors with parameters
Constructors with parameters Interface
Constructors with parameters
Constructors with parameters
Constructors with parameters
Constructors with parameters
Constructors with parameters
- Slides: 96