School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 1 CMT1000: Introduction to Programming Ed Currie Lecture 11: Objects.

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

Written by: Dr. JJ Shepherd
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Enhancing classes Visibility modifiers and encapsulation revisited
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 1 TEST!!
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 1 CMT1000: Introduction to Programming Ed Currie Lecture 9: Arrays.
Object Oriented Software Development
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Java Classes Using Java Classes Introduction to UML.
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.
Java Implementation: Part 3 Software Construction Lecture 8.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
GP3, Martin Lillholm 1 Introductory Programming (GP) Spring 2006 Lecture 3 We start at 13:00 Slides are available from the course home page Feel free to.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Introducing Objects and stuff GABY and MATT Definition: Object: “a class that is the root of the hierarchy tree for all classes in JAVA.” –An object.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Session 7 Methods Strings Constructors this Inheritance.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
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.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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 ◦
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
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.
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
OOP Basics Classes & Methods (c) IDMS/SQL News
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
Lecture 12 Inheritance.
Examples of Classes & Objects
Chapter 3: Using Methods, Classes, and Objects
Road Map Introduction to object oriented programming. Classes
CS 302 Week 11 Jim Williams, PhD.
Object Based Programming
Classes & Objects: Examples
Encapsulation and Constructors
Object Oriented Programming in java
Web Design & Development Lecture 4
String Class.
Presentation transcript:

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 1 CMT1000: Introduction to Programming Ed Currie Lecture 11: Objects

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 2 Objects declaration creation manipulation

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 3 What is an object? So far, we have considered a class to be a collection of methods, which work together by calling each other to solve a programming problem Each method can contain variable declarations, which are local to (only exist within) that method… … and that’s it

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 4 But there’s more! Much more! Classes can be used to define blueprints for objects What are objects? An object (usually) encapsulates –Attributes (Data) –Behaviours (methods) The attributes do not reside in any of the methods, but are shared by (accessible to) all of them.

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 5 What attributes might be associated with these objects? A coordinate point A glass which can hold up to 1 litre A square to be drawn on a graphics screen A car in a simulation of a traffic situation A person’s personal details A timepiece (clock, watch)

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 6 What about methods? They implement the behaviours of the object (i.e. what it can do) For example changing the attributes, reporting the values of attributes, outputting results of calculations What might we want to ask our objects to do? –Coordinate point –Glass –Square –Car –Person –Timepiece

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 7 ‘Time’ for an example A class to define timepiece objects Data (attributes) needed? –A time! (hours, minutes and seconds) Behaviours (methods) needed? –Create a timepiece with a time associated with it –Change the time associated with the timepiece –Output the current time stored, in hrs mins and secs –Ditto but in seconds –TickTock (i.e. advance the time stored by one second) –and so on

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 8 The class public class Timepiece { private int hrs, mins, secs; public Timepiece(int h, int m, int s) {hrs = h; mins = m; secs = s;} public int getHrs() { return hrs;} public int getMins() { return mins;} public int getSecs() { return secs;} }

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 9 Details We can create lots of instances of this class; these are called objects –just as a variable is an instance of a primitive type Each object represents one timepiece Each object has its own copy of the attributes Attributes are called instance variables –numerical ones are automatically initialised to 0 –doesn’t happen with local variables –they have class scope

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 10 The details Instance variables - private (usually); can only be accessed by the methods of this class i.e. not by methods of other classes So how can we create Timepiece objects using this class? Constructor function Timepiece is called... –has same name as the class –parameters usually used to give initial values to instance variables –Note; no return value (not even void) … but how is it called?

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 11 Answer The constructor function is called automatically when we create an object of the class using new –allocates memory, then calls constructor The same as when we created array objects Timepiece myWatch = new Timepiece(h,m,s); Note how constructor arguments are provided

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 12 Using Timepiece public class Time { public static void main(String args[]) { int h=4,m=30,s=45; Timepiece myWatch = new Timepiece(h,m,s); Timepiece myClock = new Timepiece(h,75,20); myWatch = myClock; System.exit( 0 ); }

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 13 Overloading constructors We may overload the constructor method by declaring different versions with different numbers and/or orders of parameters. For example public Timepiece() {hrs = 12; mins = 30; secs = 0;} public Timepiece(int s) { hrs = s / 3600; mins = (s %3600) / 60; secs = s % 60; }

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 14 Now we can create objects with Timepiece myWatch = new Timepiece(); Timepiece myWatch = new Timepiece(3820);

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 15 Get and set get methods are used to return the values of attributes public int getHrs() { return hrs;} They might also present data in a different form public int getHrs() { if (hrs > 12) return hrs - 12; else return hrs; }

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 16 Get and set Set methods are used to set the values of attributes public void setHrs(int h, boolean isAM) { if (isAM) hrs = h; else hrs = h + 12; }

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 17 Get and set Set method may also do validation public void setHrs(int h) { if (h >= 0)&&(h <= 24) hrs = h; else hrs = 0; }

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 18 Predicate methods Used to test the truth of some property of the object public boolean isTeaTime() { return ((hrs ==16) && (mins==0)&& (secs==0)); }

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 19 Calling the methods Also known as sending messages to the object With static (class) methods, we used the name of the class with a dot and the method name JOptionPane.showMessageDialog(null, “Hello”); With non-static methods, each object effectively has its own copy; we use object name, dot and method name

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 20 myWatch.setHrs(14); int hours = myClock.getHrs(); if (myWatch.isTeaTime() && (myClock.isTeaTime() ) JOptionPane.showMessageDialog( null, “Both agree its time for tea!”);

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 21 TickTock?? public void TickTock() { secs++; if (secs==60) { secs = 0; mins++;} if (mins==60) {mins=0; hrs++;} if (hrs == 24) hrs = 0; } myClock.TickTock()

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 22 Information hiding Objects of class Timepiece hide from other objects the details of how they implement the time. Other objects only know that they can communicate with the object using the objects public methods - the interface We could change the way the time is stored, and as long as the signatures of the object’s methods do not change, objects using this object do not need to be modified Very important when developing large programs

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 23 Example Attributes private int time[] = new int[3]; sample method public void setHrs(int h) { if (h >= 0)&&(h <= 24) time[0] = h; else time[0] = 0; }

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 24 Example Attributes private int timeInSecs; sample method public boolean isTeaTime() { return (timeInSecs/3600==16) && (timeInSecs %3600==0); }

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 25 sample method public void setHrs(int h) { if (h >= 0)&&(h <= 24) timeInSecs = (timeInSecs % 3600) + h * 3600; else timeInSecs = (timeInSecs % 3600); }

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 26 To facilitate information hiding, we declare instance variables as private –cannot be accessed by methods of other classes/ objects … and methods which we want other classes/ objects to be able to access as public The latter form the interface between any object of this class and the rest of the program. Note the meaning of static (class attributes/ methods) –counting objects etc.

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 27 toString() Inheritance public class TimePiece extends Object Class Object is in package java.lang Inherited by all Java classes Defines 11 methods, including toString(), which all Java objects will have We can override these methods

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 28 Overridden toString() public String toString() { return “\n” + hrs + “ hours ” + mins + “ minutes and ” + secs + “ seconds”; }

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 29 The neat bit We can concatenate an object with a string; this causes the object’s toString() method to be called Example JOptionPane.showMessageDialog(null, “The time by my watch is: “+ myWatch); This prints The time by my watch is: 4 hours 10 minutes and 18 seconds

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 30 This is just the beginning... Object-oriented programming is lots of fun You’ll be doing lots of it if you continue your study of programming It’s the present and the future Enjoy!