Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 1 CMT1000: Introduction to Programming Ed Currie Lecture 11: Objects."— Presentation transcript:

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

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

3 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

4 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.

5 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)

6 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

7 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

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

9 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

10 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?

11 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

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

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

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

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

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

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

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

19 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

20 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!”);

21 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()

22 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

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

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

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

26 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.

27 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

28 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”; }

29 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

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


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

Similar presentations


Ads by Google