C OMP 110/401 C OMPOSITE A NNOTATIONS Instructor: Prasun Dewan.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

C OMP 110/401 P ACKAGES Instructor: Prasun Dewan.
Graphics Graphics Java Vectors Java Enumeration Graphical Spreadsheets
The Point Class public class Point { public double x; public double y; public Point(double x0, double y0) { x = x0; y = y0; } public double distance(Point.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Java Programming Abstract classes and Interfaces.
Inheritance // A simple class hierarchy. // A class for two-dimensional objects. class TwoDShape { double width; double height; void showDim() { System.out.println("Width.
Pointers Prasun Dewan Comp 114.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
C OMP 110 S TATE Instructor: Jason Carter. 2 O UTLINE Instance Variables Procedures Properties Print Statements Println vs. Print Overloading.
C OMP 401 M EMORY R EPRESENTATION OF P RIMITIVE V ALUES AND O BJECTS Instructor: Prasun Dewan.
C OMP 401 C LASS S TATE Instructor: Prasun Dewan.
C OMP 110 R EPRESENTATION Instructor: Jason Carter.
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 110 L OOPS Instructor: Jason Carter. 2 L OOPS More loops Off-by-one errors Infinite loops Nested Loops Animations Concurrency Synchronized methods.
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.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
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.
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.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
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.
Programmer-Defined Types Object Types as Programmer-defined Types Two-way Dependencies Representation Errors Primitive Vs Object Properties Constructors.
ObjectEditor Prasun Dewan Comp 114. ObjectEditor Automatic user-interface generation. You only write computation code Separate object to do I/O Main just.
C OMP 110 M ODEL -V IEW -C ONTROLLER MVC Instructor: Jason Carter.
State Instance Variables Procedures Properties Print Statements Println Vs Print Overloading J++
UML Basics & Access Modifier
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
C OMP 401 A NIMATION L OOPS Instructor: Prasun Dewan.
C OMP 401 P ATTERNS, I NTERFACES AND O BJECT E DITOR Instructor: Prasun Dewan.
Week 3 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
C OMP 401: C ONSTRUCTORS AND P OINTERS Instructor: Prasun Dewan (FB 150,
C OMP 110 T YPES Instructor: Prasun Dewan 2 P REREQUISITES Interfaces.
C OMP 110/401 D OCUMENTATION : C OMMENTS Instructor: Prasun Dewan.
Cross cutting Cross-cutting of components and aspects ordinary program structure-shy functionality structure synchronization better program Components.
C OMP 110/401 E NCAPSULATION AND L EAST P RIVILEGE Instructor: Prasun Dewan.
I NCREASING THE A UTOMATION OF A T OOLKIT WITHOUT R EDUCING ITS A BSTRACTION AND U SER - I NTERFACE F LEXIBILITY Prasun Dewan University of North Carolina.
4.3.1 Non-void Methods Parameters are largely one-way communication.  Shared instances variables is one way to accomplish this. calling codemethod parameter.
C OMP 401 A NIMATION : MVC Instructor: Prasun Dewan.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
C OMP 401 U SER -I NTERFACE VS. M AIN T HREADS Instructor: Prasun Dewan.
C OMP 401 R EFLECTION AND A CTION O BJECTS Instructor: Prasun Dewan.
(c) University of Washington02-1 CSC 143 Java Object and Class Relationships: Interfaces Reading: Ch. 9 (on Java interfaces)
C OMP 401 G RAPH VS. D AG VS. T REE O BJECT S TRUCTURES Instructor: Prasun Dewan.
© 2006 Pearson Addison-Wesley. All rights reserved Non-void Methods Parameters are largely one-way communication.  Shared instances variables is.
XAspects slides Cross-cutting of concerns ordinary program structure-shy functionality structure synchronization better program Chapter 1 Chapter 2 Chapter.
C# - Inheritance Ashima Wadhwa. Inheritance One of the most important concepts in object- oriented programming is inheritance. Inheritance allows us to.
C OMP 401 P RACTICAL A PPLICATIONS OF MVC AND O BSERVER : P ROPERTY N OTIFICATION Instructor: Prasun Dewan.
C OMP 401 E XCEPTIONS -R EMAINDER Instructor: Prasun Dewan.
This computer science resource was developed through a collaboration between IBM Corporation and CSTA. 1 Lesson 2: Pong Class Design.
C OMP 110/401 D OCUMENTATION : A NNOTATIONS Instructor: Prasun Dewan.
C OMP 401 C OPY : S HALLOW AND D EEP Instructor: Prasun Dewan.
Recitation 5 September 23, As you come in...  Please sit next to someone you will collaborate with for today’s recitation.  Choose someone with.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
C OMP 401 P ACKAGES Instructor: Prasun Dewan 2 P REREQUISITES Objects.
T YPES OF M ODELS Prasun Dewan Department of Computer Science University of North Carolina at Chapel Hill Code available at:
Inheritance ITI1121 Nour El Kadri.
Programming in C# Properties
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Composite Objects with Object Editor With slides from Andrew Ghobrial
Comp 110/401 Documentation: Comments
Recitation 4 September 16, 2011.
Review Session Biggest discrepancy questions
Recitation 3 September 9, 2011 Wade Gobel.
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

C OMP 110/401 C OMPOSITE A NNOTATIONS Instructor: Prasun Dewan

2 P REREQUISITE Composite Objects Shapes

3 I NTERPRETED H OW ? public interface RectangleWithPoint { public int getX(); public int getY(); public void getWidth(); public void getHeight(); public Point getPoint(); } Object interpreted as either an atomic rectangle or an atomic point depending on whether Point or Rectangle in the name gets precedence Bad programming as code for defining Rectangle cannot be used in situations where rectangle without point is needed

4 R EUSABLE D ESIGN public interface RectangleWithPoint { public Rectangle getRectangle(); public Point getPoint(); } public interface Rectangle { public int getX(); public int getY(); public void getWidth(); public void getHeight(); }

5 O BJECT E DITOR G RAPHICS R ULES public interface ShuttleLocation { public FancyCartesianPlane getCartesianPlane(); public ImageLabel getShuttleLabel(); public int getShuttleX(); public void setShuttleX(int newVal); public int getShuttleY(); public void setShuttleY(int newVal); } public interface NotAPoint { public FancyCartesianPlane getCartesianPlane(); public ImageLabel getShuttleLabel(); public int getX(); public void setX(int newVal); public int getY(); public void setY(int newVal); public Point getLocation(); } Type interpreted as a Point as its name contains “Point” and has X and Y Properties

6 import //same as AShuttleLocation except interface name public class AShuttleLocationImplementingABadlyNamedInterface implements NotAPoint { IS_ATOMIC_SHAPE C LASS A NNOTATION Annotation is like a comment except it is typed and available at runtime IsAtomicShape(false) before class name says do not interpret the class as an atomic shape (it can be a composition)

7 S HUTTLE L OCATION A LTERNATIVES public interface ShuttleLocation { public FancyCartesianPlane getCartesianPlane(); public ImageLabel getShuttleLabel(); public int getShuttleX(); public void setShuttleX(int newVal); public int getShuttleY(); public void setShuttleY(int newVal); } public interface ShuttleLocationWithPoint { public FancyCartesianPlane getCartesianPlane(); public ImageLabel getShuttleLabel(); public Point getShuttleLocation(); public void setShuttleLocation(Point newVal); }

8 public class AShuttleLocationWithPoint implements ShuttleLocationWithPoint { public Point getShuttleLocation() { return shuttleLocation; } … } P OINT P ROPERTY Does not have X and Y properties, so not a point But location displayed as a point shape

9 IS_ATOMIC_SHAPE A NNOTATION OF C LASS G ETTER IsAtomicShape(false) before getter of a property means property not displayed as an atomic shape import util.annotations.IsAtomicShape; public class AShuttleLocationWithPoint implements ShuttleLocationWithPoint public Point getShuttleLocation() { return shuttleLocation; } … } ShuttleLocation is not a textual property displayed in main panel What if we do not want it in main panel either?

10 import util.annotations.Visible; public class AShuttleLocationWithPoint implements ShuttleLocationWithPoint public Point getShuttleLocation() { return shuttleLocation; } … } V ISIBLE A NNOTATION OF C LASS G ETTER Visible(false) before getter of a property means property not displayed at all

11 A UTOMATIC L ABEL P ATTERN public class AShuttle implements SpecificImageLabel { … }

12 N OT F OLLOWING A UTOMATING L ABEL P ATTERN public class AShuttle implements Shuttle { … }

13 E XPLICIT P ATTERN S PECIFICATION import util.annotations.StructurePattern; import public class AShuttle implements Shuttle{ … } Structure( ) before class asserts that the class is following the pattern. ObjectEditor ignores class name and gives warnings if methods do not follow the pattern

14 E XPLICIT P ATTERN S PECIFICATION import util.annotations.StructurePattern; import public class AShuttle implements Shuttle{ … }

15 E XPLICIT P ATTERN S PECIFICATION public class AShuttleLocation implements ShuttleLocation { … }

16 E XPLICIT P ATTERN S PECIFICATION import util.annotations.StructurePattern; import public class AShuttleLocation implements ShuttleLocation { … }

17 E XPLICIT AND I MPLICIT L ABEL P ATTERN import util.annotations.StructurePattern; import public class AnotherShuttle implements SpecificImageLabel { static final String IMAGE = "shuttle2.jpg“; static final int WIDTH = 80; static final int HEIGHT = 25; Point location; public AShuttle (int initX, int initY) { location = new ACartesianPoint(initX, initY); } public AShuttle () { location = new ACartesianPoint(50, 50); } public Point getLocation() {return location;} public void setLocation(Point newVal) {location = newVal;} public int getWidth() { return WIDTH;} public int getHeight() {return HEIGHT;} public String getImageFileName() {return IMAGE;} }

18 E XPLICIT AND A TTEMPTED I MPLICIT L ABEL P ATTERN import util.annotations.StructurePattern; import public class AnotherShuttle implements SpecificImageLabel { static final String IMAGE = "shuttle2.jpg“; static final int WIDTH = 80; static final int HEIGHT = 25; Point location; public AShuttle (int initX, int initY) { location = new ACartesianPoint(initX, initY); } public AShuttle () { location = new ACartesianPoint(50, 50); } public Point getLocation() {return location;} public void setLocation(Point newVal) {location = newVal;} public int getWidth() { return WIDTH;} public int getHeight() {return HEIGHT;} //public String getImageFileName() {return IMAGE;} }

19 BMI C ALCULATOR P ATTERN ? public class ABMICalculator { public double calculateBMI(double height, double weight) { return weight/(height*height); }

20 BMI C ALCULATOR P ATTERN public class AnAnnotatedBMICalculator { public double calculateBMI(double height, double weight) { return weight/(height*height); }

21 BMI C ALCULATOR P ATTERN public class AnAnnotatedBMICalculator { public double calculateBMI(double height, double weight) { return weight/(height*height); }

22 B EAN P ATTERN public class ABMISpreadsheetNotFollowingBeanConventions { double height = 1.77; double weight = 75; public double getWeight() { return weight; } public void set(double newWeight, double newHeight) { weight = newWeight; height = newHeight; } public double getHeight() { return height; } public void setHeight(int newHeight) { height = newHeight; } public double BMI() { return weight/(height*height); }

23 (E DITABLE ) P ROPERTY N AME "Height", "Weight", "Weight"}) public class ABMISpreadsheetNotFollowingBeanConventions { double height = 1.77; double weight = 75; public double getWeight() { return weight; } public void set(double newWeight, double newHeight) { weight = newWeight; height = newHeight; } public double getHeight() { return height; } public void setHeight(int newHeight) { height = newHeight; } public double BMI() { return weight/(height*height); }

24 O RDER OF "Weight", "Height", "Weight"}) public class ABMISpreadsheetNotFollowingBeanConventions { double height = 1.77; double weight = 75; public double getWeight() { return weight; } public void set(double newWeight, double newHeight) { weight = newWeight; height = newHeight; } public double getHeight() { return height; } public void setHeight(int newHeight) { height = newHeight; } public double BMI() { return weight/(height*height); }

25 B EAN P ATTERN public class ABMISpreadsheetNotFollowingBeanConventions { double height = 1.77; double weight = 75; public double getWeight() { return weight; } public void set(double newWeight, double newHeight) { weight = newWeight; height = newHeight; } public double getHeight() { return height; } public void setHeight(int newHeight) { height = newHeight; } public double BMI() { return weight/(height*height); } Warning if (editable) properties not declared? Overhead, chances of mistake low, C# has built in support for properties Why warning if no structure annotation?

26 W HY W ARNINGS Accidental patterns Efficiency

27 L ARGE S EARCH S PACE Accidental patterns Efficiency public class StructurePatternNames { public static final String NO_PATTERN = "No Pattern"; public static final String BEAN_PATTERN = "Bean Pattern"; public static final String POINT_PATTERN = "Point Pattern"; public static final String LINE_PATTERN = "Line Pattern"; public static final String OVAL_PATTERN = "Oval Pattern"; public static final String RECTANGLE_PATTERN = "Rectangle Pattern"; public static final String ARC_PATTERN = "Arc Pattern"; public static final String LABEL_PATTERN = "Label Pattern"; public static final String TEXT_PATTERN = "Text Pattern"; public static final String STRING_PATTERN = "String Pattern"; public static final String CURVE_PATTERN = "Curve Pattern"; public static final String VECTOR_PATTERN = "Vector Pattern"; public static final String LIST_PATTERN = "List Pattern"; public static final String STACK_PATTERN = "Stack Pattern"; public static final String HASHTABLE_PATTERN = "Hashtable Pattern"; public static final String MAP_PATTERN = "Hashmap Pattern"; public static final String ENUM_PATTERN = "Enum Pattern"; … }