A Singleton Puzzle: What is Printed? 1 public class Elvis { public static final Elvis INSTANCE = new Elvis(); private final int beltSize; private static.

Slides:



Advertisements
Similar presentations
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Advertisements

L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Singleton vs utility class  at first glance, the singleton pattern does not seem to offer any advantages to using a utility class  i.e., a utility class.
Written by: Dr. JJ Shepherd
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.
UML Basics & Access Modifier
Object Oriented Software Development
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
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.
Composition (Part 2) 1. Class Invariants  class invariant  some property of the state of the object that is established by a constructor and maintained.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Goals for Today  implement a Deck of Cards  composition  Iterator interface  Iterable interface 1.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
A Singleton Puzzle: What is Printed? 1 public class Elvis { public static final Elvis INSTANCE = new Elvis(); private final int beltSize; private static.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Puzzle 3 1  Write the class Enigma, which extends Object, so that the following program prints false: public class Conundrum { public static void main(String[]
Utilities (Part 2) Implementing static features 1.
Inheritance (Part 4) Abstract Classes 1.  sometimes you will find that you want the API for a base class to have a method that the base class cannot.
1 Class Diagrams: Advanced Concepts. 2 Overview Class diagrams are the most commonly used diagrams in UML. Class diagrams are the most commonly used diagrams.
P Improvements - Constructor p Parameter passing Constructors Problem Solving With C++
Singleton and Basic UML CS340100, NTHU Yoshi. What is UML Unified Modeling Language A standardized general-purpose modeling language in the field of software.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Goals for Today 1  Multiton  maps  static factory methods.
Mixing Static and Non-static
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Puzzle 1  what does the following program print? public class Puzzle01 { public static void main(String[] args) { System.out.print("C" + "S" + "E"); System.out.println('1'
Week101 APCS-AB: Java Miscellaneous Topics: Snippets we missed in Chapters 1-6 of book November 11, 2005.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Composition  recall that an object of type X that is composed of an object of type Y means  X has-a Y object and  X owns the Y object  in other words.
Composition 1.  recall that an object of type X that is composed of an object of type Y means  X has-a Y object and  X owns the Y object  in other.
Mixing Static and Non-static Singleton 1. Singleton Pattern 2  “There can be only one.”  Connor MacLeod, Highlander.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
Puzzle 2 1  what does the following program print? public class Puzzle02 { public static void main(String[] args) { final long MICROS_PER_DAY = 24 * 60.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
UML2-1 UML Exercise A "draw" utility program lets users draw several geometric objects on a diagram. A geometric object may be a Circle, Rectangle, Square.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Chapter 10 Thinking in Objects
Classes and Objects Introduced
Lecture 3: Introduction to Object and Classes
Aggregation and Composition
Chapter 10 Thinking in Objects
Chapter 10 Thinking in Objects
Intro To Classes Review
The hashCode method.
CS Week 13 Jim Williams, PhD.
CS 302 Week 10 Jim Williams.
Still Aggregation and Composition
Class Structure 16-Nov-18.
Chapter 9 Thinking in Objects
Chapter 9 Thinking in Objects
Basics of OOP A class is the blueprint of an object.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Object Oriented Programming Review
Chapter 9 Objects and Classes
Dr. R Z Khan Handout-3 Classes
OO Programming Concepts
Composition.
Presentation transcript:

A Singleton Puzzle: What is Printed? 1 public class Elvis { public static final Elvis INSTANCE = new Elvis(); private final int beltSize; private static final int CURRENT_YEAR = Calendar.getInstance().get(Calendar.YEAR); private Elvis() { this.beltSize = CURRENT_YEAR – 1930; } public int getBeltSize() { return this.beltSize; } public static void main(String[] args) { System.out.println("Elvis has a belt size of " + INSTANCE.getBeltSize()); } from Java Puzzlers by Joshua Bloch and Neal Gafter

A Singleton Puzzle: Solution  Elvis has a belt size of is printed  to solve the puzzle you need to know how Java initializes classes (JLS 12.4)  the call to main() triggers initialization of the Elvis class (because main() belongs to the class Elvis)  the static attributes INSTANCE and CURRENT_YEAR are first given default values ( null and 0, respectively)  then the attributes are initialized in order of appearance 2

1. public static final Elvis INSTANCE = new Elvis(); 2. this.beltSize = CURRENT_YEAR – 1930; 3. private static final int CURRENT_YEAR = Calendar.getInstance().get(Calendar.YEAR); the problem occurs because initializing INSTANCE requires a valid CURRENT_YEAR solution: move CURRENT_YEAR before INSTANCE 3 CURRENT_YEAR == 0 at this point

Aggregation and Composition [notes Chapter 4] 4

Aggregation and Composition  the terms aggregation and composition are used to describe a relationship between objects  both terms describe the has-a relationship  the university has-a collection of departments  each department has-a collection of professors 5

Aggregation and Composition  composition implies ownership  if the university disappears then all of its departments disappear  a university is a composition of departments  aggregation does not imply ownership  if a department disappears then the professors do not disappear  a department is an aggregation of professors 6

Aggregation  suppose a Person has a name and a date of birth public class Person { private String name; private Date birthDate; public Person(String name, Date birthDate) { this.name = name; this.birthDate = birthDate; } public Date getBirthDate() { return birthDate; } 7

 the Person example uses aggregation  notice that the constructor does not make a copy of the name and birth date objects passed to it  the name and birth date objects are shared with the client  both the client and the Person instance are holding references to the same name and birth date 8 // client code somewhere String s = "Billy Bob"; Date d = new Date(91, 2, 26); // March 26, 1991 Person p = new Person(s, d);

9 64 client s250 d350 p String object Date object Person object name250 birthDate350

 what happens when the client modifies the Date instance?  prints Fri Nov 03 00:00:00 EST // client code somewhere String s = "Billy Bob"; Date d = new Date(90, 2, 26); // March 26, 1990 Person p = new Person(s, d); d.setYear(95); // November 3, 1995 d.setMonth(10); d.setDate(3); System.out.println( p.getBirthDate() );

 because the Date instance is shared by the client and the Person instance:  the client can modify the date using d and the Person instance p sees a modified birthDate  the Person instance p can modify the date using birthDate and the client sees a modified date d 11

 note that even though the String instance is shared by the client and the Person instance p, neither the client nor p can modify the String  immutable objects make great building blocks for other objects  they can be shared freely without worrying about their state 12

UML Class Diagram for Aggregation 13 PersonStringDate 11 number of Date objects each Person has number of String objects each Person has open diamonds indicate aggregation

Another Aggregation Example  3D videogames use models that are a three- dimensional representations of geometric data  the models may be represented by:  three-dimensional points (particle systems)  simple polygons (triangles, quadrilaterals)  smooth, continuous surfaces (splines, parametric surfaces)  an algorithm (procedural models)  rendering the objects to the screen usually results in drawing triangles  graphics cards have specialized hardware that does this very fast 14

15

16

Aggregation Example  a Triangle has 3 three-dimensional Point s 17 TrianglePoint 3 Triangle + Triangle(Point, Point, Point) + getA() : Point + getB() : Point + getC() : Point + setA(Point) : void + setB(Point) : void + setC(Point) : void Point + Point(double, double, double) + getX() : double + getY() : double + getZ() : double + setX(double) : void + setY(double) : void + setZ(double) : void

Triangle // attributes and constructor public class Triangle { private Point pA; private Point pB; private Point pC; public Triangle(Point c, Point b, Point c) { this.pA = a; this.pB = b; this.pC = c; } 18

Triangle // accessors public Point getA() { return this.pA; } public Point getB() { return this.pB; } public Point getC() { return this.pC; } 19

Triangle // mutators public void setA(Point p) { this.pA = p; } public void setB(Point p) { this.pB = p; } public void setC(Point p) { this.pC = p; } 20

Triangle Aggregation  implementing Triangle is very easy  attributes (3 Point references)  are references to existing objects provided by the client  accessors  give clients a reference to the aggregated Point s  mutators  set attributes to existing Point s provided by the client  we say that the Triangle attributes are aliases 21

// client code Point a = new Point(-1.0, -1.0, -3.0); Point b = new Point(0.0, 1.0, -3.0); Point c = new Point(2.0, 0.0, -3.0); Triangle tri = new Triangle(a, b, c); 22

23 64 client a250 b350 c450 tri Point object x y z Point object x0.0 y1.0 z Point object x2.0 y0.0 z Triangle object pA250 pB350 pC450

// client code Point a = new Point(-1.0, -1.0, -3.0); Point b = new Point(0.0, 1.0, -3.0); Point c = new Point(2.0, 0.0, -3.0); Triangle tri = new Triangle(a, b, c); Point d = tri.getA(); boolean sameObj = a == d; 24 client asks the triangle for one of the triangle points and checks if the point is the same object that was used to create the triangle

25 64 client a250 b350 c450 tri550 d250 sameObjtrue 250Point object x y z Point object x0.0 y1.0 z Point object x2.0 y0.0 z Triangle object pA250 pB350 pC450

// client code Point a = new Point(-1.0, -1.0, -3.0); Point b = new Point(0.0, 1.0, -3.0); Point c = new Point(2.0, 0.0, -3.0); Triangle tri = new Triangle(a, b, c); Point d = tri.getA(); boolean sameObj = a == d; tri.setC(d); 26 client asks the triangle to set one point of the triangle to d

27 64 client a250 b350 c450 tri550 d250 sameObjtrue 250Point object x y z Point object x0.0 y1.0 z Point object x2.0 y0.0 z Triangle object pA250 pB350 pC250

// client code Point a = new Point(-1.0, -1.0, -3.0); Point b = new Point(0.0, 1.0, -3.0); Point c = new Point(2.0, 0.0, -3.0); Triangle tri = new Triangle(a, b, c); Point d = tri.getA(); boolean sameObj = a == d; tri.setC(d); b.setX(0.5); b.setY(6.0); b.setZ(2.0); 28 client changes the coordinates of one of the points (without asking the triangle for the point first)

29 64 client a250 b350 c450 tri550 d250 sameObjtrue 250Point object x y z Point object x0.5 y6.0 z Point object x2.0 y0.0 z Triangle object pA250 pB350 pC250

Triangle Aggregation  if a client gets a reference to one of the triangle's points, then the client can change the position of the point without asking the triangle  run demo program in class here 30

31 pointB = new Point(0.0, 1.0, -3.0); tri = new Triangle(new Point(-1.0, -1.0, -3.0), pointB, new Point(2.0, 0.0, -3.0)); // Draw triangle gl.glBegin(GL2.GL_TRIANGLES); gl.glColor3f(0.0f, 1.0f, 1.0f); // set the color gl.glVertex3d(tri.getA().getX(), tri.getA().getY(), tri.getA().getZ()); gl.glVertex3d(tri.getB().getX(), tri.getB().getY(), tri.getB().getZ()); gl.glVertex3d(tri.getC().getX(), tri.getC().getY(), tri.getC().getZ()); gl.glEnd(); // the client moves a point without help from the triangle delta += 0.05f; pointB.setY(1.0 + Math.sin(delta)); client and triangle share a reference to pointB draw the triangle by asking tri for the coordinates of each of its points client uses pointB to change the point coordinates