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

Slides:



Advertisements
Similar presentations
Basic -2 Classes and Objects. Classes and Objects A class is a complex data TYPE An object is an instance of a class. Example: Class: Person Objects:
Advertisements

Graphics Graphics Java Vectors Java Enumeration Graphical Spreadsheets
***** SWTJC STEM ***** Chapter 4-1 cg 42 Object Oriented Program Terms Up until now we have focused on application programs written in procedural oriented.
C OMP 110 S TATE Instructor: Jason Carter. 2 O UTLINE Instance Variables Procedures Properties Print Statements Println vs. Print Overloading.
C OMP 110/401 C OLLECTION K INDS Instructor: Prasun Dewan.
C OMP 110 A RRAYS Instructor: Jason Carter. 2 O UTLINE for loops Arrays.
C OMP 401 D YNAMIC D ISPATCH AND A BSTRACT M ETHODS Instructor: Prasun Dewan.
C OMP 110 S TYLE Instructor: Jason Carter. 2 I NTERFACES AND M ORE S TYLE Define contracts between our users and implementers Optional – they may not.
C OMP 401 O BJECTS Instructor: Prasun Dewan 2 C OMPUTER VS. P ROGRAM M ODEL Processor Compiler Program (source code)
C OMP 110 L OAN C ASE S TUDY Instructor: Jason Carter.
Road Map Introduction to object oriented programming. Classes
By - Qiong Han. DON’T FALL BEHIND IN READING Submission Instruction Everyone needs to submit the printed HARD- COPY of the codes and the grading sheets.
Comp 114 Foundations of Programming Instructor: Prasun Dewan.
Midterm Exam 75 points 1 min per point Allocate time proportionate to points Closed book Chapters 1-5 (except char) PDF/PS with index and corrections coming.
Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules.
Program Style Identifier Names Comments Named Constants Avoiding Code Repetition Giving Least Privilege Efficiency Interfaces.
Comp 14 (3) By Stephan Sherman Office hours, W, 3:00-3:50pm.
Object-based Programming Intuitive explanation Using objects to read input Creating objects Style rules.
Arrays –Collections (Set, Database, History) Inheritance –inheriting ancestor’s traits –inheriting benefactor’s assets –inheriting instance members( methods/variables)
Programmer-Defined Types Object Types as Programmer-defined Types Two-way Dependencies Representation Errors Primitive Vs Object Properties Constructors.
Object-based Programming Intuitive explanation Two concrete examples Calculators addition BMI Programming Environment.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Writing Classes (Chapter 4)
1 v1.6 08/02/2006 Overview of Eclipse Lectures 1.Overview 2.Installing and Running 3.Building and Running Java Classes 4.Refactoring 5.Debugging 6.Testing.
C OMP 401 P ATTERNS, I NTERFACES AND O BJECT E DITOR Instructor: Prasun Dewan.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
C OMP 110/401 C OMPOSITE A NNOTATIONS Instructor: Prasun Dewan.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Chap. 1 Classes, Types, and Objects. How Classes Are Declared [ ] class [extends ] [implements,, … ] { // class methods and instance variable definitions.
C OMP 401: C ONSTRUCTORS AND P OINTERS Instructor: Prasun Dewan (FB 150,
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
C OMP 110/401 E NCAPSULATION AND L EAST P RIVILEGE Instructor: Prasun Dewan.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
C OMP 401 A DVANCED G ENERICS Instructor: Prasun Dewan.
1 CSC/ECE 517 Fall 2010 Lec. 3 Overview of Eclipse Lectures Lecture 2 “Lecture 0” Lecture 3 1.Overview 2.Installing and Running 3.Building and Running.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
C OMP 401 P RACTICAL A PPLICATIONS OF MVC AND O BSERVER : P ROPERTY N OTIFICATION Instructor: Prasun Dewan.
Topics Instance variables, set and get methods Encapsulation
C OMP 110/401 D OCUMENTATION : A NNOTATIONS Instructor: Prasun Dewan.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
T YPES OF M ODELS Prasun Dewan Department of Computer Science University of North Carolina at Chapel Hill Code available at:
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
C OMP 401 D YNAMIC D ISPATCH AND V IRTUAL AND A BSTRACT M ETHODS Instructor: Prasun Dewan.
Module Road Map Refactoring Why Refactoring? Examples
Instructor: Prasun Dewan
Reference: COS240 Syllabus
Instructor: Prasun Dewan
Comp 401 Dynamic Dispatch and Virtual and Abstract Methods
CS 200 Creating Classes Jim Williams, PhD.
Beans and object editor
Comp 110/401 Documentation: Comments
Recitation 4 September 16, 2011.
Simple Classes in Java CSCI 392 Classes – Part 1.
Recitation 10 November 3, 2011.
Recitation 3 September 9, 2011 Wade Gobel.
CS 200 Creating Classes Jim Williams, PhD.
CS 200 More Classes Jim Williams, PhD.
CS 200 Creating Classes Jim Williams, PhD.
CSG2H3 Object Oriented Programming
Presentation transcript:

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; public class ABMICalculator implements BMICalculator { public double calculateBMI (double weight, double height) { return weight/(height*height); }

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()); }

ABMICalculator UI

Adding a library in JBuilder

Location of Libraries 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.

Location of Libraries

What-if BMI Calculations Unchanging value retyped

BMI Spreadsheet State: Data Remembered by an Object between computations

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

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

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

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

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

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 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); }

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); }

Actual Trace

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?

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 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); | }

ObjectEditor does not know it is getter

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); | }

Height is not a readable property

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); | }

Height is not a writeable property

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

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)

Initial State

Adding an element Name does not matter to ObjectEditor

Adding an element

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

Adding another element

Public Class Constants

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++; }

Non-Public Class Constant Non constants menu

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++; }

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

Mixing static and dynamic properties

Another UI

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++; }

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; } …. }

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) { … }

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) { … }

Uninitialized properties

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) { … }

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

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

ObjectEditor Properties

ObjectEditor 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 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!

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

Running ObjectEditor Main

Object parameters

Constructors with parameters

Interface

Constructors with parameters