150 likes | 259 Views
CSE 1341 - Honors Principles of Computer Science I. Mark Fontenot mfonten@engr.smu.edu. Note Set 12. 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. Time Class.
E N D
CSE 1341 - HonorsPrinciples of Computer Science I Mark Fontenot mfonten@engr.smu.edu Note Set 12
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
Time Class • Time is a class. • Objects of type Time provide a service. • The services are indicated by the public interface 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 Class methods that modify private instance variables should perform error checking – keeps object in consistent state more elegant error checking to come 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”) ); } }
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.
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
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 • Clientsusually 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, butyou don’t want to break code that depends on the fact that you can do this or that in some wha
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.
The this object reference 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 referenced through the this reference variable. 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
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. Equivalent 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); }
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.
Overloaded constructors Call another constructor using the this reference. Calling another constructor with this must be the 1st line in the body of the constructor public class Test { private int a; public Test() { this(0); } public Test(int x) { a = x; } } Concentrate init logic in one method See Fig 8.5 – Time2.java
Special constructor public class Time2 { //Declarations public Time2 (Time2 time){ this(time.getHour(), time.getMinute(), time.getSecond()); } }
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).
Set and Get vs. public data Why not just make the instance variables public?