Presentation is loading. Please wait.

Presentation is loading. Please wait.

ObjectEditor Prasun Dewan Comp 114. ObjectEditor Automatic user-interface generation. You only write computation code Separate object to do I/O Main just.

Similar presentations


Presentation on theme: "ObjectEditor Prasun Dewan Comp 114. ObjectEditor Automatic user-interface generation. You only write computation code Separate object to do I/O Main just."— Presentation transcript:

1 ObjectEditor Prasun Dewan Comp 114

2 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

3 Example Class package bmi; public class ABMICalculator implements BMICalculator { public double calculateBMI (double weight, double height) { return weight/(height*height); }

4 Example Main Class package main; import bus.uigen.ObjectEditor; import bmi.ABMICalculator; public class ABMIDisplayer { public static void main(String[] args) { ObjectEditor.edit(new ABMICalculator()); }

5 ABMICalculator UI

6

7 Adding a library in JBuilder

8

9

10

11

12

13

14 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.

15 Location of Libraries

16 What-if BMI Calculations Unchanging value retyped

17 BMI Spreadsheet State: Data Remembered by an Object between computations

18 Instance Variables ABMICalculator Instance calculateBMI Parameters Body accesses ABMISpreadsheet Instance getBMI Instance Variables Body accesses Belong to a single method Belong to all methods of an instance local variableglobal variable

19 State-less Vs State-ful Objects ~ car radios without presets ~ car radios with presets Identical Instances Different Instances

20 ABMISpreadsheet public class ABMISpreadsheet { double height, weight; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); } Height Weight BMI

21 public class C { } Read-Only and Editable Properties public T getP() {... } Typed, Named Unit of Exported Object State Name: P Type: T Readonly public void setP(T newValue) {... } Editable newP obtainP Violates Bean Conventions Bean Conventions for humans tools Getter method Setter method

22 Properties Classification public class ABMISpreadsheet { double height; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; } double weight; public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); } … Height Weight BMI Editable Read-only Independent Dependent Read-Only

23 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

24 Calling Getter and Setter Methods public class ABMISpreadsheet { double height = 1.77; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; } double weight = 77; public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return weight/(height*height); }

25 Tracing Method Calls public class ABMISpreadsheet { double height = 1.77; public double getHeight() { System.out.println (“getHeight Called”); return height; } public void setHeight(double newHeight) { System.out.println (“setWeight Called”); height = newHeight; } double weight = 77; public double getWeight() { System.out.println (“getWeight Called”); return weight; } public void setWeight(double newWeight) { System.out.println (“setWeight Called”); weight = newWeight; } public double getBMI() { System.out.println (“getBMI Called”); return weight/(height*height); }

26 Actual Trace

27 Modified ABMISpreadsheet public class ABMISpreadsheet { double height, weight; public double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return calculateBMI(weight, height); } public double calculateBMI(double weight, double height) { return weight / (height*height); | } How will above UI change?

28 Properties + Class Menu

29 Editing in slow motion : Initial value

30 Editing in slow motion: text string edited

31 Editing in slow motion: return triggers setter and getter calls

32 Renaming getter method public class ABMISpreadsheet { double height, weight; public double height() { return height; } public void setHeight(double newHeight) { height = newHeight; } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return calculateBMI(weight, height); } public double calculateBMI(double weight, double height) { return weight / (height*height); | }

33 ObjectEditor does not know it is getter

34 Reducing Access public class ABMISpreadsheet { double height, weight; double getHeight() { return height; } public void setHeight(double newHeight) { height = newHeight; } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return calculateBMI(weight, height); } public double calculateBMI(double weight, double height) { return weight / (height*height); | }

35 Height is not a readable property

36 Changing setter public class ABMISpreadsheet { double height, weight; public double getHeight() { return height; } public int setHeight(double newHeight) { height = newHeight; return height; } public double getWeight() { return weight; } public void setWeight(double newWeight) { weight = newWeight; } public double getBMI() { return calculateBMI(weight, height); } public double calculateBMI(double weight, double height) { return weight / (height*height); | }

37 Height is not a writeable property

38 An alternative class public class AStringHistory implements StringHistory { public static final int MAX_SIZE = 50; String[] contents = new String[MAX_SIZE]; int size = 0; public int size() { return size;} public String elementAt (int index) { return contents[index]; } boolean isFull() { return size == MAX_SIZE; } public void 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

39 ObjectEditor Conventions for Variable-Sized Collection public interface I { public void addElement (T t); public T elementAt (int index); public int size(); } Arbitrary Type (Must be Object Type to be recognized by ObjectEditor) Read methods Write method (name does not matter to OE)

40 Initial State

41 Adding an element Name does not matter to ObjectEditor

42 Adding an element

43 ObjectEditor calls all elementAt () and getter() methods after each method call

44 Adding another element

45

46 Public Class Constants

47

48 Non Public Class Constant public class AStringHistory implements StringHistory { static final int MAX_SIZE = 50; String[] contents = new String[MAX_SIZE]; int size = 0; public int size() { return size;} public String elementAt (int index) { return contents[index]; } boolean isFull() { return size == MAX_SIZE; } public void addElement(String element) { if (isFull()) System.out.println("Adding item to a full history"); else { contents[size] = element; size++; }

49 Non-Public Class Constant Non constants menu

50 Public Instance Constant public class AStringHistory implements StringHistory { public final int MAX_SIZE = 50; String[] contents = new String[MAX_SIZE]; int size = 0; public int size() { return size;} public String elementAt (int index) { return contents[index]; } boolean isFull() { return size == MAX_SIZE; } public void addElement(String element) { if (isFull()) System.out.println("Adding item to a full history"); else { contents[size] = element; size++; }

51 Public Instance Constant Public instance variables also displayed. Considered part of displayable instance state. Breaking of encapsulation is visible in the display!

52 Mixing static and dynamic properties

53

54

55 Another UI

56

57

58 New Class (edit) public class AStringHistory implements StringHistory { public static final int MAX_SIZE = 50; String[] contents = new String[MAX_SIZE]; int size = 0; public int size() { return size;} public String elementAt (int index) { return contents[index]; } boolean isFull() { return size == MAX_SIZE; } public void addElement(String element) { if (isFull()) System.out.println("Adding item to a full history"); else { contents[size] = element; size++; }

59 New Class (edited) public class AStringHistory implements StringHistory { public static final int MAX_SIZE = 50; String[] contents = new String[MAX_SIZE]; int size = 0; public int size() { return size;} public String elementAt (int index) { return contents[index]; } boolean isFull() { return size == MAX_SIZE; } String newString = “”; public String getNewString() {return newString;} public void setNewString(String newVal) { addElement(newVal); newString = newVal; } …. }

60 New Class public class AStringHistory implements StringHistory { String newString = ""; public void setNewString(String newVal ) { newString = newVal; addElement(newString); } public String getNewString() { return newString; } public static final int MAX_SIZE = 50; String[] contents = new String[MAX_SIZE]; int size = 0; public int size() { return size;} public String elementAt (int index) { return contents[index]; } boolean isFull() { return size == MAX_SIZE; } public void addElement(String element) { … }

61 Uninitialized properties public class AStringHistory implements StringHistory { String newString; public void setNewString(String newVal ) { newString = newVal; addElement(newString); } public String getNewString() { return newString; } public static final int MAX_SIZE = 50; String[] contents = new String[MAX_SIZE]; int size = 0; public int size() { return size;} public String elementAt (int index) { return contents[index]; } boolean isFull() { return size == MAX_SIZE; } public void addElement(String element) { … }

62 Uninitialized properties

63 Non-public size() public class AStringHistory implements StringHistory { String newString; public void setNewString(String newVal ) { newString = newVal; addElement(newString); } public String getNewString() { return newString; } public static final int MAX_SIZE = 50; String[] contents = new String[MAX_SIZE]; int size = 0; int size() { return size;} public String elementAt (int index) { return contents[index]; } boolean isFull() { return size == MAX_SIZE; } public void addElement(String element) { … }

64 Non-public size() ObjectEditor discovers only one fixed property

65 “Recursion” in ObjectEditing package main; import bus.uigen.ObjectEditor; public class AnObectEditorDisplayer { public static void main(String[] args) { ObjectEditor.edit(new ObjectEditor()); }

66 ObjectEditor Properties

67 ObjectEditor Properties and Commands

68 Newly instantiated and displayed object

69 Bean Methods Getters and setters shown in separate menu Usually invoked implicitly as side effect of editing

70 “Recursion” in ObjectEditing package main; import bus.uigen.ObjectEditor; public class AnObectEditorDisplayer { public static void main(String[] args) { ObjectEditor.edit(new ObjectEditor()); } Application-independent! ObjectEditor has a main method that does this automatically!

71 Running ObjectEditor java bus.uigen.ObjectEditor = ObjectEditor.edit(new ObjectEditor()) java bus.uigen.ObjectEditor C = ObjectEditor.edit (new C())

72 Running ObjectEditor Main

73

74

75

76

77 Object parameters

78

79

80

81

82

83

84

85

86

87

88

89

90 Constructors with parameters

91 Interface

92 Constructors with parameters

93

94

95

96


Download ppt "ObjectEditor Prasun Dewan Comp 114. ObjectEditor Automatic user-interface generation. You only write computation code Separate object to do I/O Main just."

Similar presentations


Ads by Google