Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spring 2008 Mark Fontenot CSE 1341 - Honors Principles of Computer Science I Note Set 12.

Similar presentations


Presentation on theme: "Spring 2008 Mark Fontenot CSE 1341 - Honors Principles of Computer Science I Note Set 12."— Presentation transcript:

1 Spring 2008 Mark Fontenot mfonten@engr.smu.edu CSE 1341 - Honors Principles of Computer Science I Note Set 12

2 Note Set 12 Overview Objects in depth Interface of a class Review of Access to members the this reference Overloaded Constructors Default and No Arg constructors

3 Time Class public class Time1 { private int hour; private int minute; private int second; public void setTime(int h, int m, int s) { hour = ((h >= 0 && h < 24) ? h : 0); minute = ((m >= 0 && m < 60) ? m : 0); second = ((s >= 0 && s < 60) ? m : 0); } public String toUniversalString() { return String.format(“%02d:%02d:%02d”, hour, minute, second); } public String toString () { return String.format(“%d:%02d:%02d %s”, ((hour == 0 || hour == 12) ? 12 : hour % 12 ), minute, second, (hour < 12 ? “AM” : “PM”) ); } Public Interface Time is a class. Objects of type Time provide a service. The services are indicated by the public interface

4 Time Class public class Time1 { private int hour; private int minute; private int second; public void setTime(int h, int m, int s) { hour = ((h >= 0 && h < 24) ? h : 0); minute = ((m >= 0 && m < 60) ? m : 0); second = ((s >= 0 && s < 60) ? m : 0); } public String toUniversalString() { return String.format(“%02d:%02d:%02d”, hour, minute, second); } public String toString () { return String.format(“%d:%02d:%02d %s”, ((hour == 0 || hour == 12) ? 12 : hour % 12 ), minute, second, (hour < 12 ? “AM” : “PM”) ); } methods that modify private instance variables should perform error checking – keeps object in consistent state more elegant error checking to come methods that modify private instance variables should perform error checking – keeps object in consistent state more elegant error checking to come

5 Time Class public class Time1 { private int hour; private int minute; private int second; public void setTime(int h, int m, int s) { hour = ((h >= 0 && h < 24) ? h : 0); minute = ((m >= 0 && m < 60) ? m : 0); second = ((s >= 0 && s < 60) ? m : 0); } public String toUniversalString() { return String.format(“%02d:%02d:%02d”, hour, minute, second); } public String toString () { return String.format(“%d:%02d:%02d %s”, ((hour == 0 || hour == 12) ? 12 : hour % 12 ), minute, second, (hour < 12 ? “AM” : “PM”) ); } Every object has a toString() method (implicitly or explicity) Called implicitly whenever a Time object needs to be converted to a string, such as in a printf. Every object has a toString() method (implicitly or explicity) Called implicitly whenever a Time object needs to be converted to a string, such as in a printf.

6 Time1Test public class Time1Test { public static void main (String [] args) { Time1 time = new Time1(); time.setTime(13, 27,6); System.out.print(“Initial Universal Time: “); System.out.println(time.toUniversalString()); System.out.print(“Initial Standard Time: “); System.out.println(time.toString()); System.out.println(); time.setTime(99, 99, 99); System.out.print(“Universal Time: “); System.out.println(time.toUniversalString()); System.out.print(“Standard Time: “); System.out.println(time.toString()); } Initial Universal Time: 13:27:06 Initial Standard Time: 1:27:06 PM Universal Time: 00:00:00 Standard Time: 12:00:00 AM Initial Universal Time: 13:27:06 Initial Standard Time: 1:27:06 PM Universal Time: 00:00:00 Standard Time: 12:00:00 AM

7 Time1Test public class Time1Test { public static void main (String [] args) { Time1 time = new Time1(); time.setTime(99, 99, 99); System.out.print(“Universal Time: “); System.out.println(time.toUniversalString()); System.out.print(“Standard Time: “); System.out.println(time.toString()); } Classes simplify the programming here. We don’t care how this happens, we just know it does. Details are abstracted away in the object’s implementation Clients usually care about what the class does but not how it does it Clients are not affected by a change in implementation. Interfaces change less frequently than implementation You might find a more efficient way of doing this or that later, but you don’t want to break code that depends on the fact that you can do this or that in some wha

8 Time1Test public class Time1Test { public static void main (String [] args) { Time1 time = new Time1(); time.hour = 7; time.minute = 15; time.second = 30; } Illegal – cannot access the private data members directly. Must go through the interface methods that are supplied by the class.

9 The this object reference Every object can access a reference to itself with keyword this. When a non-static method is called, the calling object is passed implicitly to the method Time t = new Time(); t.setTime( 12, 13, 14); System.out.println( t.toString() ); t is passed to the toString method implicitly. in toString, it can be refferenced through the this reference variable.

10 this public void setTime(int h, int m, int s) { hour = ((h >= 0 && h < 24) ? h : 0); minute = ((m >= 0 && m < 60) ? m : 0); second = ((s >= 0 && s < 60) ? m : 0); } How can these variables be accessed if they have no declaration? They were declared when the object that called this method was instantiated. They are referring to those variables that are part of the invoking object. public void setTime(int h, int m, int s) { this.hour = ((h >= 0 && h < 24) ? h : 0); this.minute = ((m >= 0 && m < 60) ? m : 0); this.second = ((s >= 0 && s < 60) ? m : 0); } Equivalent

11 Using this to avoid shadowing public class Test { private int a, b, c; public void foo(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } Accesses the instance data members instead of the parameters.

12 Overloaded constructors public class Test { private int a; public Test() { this(0); } public Test(int x) { a = x; } Call another constructor using the this reference. Calling another constructor with this must be the 1 st line in the body of the constructor Concentrate init logic in one method See Fig 8.5 – Time2.java

13 Special constructor public class Time2 { //Declarations public Time2 (Time2 time){ this(time.getHour(), time.getMinute(), time.getSecond()); } }

14 Accessing sets and gets in the class Reusability – concentrate logic to set value and get value in one area – the accessors and mutators. Consider if time was represented as an int holding number of seconds since midnight rather than 3 ints. if you have many time objects, this can be a space saver – from 12 bytes down to 4. If all conversion is done in set and get, then don’t have to worry about modifying the same logic twice (or getting it wrong twice).

15 Set and Get vs. public data Why not just make the instance variables public?


Download ppt "Spring 2008 Mark Fontenot CSE 1341 - Honors Principles of Computer Science I Note Set 12."

Similar presentations


Ads by Google