Java Coding 5 – Part 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…

Slides:



Advertisements
Similar presentations
Chapter 5: The Singleton Pattern
Advertisements

Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Example - The.
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.
Java Coding OOP David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Towards Event-driven programming &
Everyday Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction to.
Introduction to UML David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …Unified Modeling Language.
Jan Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
Oct Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
Road Map Introduction to object oriented programming. Classes
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
Java Coding 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Decisions, decisions…!
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Java Syntax Primitive data types Operators Control statements.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Java Coding 3 David Davenport Computer Eng. Dept.,
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
Spring 2010ACS-3913 Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
Review: OOP & Arrays David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …from CS101.
Robo David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction to the engineering.
Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey.
Java Coding 4 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Method madness.
Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
Java Coding 3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Over & over again!
Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
From Algorithms to Architecture David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction.
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.
Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Syntax for Variables & Constants Input,
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
Objects & Classes Weiss ch. 3. So far: –Point (see java.awt.Point) –String –Arrays of various kinds –IPAddress (see java.net.InetAddress) The Java API.
Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Some important Java interfaces +
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
From Algorithms to Architecture David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Objects and Variables Local variables – Confined to single context: allocated on stack – Primitive types such as int or object references – Must be initialized.
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
ITI 1120 Lab #10 Objects and Classes Slides by: Romelia Plesa, Sylvia Boyd, Alan Williams, Diana InkPen, Daniel Amyot, Gilbert Arbez, Mohamad Eid.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Java Coding 6 David Davenport Computer Eng. Dept.,
Data Structures and Algorithms revision
Java Coding – part 2 David Davenport Computer Eng. Dept.,
Software Development Java Classes and Methods
Java Coding Extra David Davenport Computer Eng. Dept.,
Java Coding 3 – part2 David Davenport Computer Eng. Dept.,
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept.,
Java Coding 8 David Davenport Computer Eng. Dept.,
March 29th Odds & Ends CS 239.
Java Coding 4 David Davenport Computer Eng. Dept.,
Java Coding 6-extra David Davenport Computer Eng. Dept.,
Java Coding 6 – part2 David Davenport Computer Eng. Dept.,
Programs and Classes A program is made up from classes
Java Coding 8 David Davenport Computer Eng. Dept.,
Java Coding 4 (part2) David Davenport Computer Eng. Dept.,
Dr. Sampath Jayarathna Cal Poly Pomona
Java Coding 6_part3 David Davenport Computer Eng. Dept.,
Classes and Objects Object Creation
Java Coding 6 David Davenport Computer Eng. Dept.,
Java Coding 6 David Davenport Computer Eng. Dept.,
CMSC 202 Constructors Version 9/10.
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept.,
Presentation transcript:

Java Coding 5 – Part 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…

IMPORTANT… Students… This presentation is designed to be used in class as part of a guided discovery sequence. It is not self- explanatory! Please use it only for revision purposes after having taken the class. Simply flicking through the slides will teach you nothing. You must be actively thinking, doing and questioning to learn! Instructors… You are free to use this presentation in your classes and to make any modifications to it that you wish. All I ask is an saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you, David. David

Object vs. Reference In the “real” world david {Person} CS101 instructor {Person} derya {Person} Derya’s dad {Person} So too in Java! david2 {Person}

Object vs. Reference In the Java world need to revise our mental model! David Derya david {Person} CS101 instructor {Person} derya {Person} Derya’s dad {Person} David’s clone david2 {Person}

Same or Different? (1) Comparing objects TitleB.R. ArtistQueen Date1976 Length3:50 myCd {CD} TitleB.R. ArtistQueen Date1976 Length3:50 yourQCd {CD} TitleBest of ArtistGenesis Date1983 Length2:40 yourCd {CD} if ( myCd == yourCd) System.out.println( “Same”); else System.out.println( “Different”); if ( myCd == yourQCd) System.out.println( “Same”); else System.out.println( “Different”); myQCd {CD}

Same or Different? (2) Define an “equals” method TitleB.R. ArtistQueen Date1976 Length3:50 myCd {CD} TitleB.R. ArtistQueen Date1976 Length3:50 yourQCd {CD} TitleBest of ArtistGenesis Date1983 Length2:40 yourCd {CD} if ( myCd.equals( yourCd) ) System.out.println( “Same”); else System.out.println( “Different”); if ( myCd.equals( yourQCd) ) System.out.println( “Same”); else System.out.println( “Different”); myQCd {CD}

Copying in primitive vs. Object types… int i, j; i = 5; j = i; i++; Sys… ( i, j); Person me, x; me = new Person( …); x = me; me.setComments( “nice!”); Sys… ( me.getComments() + x.getComments(), ); Different Same!

Copy vs. Clone TitleB.R. ArtistQueen Date1976 Length3:50 TitleB.R. ArtistQueen Date1976 Length3:50 myCd {CD} yourQCd {CD} favouriteCd {CD} favouriteCd = myCd;yourQCd = myCd.clone();

Parameter Passing (1) Primitive types… public int xyz( int i) { i++; return i; } int a, b; a = 5; b = xyz( a); Sys… ( a, b); 5 a b main 5 i xyz 6 6

Parameter Passing (2) Object types… public Person xyz( Person x) { x.setComments(“Nice); return x; } Person a, b; a = new Person( “David” …); b = xyz( a); Sys… ( a.getComments() + b.getComments() ); a b main x xyz David “” Nice

Parameter Passing (3) Object types… public Person xyz( Person x) { x = new Person( “Derya” …); x.setComments(“Nice); return x; } Person a, b; a = new Person( “David” …); b = xyz( a); Sys… ( a.getComments() + b.getComments() ); a b main x xyz David “” Derya “” Nice

All Objects… automatically have boolean equals( Object) Object clone() String toString() BUT they may not do what you would like/expect, so implement yourself! Code using these methods will compile & run even if your class does not define them!

Lost objects & null Java collects its garbage! TitleB.R. ArtistQueen Date1976 Length3:50 TitleBest of ArtistGenesis Date1983 Length2:40 myCd {CD} yourCd {CD} myCd = yourCd; aCd {CD} aCd = null;

STATIC VS. INSTANCE

Static vs. instance Variables “count” as an instance variable -- each instance (object) has count variable name age salary comments Person instance 0 David “Quiet” 0 Derya “Nice” 0 Gunes “Sunny” 0 Ayse “Happy” count abcd 2 3 c.count++; a.count = 3;

Static vs. instance Variables “count” as a static variable -- only one count variable, associated with class name age salary comments Person static instance David “Quiet” Derya “Nice” Gunes “Sunny” Ayse “Happy” count abcd initialise count to zero, then increment it in constructor also known as “class variables”

Misc: Can combine, so static “nextID” gives next value to be assigned to instance variable “personID”. Constants often defined as static hence saving space public static final int PI = 3.142; public static final String COMPANY = “Bilkent University”;

Static vs. instance Methods Classes can have both static & instance methods. Static methods useful when accessing static variables public static int getCount() object state is not needed public static int getAge( day, month, year) Static methods cannot access instance variables or methods Instance methods can access static & instance, variables & methods

Singletons (design pattern) Problem: Ensure only a single instance of a class is created. (for database or network connections, etc.) Solution: Combine static variable, private constructor & static method! public class SingletonClass { private static SingletonClass ourInstance = new SingletonClass(); private SingletonClass() { } public static SingletonClass getInstance() { return singletonObj; }