Chapter 8 - Object-Based Programming

Chapter 8 - Object-Based Programming

Blm 8 Nesne-Tabanl Programlama Outline - nceki dersi tekrar - Giri - Snf Temelleri - rnek 1:Zaman Gsteren Veri Tipini Snfla Tanmlama - Snfn Eriim Alanlar - Kullanlan Nesnenin yelerine this Kalb ile Referans Verme - Ar Yklenmi (Overloaded)Yaplandrclar Kullanmak - Bir yaplandrcdan dierini armak - Get Set Metodlarnn Kullanm - Kompozisyon - p Toplaycs (Garbage Collection) - Static Snf yeleri - Final ile Tantlm Deikenler - Paketler Oluturma ve Eriim nceki Dersi Hatrlama !! Nesne Tabanl Programlamaya (NTP) ait 3 ana konu Kapsllenme Miras Polimorfizm Nesne Tabanl Programlama da geen ders rendiimiz baz terimler

Snf Obje Bir snf oluturan yaplar ye Snf rnei Gizlilik (kapsllenme) Mesaj Alc Miras Hiyerari eidi Polimorfizm Ge balanma (late binding) Giri Nesne- Tabanl Programlama (OOP) Java da herey snflar iinde tanmlanr. Bir snf ise ya API ler tarafndan tanmldr yada kullanc kendisi tanmlar Aslnda Java da deiken tipi olarak lkel veri tipleri (int, float..) Snftan treyen veri tipleri vardr. Objeler Bir snftan trerler Veri (zellikleri-attributeleri) ve metodlar (davranlar) kapsllenir. Objelerin birbirleri ile iletiimi yi tanmlanm arayzler (interfaceler) tarafndan olur. 8.1 Giri (devam) Prosedrel programlama dili C dili rnektir. Harekete dayal Fonksiyonlar programn birimleridir. Nesneye dayal programlama dili

Java dili rnektir. Nesneye dayal Snflar (classlar) programn birimleridir. Fonksiyonlar yada metodlar snflarn iinde kapsllenir. 8.1 Giri (devam) Bu blmde Objeleri (nesneleri) nasl oluturacaz Ve onlar nasl kullanacamz reneceiz. Snf Temelleri Snf iinde tanmlanan metot ve deikenlere, snfn yeleri denir. Java snflar bir main() metoduna sahip olmak zorunda deildir. Eer o snf programn balangc ise o zaman bir tane main metodu tanmlanr. Appletlerde ise main metoduna ihtiya duyulmaz. Basit Snf class Kutu { double en; double boy; double yukseklik; } Kutu sandik = new Kutu(); Kutu sandik nesnenin referansn bildirir. sandik= new Kutu() bir kutu nesnesi oluturulur. New Komutu New komutu: Bellekte bir nesne iin dinamik olarak yer ayrr. Run-time zamannda olur. Basit tipler iin nesne oluturulmaz.Bylece bu tipteki deikenler daha verimli alrlar.

8.2 rnek 1:Zaman Gsteren Veri Tipini Snfla Tanmlama Time1 ve TimeTest adnda iki snfmz var. Time1.java, Time1 snfn gsteriyor. TimeTest.java, TimeTest snfn gsteriyor. public tantlm snflar mutlaka ayr dosyalarda tantlmaldr. Time1 snf kendi bana altrlamaz. main metodu yok main metodunu ieren snf olan TimeTest snf, Time1 objesini oluturur ve kullanr. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

// Fig. 8.1: Time1.java // Time1 class declaration maintains the time in 24-hour format. import java.text.DecimalFormat; Time1 (subclass) extends superclass java.lang.Object Time1.java public class Time1 extends Object { (Chapter 9 discusses inheritance) private int hour; // 0 - 23 Line 5 private int minute; // 0 - 59 (subclass) private int second; // 0 - 59 private variables (andTime1 methods) are extends accessible only to methods in thissuperclass class // Time1 constructor initializes each instance variable to zero; java.lang.Objec // ensures that each Time1 object starts in a consistent state t public Time1() Lines 6-8 { private variables setTime( 0, 0, 0 ); } Time1 constructor creates Lines 12-15 Time1 constructor

// set a new time value using universal time; Time1 perform object then invokes then invokes method // validity checks on the data; set invalid values method to zero setTime setTime public void setTime( int h, int m, int s ) Line 19 { hour = ( ( h >= 0 && h < 24 ) ? h : 0 ); public methods minute = ( ( m >= 0 && m < 60 ) ? m : 0 ); Method setTime sets private 19-24 public methods (andLines variables) variables according to arguments second = ( ( s >= 0 && s < 60 ) ? s : 0 ); Method setTime are accessible wherever program } sets private has Time1 reference variables according to arguments 26 27 28 29 30 31

32 33 34 35 36 37 38 39 40 41 42 43 44 45 // convert to String in universal-time format public String toUniversalString() { DecimalFormat twoDigits = new DecimalFormat( "00" ); return twoDigits.format( hour ) + ":" + twoDigits.format( minute ) + ":" + twoDigits.format( second ); } // convert to String in standard-time format public String toStandardString() { DecimalFormat twoDigits = new DecimalFormat( "00" ); return ( (hour == 12 || hour == 0) ? 12 : hour % 12 ) + ":" + twoDigits.format( minute ) + ":" + twoDigits.format( second ) + ( hour < 12 ? " AM" : " PM" ); } } // end class Time1 Time1.java rnek 1:Zaman Gsteren Veri Tipini Snfla Tanmlama (devam) Her Java snf baka bir snf miras alr.

Time1 snf java.lang.Object snfn miras alr (extend). Eer bir snf tanmnda extend szc yoksa o snf Kesinlikle Object snfn miras almtr. Object Snf Metotlar Object clone() boolean equals(Object nesne) void finalize() String toString() Class getClas() int hashCode() void notify() .... rnek 1:Zaman Gsteren Veri Tipini Snfla Tanmlama (devam) Snf yaplandrcs ( constructor) Snf ismi ile ayn ad tar. Snfa ait deikenlerin ilk deerlerini verir. O snfa ait bir obje oluturulaca zaman otomatik olarak yaplandrc arlr. Parametre alabilir ama geriye deer dndrmez. Bir snf birden fazla yaplandrcya sahip olabilir. (overloading sayesinde) Time1 snfnn yaplandrcs (satr 12-15) rnek 1:Zaman Gsteren Veri Tipini Snfla Tanmlama (devam) Snf yaplandrcs ( constructor) Eer uygulamamza herhangi bir yaplandrc koymazsak Java bu ilemi kendi otomatik olarak yapmaktadr. Varsaylan yaplandrclar (parametresiz yaplandrclar,default constructor veya no-args constructor) ii bo bir yordam olarak dnlebilir Eer kendimiz yaplandrc yazarsak, Java bizden varsylan

yaplandrc desteini ekecektir. Kendimize ait zel yaplandrclar tanmlarsak Javaya "Ben ne yaptm biliyorum, ltfen karma" demi oluruz. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 // Fig. 8.2: TimeTest1.java Declare and create instance of class // Class TimeTest1 to exercise class Time1. Time1 by calling Time1 constructor import javax.swing.JOptionPane;

public class TimeTest1 { TimeTest1.java Line 9 Declare and create interacts with Time1 instance of class by calling Time1 public methods Time1 by calling // append String version of time to String output Time1 constructor String output = "The initial universal time is: " + public static void main( String args[] ) { TimeTest1 Time1 time = new Time1(); // calls Time1 constructor time.toUniversalString() + "\nThe initial standard time is: " + time.toStandardString(); // change time and append updated time to output time.setTime( 13, 27, 6 ); output += "\n\nUniversal time after setTime is: " + time.toUniversalString() + "\nStandard time after setTime is: " + time.toStandardString(); // set time with invalid values; append updated time to output time.setTime( 99, 99, 99 ); output += "\n\nAfter attempting invalid settings: " + "\nUniversal time: " + time.toUniversalString() + "\nStandard time: " + time.toStandardString(); Lines 12-26 TimeTest1 interacts with Time1 by calling Time1 public methods

28 29 30 31 32 33 34 35 JOptionPane.showMessageDialog( null, output, "Testing Class Time1", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } // end main } // end class TimeTest1 TimeTest1.java Snfn Eriim Alanlar Snfn eriim alan Snf deikenleri ve metodlar Snf deikenleri (yeleri) tm snf metodlar tarafndan eriilebilir. yeler isimleriyle refere edilirler. nesneReferanssmi.nesneyesmi Sakl snf deikenleri this.deikenismi yelere Eriimde Kontrol ye eriim alann deitiriciler Snf deikenlerine ve metodlarna eriim anahtarlar public Deikenler ve metodlar snf tarafndan retilen nesneler tarafndan ulalabilir. private

Deikenler ve metodlar snf tarafndan retilen nesneler tarafndan ulalamazlar. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 // Fig. 8.3: TimeTest2.java // Errors resulting from attempts to access private members of Time1. public class TimeTest2 { public static void main( String args[] ) { Time1 time = new Time1(); time.hour = 7; // error: time.minute = 15; // error: time.second = 30; // error: } Lines 9-11 Compiler error hour is a private instance variable TimeTest2 cannot minute is a private instance variable directly access second is a private instance variable

Time1s private Compiler error TimeTest2 cannot data directly access Time1s private data } // end class TimeTest2 TimeTest2.java:9: hour has private access in Time1 time.hour = 7; // error: hour is a private instance variable ^ TimeTest2.java:10: minute has private access in Time1 time.minute = 15; // error: minute is a private instance variable ^ TimeTest2.java:11: second has private access in Time1 time.second = 30; // error: second is a private instance variable ^ 3 errors TimeTest2.java Kullanlan Nesnenin yelerine this Kalb ile Referans Verme Anahtar kelime this (this reference) Nesnenin kendisini referans etmesini salar. Bu referans sayesinde nesnelere ait global alanlara erime frsat buluruz. 1 2 3 4 5 6 7

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 // Fig. 8.4: ThisTest.java // Using the this reference to refer to instance variables and methods. import javax.swing.*; import java.text.DecimalFormat; public class ThisTest { public static void main( String args[] ) { SimpleTime time = new SimpleTime( 12, 30, 19 ); JOptionPane.showMessageDialog( null, time.buildString(), "Demonstrating the \"this\" Reference", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } } // end class ThisTest // class SimpleTime demonstrates the "this" reference class SimpleTime { private int hour;

private int minute; private int second; ThisTest.java 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 // constructor uses parameter names identical to instance variable // names; "this" reference required to distinguish between names

public SimpleTime( int hour, int minute, int second ) { this.hour = hour; // set "this" object's hour this used to distinguish this.minute = minute; // set "this" object's minute between arguments and this.second = second; // set "this" object's second ThisTest variables } // use explicit and implicit "this" to call toStandardString public String buildString() { return "this.toStandardString(): " + this.toStandardString() + "\ntoStandardString(): " + toStandardString(); } // return String representation of SimpleTime public String toStandardString() { DecimalFormat twoDigits = new DecimalFormat( "00" ); } // end class SimpleTime Lines 31-33 this used to distinguish between argumens and variables Lines 39-40 use explicit and implicit this to call Use explicit and toStandarsString implicit this to call toStandardString // "this" is not required here, because method does not // have local variables with same names as instance variables

return twoDigits.format( this.hour ) + ":" + twoDigits.format( this.minute ) + ":" + twoDigits.format( this.second ); } ThisTest.java Ar Yklenmi (Overloaded)Yaplandrclar Kullanmak Ar Yklenmi Yaplandrclar Ayn ismi alan metodlar (ayn snfn iinde) Parametre listeleri farkl olmak zorunda 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

25 26 27 28 // Fig. 8.5: Time2.java // Time2 class declaration with overloaded constructors. import java.text.DecimalFormat; public class Time2 { private int hour; private int minute; private int second; Time2.java // 0 - 23 // 0 - 59 // 0 - 59 Lines 12-15 No-argument (default) Use this to invoke the Time2 constructor constructor declared at lines 30-33 // Time2 constructor initializes each instance variable to zero; Line 14 // ensures that Time object starts in a consistent state Use this to invoke the public Time2() Time2 constructor { Overloaded constructor declared at lines 30-33 this( 0, 0, 0 ); // invoke Time2 constructor

withint three arguments has one argument } Lines 18-21 Overloaded // Time2 constructor: hour supplied, minute and second defaulted to 0 constructor has one public Time2( int h ) Second overloaded constructor int argument { has with two int Lines 24-27 this( h, 0, 0 ); // invoke Time2 constructor threearguments arguments } Second overloaded constructor has two // Time2 constructor: hour and minute supplied, second defaulted to 0 int arguments public Time2( int h, int m ) No-argument (default) constructor { this( h, m, 0 ); // invoke Time2 constructor with three arguments } 29 30 31 32 33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 // Time2 constructor: hour, minute and second supplied public Time2( int h, int m, int s ) Third overloaded constructor { has three int arguments setTime( h, m, s ); // invoke setTime to validate time Time2.java } // Time2 constructor: another Time2 object supplied public Time2( Time2 time ) {

// invoke Time2 constructor with three arguments this( time.hour, time.minute, time.second ); Fourth overloaded } constructor has Time2 argument // set a new time value using universal time; perform // validity checks on data; set invalid values to zero 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 ) ? s : 0 ); } // convert to String in universal-time format public String toUniversalString() { DecimalFormat twoDigits = new DecimalFormat( "00" ); return twoDigits.format( hour ) + ":" + twoDigits.format( minute ) + ":" + twoDigits.format( second ); } Lines 30-33 Third overloaded constructor has three int arguments Lines 36-40 Fourth overloaded constructor has Time2 argument 59 60 61 62

63 64 65 66 67 68 69 70 // convert to String in standard-time format public String toStandardString() { DecimalFormat twoDigits = new DecimalFormat( "00" ); return ( (hour == 12 || hour == 0) ? 12 : hour % 12 ) + ":" + twoDigits.format( minute ) + ":" + twoDigits.format( second ) + ( hour < 12 ? " AM" : " PM" ); } } // end class Time2 Time2.java 1 // Fig. 8.6: TimeTest3.java 2 3 4 5 6 7 8 9 10 11 12 13

14 15 16 17 18 19 20 21 22 23 24 25 26 27 // Overloaded constructors used to initialize Time2 objects. import javax.swing.*; public class TimeTest3 { public static { Time2 t1 = Time2 t2 = Time2 t3 = Time2 t4 = Time2 t5 = Time2 t6 = void main( String args[] ) new new new new new new String output "\nt1: all

"\n " "\n " Time2(); Time2( 2 ); Time2( 21, 34 ); Time2( 12, 25, 42 ); Time2( 27, 74, 99 ); Time2( t4 ); // // // // // // TimeTest3.java Instantiate each Time2 reference Lines 9-14 using a different constructor Instantiate each Time2 reference using a different 00:00:00 constructor 02:00:00 21:34:00 12:25:42 00:00:00 12:25:42 = "Constructed with: " + arguments defaulted" +

+ t1.toUniversalString() + + t1.toStandardString(); output += "\nt2: hour specified; minute and second defaulted" + "\n " + t2.toUniversalString() + "\n " + t2.toStandardString(); output += "\nt3: hour and minute specified; second defaulted" + "\n " + t3.toUniversalString() + "\n " + t3.toStandardString(); 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 output += "\nt4: hour, minute and second specified" +

"\n " + t4.toUniversalString() + "\n " + t4.toStandardString(); output += "\nt5: all invalid values specified" + "\n " + t5.toUniversalString() + "\n " + t5.toStandardString(); output += "\nt6: Time2 object t4 specified" + "\n " + t6.toUniversalString() + "\n " + t6.toStandardString(); JOptionPane.showMessageDialog( null, output, "Overloaded Constructors", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } // end main } // end class TimeTest3 TimeTest3.java Bir yaplandrcdan dierini armak Yaplandrc ierisinden dier bir yaplandrcy arrken this ifadesi her zaman ilk satrda yazlmaldr. Her zaman yaplandrclar ierisinden this ifadesi ile baka bir yaplandrc arlr. Yaplandrclar iersinde birden fazla this ifadesi ile baka yaplandrc arlamaz. 8.8 Set ve Get Metodlar Kullanm Erien metod (get method) public method private trndeki verileri okumay salar. Deitiren metod (set method)

public method private trndeki verileri deiimini salar. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 // Fig. 8.7: Time3.java // Time3 class declaration with set and get methods. import java.text.DecimalFormat; public class Time3 { private int hour;

private int minute; private int second; Time3.java // 0 - 23 // 0 - 59 // 0 - 59 private variables cannot be accessed directly by objects in differenttoclasses instance variable zero; // Time3 constructor initializes each // ensures that Time object starts in a consistent state public Time3() { this( 0, 0, 0 ); // invoke Time3 constructor with three arguments } // Time3 constructor: hour supplied, minute and second defaulted to 0 public Time3( int h ) { this( h, 0, 0 ); // invoke Time3 constructor with three arguments } // Time3 constructor: hour and minute supplied, second defaulted to 0 public Time3( int h, int m ) { this( h, m, 0 ); // invoke Time3 constructor with three arguments } Lines 6-8 private variables cannot be accessed directly by objects in different classes

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 // Time3 constructor: hour, minute and second supplied public Time3( int h, int m, int s ) { setTime( h, m, s ); } // Time3 constructor: another Time3 object supplied

public Time3( Time3 time ) { // invoke Time3 constructor with three arguments this( time.getHour(), time.getMinute(), time.getSecond() ); } // Set Methods // set a new time value using universal time; perform // validity checks on data; set invalid values to zero public void setTime( int h, int m, int s ) { setHour( h ); // set the hour setMinute( m ); // set the minute setSecond( s ); // set the second } // validate and set hour public void setHour( int h ) { hour = ( ( h >= 0 && h < 24 ) ? h : 0 ); } Time3.java Lines 45-68 Set methods allows objects to manipulate private variables Set methods allows objects to manipulate private variables 58 59 60 61 62 63 64

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 // validate and set minute public void setMinute( int m ) { minute = ( ( m >= 0 && m < 60 ) ? m : 0 ); } // validate and set second public void setSecond( int s ) { second = ( ( s >= 0 && s < 60 ) ? s : 0 ); } // Get Methods // get hour value public int getHour() { return hour; } // get minute value public int getMinute()

{ return minute; } Get methods allow objects to read private variables Time3.java Lines 72-87 Get methods allow objects to read private variables 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

107 108 109 110 // get second value public int getSecond() { return second; } // convert to String in universal-time format public String toUniversalString() { DecimalFormat twoDigits = new DecimalFormat( "00" ); return twoDigits.format( getHour() ) + ":" + twoDigits.format( getMinute() ) + ":" + twoDigits.format( getSecond() ); } // convert to String in standard-time format public String toStandardString() { DecimalFormat twoDigits = new DecimalFormat( "00" ); return ( ( getHour() == 12 || getHour() == 0 ) ? 12 : getHour() % 12 ) + ":" + twoDigits.format( getMinute() ) + ":" + twoDigits.format( getSecond() ) + ( getHour() < 12 ? " AM" : " PM" ); } } // end class Time3 Time3.java 1 2 3 4 5 6

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 // Fig. 8.8: TimeTest4.java // Demonstrating the Time3 class set and get methods. import java.awt.*; import java.awt.event.*; import javax.swing.*; TimeTest4.java public class TimeTest4 extends JApplet implements ActionListener { private Time3 time; Declare and instantiate private JLabel hourLabel, minuteLabel, secondLabel;

Time3 objectdisplayField; private JTextIField hourField, minuteField, secondField, private JButton tickButton; // create Time3 object and set up GUI public void init() { time = new Time3(); // create Time3 object Lines 8 and 17 Declare and instantiate Time3 object Lines 25 and 31 JTextFields allow user to specify hour. // get applet's content pane and change its layout to FlowLayout Container container = getContentPane(); container.setLayout( new FlowLayout() ); // set up hourLabel and hourField hourLabel = new JLabel( "Set Hour" ); hourField = new JTextField( 10 ); container.add( hourLabel ); container.add( hourField ); JTextFields allow user to specify hour 29 30 31 32 33 34 35 36 37 38

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 // set up minuteLabel and minuteField minuteLabel = new JLabel( "Set Minute" ); minuteField = new JTextField( 10 ); container.add( minuteLabel ); container.add( minuteField ); // set up secondLabel and secondField secondLabel = new JLabel( "Set Second" ); secondField = new JTextField( 10 ); container.add( secondLabel ); container.add( secondField ); JTextField allows user to specify minute JTextField allows user to specify second

// set up displayField displayField = new JTextField( 30 ); displayField.setEditable( false ); container.add( displayField ); // set up tickButton tickButton = new JButton( "Add 1 to Second" ); container.add( tickButton ); // register event handlers; this applet is the ActionListener, // which contains method actionPerformed that will be called to // handle action events generated by hourField, minuteField, // secondField and tickButton hourField.addActionListener( this ); minuteField.addActionListener( this ); secondField.addActionListener( this ); tickButton.addActionListener( this ); TimeTest4.java Line 31 JTextField allows user to specify minute Line 37 JTextField allows user to specify second 58 59 60 61 62 63 64 65 66 67 68 69 70

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 displayTime(); // update text in displayField and status bar } // end method init // event handler for button and textfield events public void actionPerformed( ActionEvent event ) { // process tickButton event if ( event.getSource() == tickButton ) tick(); // process hourField event else if ( event.getSource() == hourField ) { time.setHour( Integer.parseInt( event.getActionCommand() ) ); hourField.setText( "" ); } TimeTest4.java Lines 71-74 Lines 77-80 Lines 83-86 TimeTest5 uses Time3 set methods to

set Time3 private variables // process minuteField event else if ( event.getSource() == minuteField ) { time.setMinute( Integer.parseInt( event.getActionCommand() ) ); minuteField.setText( "" ); TimeTest5 uses Time3 set methods } set Time3 private variables // process secondField event else if ( event.getSource() == secondField ) { time.setSecond( Integer.parseInt( event.getActionCommand() ) ); secondField.setText( "" ); } to 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

106 107 108 109 110 111 112 113 114 115 116 117 displayTime(); // update text in displayField and status bar } // end method actionPerformed // update displayField and applet container's status bar public void displayTime() { displayField.setText( "Hour: " + time.getHour() + "; Minute: " + time.getMinute() + "; Second: " + time.getSecond() ); showStatus( "Standard time is: " + time.toStandardString() + "; Universal time is: " + time.toUniversalString() ); TimeTest4.java Lines 95-96 TimeTest5 uses Time3 get methods to read Time3 private variables } // end method updateDisplay // add one to second and update hour/minute if necessary TimeTest5 uses Time3 get methods public void tick() to read Time3 private variables { time.setSecond( ( time.getSecond() + 1 ) % 60 );

if ( time.getSecond() == 0 ) { time.setMinute( ( time.getMinute() + 1 ) % 60 ); if ( time.getMinute() == 0 ) time.setHour( ( time.getHour() + 1 ) % 24 ); } } // end method tick } // end class TimeTest4 TimeTest4.java TimeTest4.java TimeTest4.java Kompozisyon Kompozisyon Bir Snf dier snf nesne referanslarn iinde barndrabilir. Bu referanslar snfn yeleridir. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

19 20 21 22 23 24 25 26 // Fig. 8.9: Date.java // Date class declaration. public class Date { private int month; private int day; private int year; Class Date encapsulates data that describes date Date.java // 1-12 // 1-31 based on month // any year // constructor: call checkMonth to confirm proper value for month; // call checkDay to confirm proper value for day public Date( int theMonth, int theDay, int theYear ) { month = checkMonth( theMonth ); // validate month year = theYear; // could validate year day = checkDay( theDay ); // validate day Date constructor System.out.println( "Date object constructor for toDateString() );

Line 4 Class Date encapsulates data that describes date Lines 11-20 Date constructor instantiates Date object based on instantiates specified arguments Date object based on date " + specified arguments } // end Date constructor // utility method to confirm proper month value private int checkMonth( int testMonth ) { if ( testMonth > 0 && testMonth <= 12 ) // validate month return testMonth; 27 28 29 30 31 32 33 34 35 36 37 38 39

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 else { // month is invalid System.out.println( "Invalid month (" + testMonth + ") set to 1." ); return 1; // maintain object in consistent state } } // end method checkMonth // utility method to confirm proper day value based on month and year private int checkDay( int testDay ) { int daysPerMonth[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // check if day in range for month if ( testDay > 0 && testDay <= daysPerMonth[ month ] ) return testDay; // check for leap year if ( month == 2 && testDay == 29 && ( year % 400 == 0 || ( year % 4 == 0 && year % 100 != 0 ) ) ) return testDay; System.out.println( "Invalid day (" + testDay + ") set to 1." ); return 1;

// maintain object in consistent state } // end method checkDay Date.java 56 57 58 59 60 61 62 63 // return a String of the form month/day/year public String toDateString() { return month + "/" + day + "/" + year; } } // end class Date Date.java 1 2 3 4 5 6 7 8 9 10 11 12

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 // Fig. 8.10: Employee.java // Employee class declaration. public class Employee { private String firstName; private String lastName; private Date birthDate; private Date hireDate; Employee.java Employee is composed of two references to Date objects // constructor to initialize name, birth date and hire date public Employee( String first, String last, Date dateOfBirth, Date dateOfHire ) { firstName = first; lastName = last; birthDate = dateOfBirth; hireDate = dateOfHire;

} // convert Employee to String format public String toEmployeeString() { return lastName + ", " + firstName + " Hired: " + hireDate.toDateString() + " Birthday: " + birthDate.toDateString(); } } // end class Employee Lines 7-8 Employee is composed of two references to Date objects 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 // Fig. 8.11: EmployeeTest.java

// Demonstrating an object with a member object. import javax.swing.JOptionPane; public class EmployeeTest { public static void main( String args[] ) { Date birth = new Date( 7, 24, 1949 ); Date hire = new Date( 3, 12, 1988 ); Employee employee = new Employee( "Bob", "Jones", birth, hire ); JOptionPane.showMessageDialog( null, employee.toEmployeeString(), "Testing Class Employee", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } } // end class EmployeeTest Date object constructor for date 7/24/1949 Date object constructor for date 3/12/1988 EmployeeTest.ja va 8.10 p Toplaycs (Garbage Collection) p Toplaycs Java dilinde, C++ dilinde olduu gibi oluturulan nesnelerimizi ileri bitince yok etme zgrl kodu yazan kiinin elinde deildir Bir nesnenin gerekten p olup olmadna karar veren mekanizma p toplaycsdr ( garbage collector). finalize () metodu Akllarda tutulmas gereken dier bir konu ise eer uygulamanz ok fazla sayda p nesnesi ( kullanlmayan nesne) retmiyorsa, p toplaycs ( garbage collector) devreye girmeyebilir. Birbaka nemli nokta; System.gc()

ile p toplaycsn tetiklemezsek , p toplaycsnn ne zaman devreye girip p haline dnm olan nesneleri bellekten temizlenecei bilinemez. 8.11 Static Snf yeleri static tanmlanm deikenler Sadece global olan alanlara statik zelliini verebiliriz. Yerel deikenlerin statik olma zellikleri yoktur. Statik alanlar, bir snfa ait olan tm nesneler iin ayn bellek alannda bulunurlar. static tanmlanm metodlar Statik yordamlar (snf yordamlar), nesnelerden bamsz yordamlardr. Statik bir yordam armak iin herhangi bir snfa ait nesne oluturma zorunluluu yoktur. Statik olmayan yordamlardan (nesneye ait yordamlar),statik yordamlar rahatlkla arlabilmesine karn statik yordamlardan nesne yordamlarn dorudan aramayz. Bir metodun static mi yoksa obje metodu olup olmayacana nasl karar vereceiz? Bir metodun static mi yoksa obje metodu olup olmayacana nasl karar vereceiz? 1 2 3 4 5 6

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 // Fig. 8.12: Employee.java // Employee class declaration. public class Employee { private String firstName; private String lastName; private static int count = 0; Employee objects share one Employee.java instance of count // number of objects in memory // initialize employee, add 1 to static count and

// output String indicating that constructor was called public Employee( String first, String last ) { firstName = first; lastName = last; ++count; // increment static count of employees System.out.println( "Employee constructor: " + firstName + " " + lastName ); } Line 6 Employee objects share one instance of count Lines 23-28 Called when Employee is marked for garbage collection Called when Employee is marked for garbage collection collector // subtract 1 from static count when garbage // calls finalize to clean up object and output String // indicating that finalize was called protected void finalize() { --count; // decrement static count of employees System.out.println( "Employee finalizer: " + firstName + " " + lastName + "; count = " + count ); } 30 31 32 33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 // get first name public String getFirstName() { return firstName; } // get last name public String getLastName() { return lastName; } Employee.java static method accesses static variable count // static method to get static count value public static int getCount() { return count; }

} // end class Employee Lines 43-46 static method accesses static variable count 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 // Fig. 8.13: EmployeeTest.java

// Test Employee class with static class variable, // static class method, and dynamic memory. import javax.swing.*; EmployeeTest can invoke Employee EmployeeTest.ja va static method, even though public class EmployeeTest { Employee has not been instantiated Line 12 public static void main( String args[] ) { EmployeeTest can // prove that count is 0 before creating Employees invoke Employee String output = "Employees before instantiation: " + static method, even Employee.getCount(); though Employee has not been // create two Employees; count should be 2 Employee e1 = new Employee( "Susan", "Baker" ); instantiated Employee e2 = new Employee( "Bob", "Jones" ); // prove that count is 2 after creating two Employees output += "\n\nEmployees after instantiation: " + "\nvia e1.getCount(): " + e1.getCount() + "\nvia e2.getCount(): " + e2.getCount() + "\nvia Employee.getCount(): " + Employee.getCount(); // get names of Employees output += "\n\nEmployee 1: " + e1.getFirstName() + " " + e1.getLastName() + "\nEmployee 2: " + e2.getFirstName() + " " + e2.getLastName(); 29 30

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 // // // e1 e2 decrement reference count for each Employee object; in this example, there is only one reference to each Employee, so these statements mark each Employee object for garbage collection Calls Javas automatic garbage= null; collection mechanism = null; System.gc(); // suggest call to garbage collector // show Employee count after calling garbage collector; count // displayed may be 0, 1 or 2 based on whether garbage collector // executes immediately and number of Employee objects collected

output += "\n\nEmployees after System.gc(): " + Employee.getCount(); JOptionPane.showMessageDialog( null, output, "Static Members", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } } // end class EmployeeTest Employee Employee Employee Employee constructor: Susan Baker constructor: Bob Jones finalizer: Susan Baker; count = 1 finalizer: Bob Jones; count = 0 EmployeeTest.ja va Line 35 Calls Javas automatic garbage-collection mechanism 8.12 Final ile Tantlm Deikenler final anahtar kelimesi Deiken gncelletirilemez. final deikeni deitirmeye kalklsa hata verir. private final int INCREMENT = 5; INCREMENT deikeni sabit olarak tantlmtr. 1

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 // Fig. 8.14: IncrementTest.java // Initializing a final variable. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class IncrementTest extends JApplet implements ActionListener {

private Increment incrementObject; private JButton button; // set up GUI public void init() { incrementObject = new Increment( 5 ); Container container = getContentPane(); button = new JButton( "Click to increment" ); button.addActionListener( this ); container.add( button ); } // add INCREMENT to total when user clicks button public void actionPerformed( ActionEvent actionEvent ) { incrementObject.increment(); showStatus( incrementObject.toIncrementString() ); } } // end class IncrementTest IncrementTest.j ava 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

47 48 49 50 51 52 53 54 55 56 57 // class containing constant variable class Increment { private int count = 0; // number of increments private int total = 0; // total of all increments final private final int INCREMENT; // constant variable // initialize constant INCREMENT public Increment( int incrementValue ) { INCREMENT = incrementValue; // intialize } keyword declaresIncrement.java INCREMENT as constant Line 36 final keyword declares INCREMENT constant variable (once) as constant // add INCREMENT to total and add 1 to count public void increment() final variable INCREMENT must

{ total += INCREMENT; be initialized before using it ++count; } // return String representation of an Increment object's data public String toIncrementString() { return "After increment " + count + ": total = " + total; } } // end class Increment Line 41 final variable INCREMENT must be initialized before using it IncrementTest.java:40: variable INCREMENT might not have been initialized { ^ 1 error 8.13 Paketler Oluturma Bizler programlarmza paketler dahil (import) ederiz. likili snflar yada arayzler Complex uygulamalar daha kolay ynetmek iin. Yazlmlarn yeniden kullanabilirliini artrma

Tekil snf isimleri salamak iin Popler paket-isimlendirme Internet domain isminin tersi e.g., com.deitel 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 // Fig. 8.16: Time1.java // Time1 class declaration maintains the time inClass 24-hour format.

Time1 is placed package com.deitel.jhtp5.ch08; in this package import java.text.DecimalFormat; public class Time1 extends Object { private int hour; // 0 - 23 private int minute; // 0 - 59 private int second; // 0 - 59 Class Time1 is in directory com/deitel/jhtp5/ch08 // Time1 constructor initializes each instance variable to zero; // ensures that each Time1 object starts in a consistent state import class DecimalFormat public Time1() from package java.text { setTime( 0, 0, 0 ); } // set a new time value using universal time; perform // validity checks on the data; set invalid values to zero 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 ) ? s : 0 ); } Time1.java Line 3 Class Time1 is placed in this package

Line 3 Class Time1 is in directory com/deitel/jhtp 5/ch08 Line 5 import class DecimalFormat from package java.text 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 // convert to String in universal-time format public String toUniversalString() { DecimalFormat twoDigits = new DecimalFormat( "00" );

DecimalFormat from Time1.java package java.text return twoDigits.format( hour ) + ":" + twoDigits.format( minute ) + ":" + twoDigits.format( second ); } // convert to String in standard-time format public String toStandardString() { DecimalFormat twoDigits = new DecimalFormat( "00" ); return ( (hour == 12 || hour == 0) ? 12 : hour % 12 ) + ":" + twoDigits.format( minute ) + ":" + twoDigits.format( second ) + ( hour < 12 ? " AM" : " PM" ); } } // end class Time1 Line 31 DecimalFormat from package java.text 1 2 3 4 5 6 7 8 9 10 11 12 13

14 15 16 17 18 19 20 21 22 23 24 25 26 // Fig. 8.17: TimeTest1.java // Class TimeTest1 to exercise class Time1. import class JOptionPane from package javax.swing // Java packages import javax.swing.JOptionPane; // Deitel packages import com.deitel.jhtp5.ch08.Time1; public class TimeTest1 { TimeTest1.java Line 5 import class // import Time1 class JOptionPane from import class Time1 from package package javax.swing com.deitel.jhtp4.ch08 args[] )

public static void main( String { Time1 time = new Time1(); // calls Time1 constructor TimeTest1 can declare Time1 object // append String version of time to String output String output = "The initial universal time is: " + time.toUniversalString() + "\nThe initial standard time is: " + time.toStandardString(); // change time and append updated time to output time.setTime( 13, 27, 6 ); output += "\n\nUniversal time after setTime is: " + time.toUniversalString() + "\nStandard time after setTime is: " + time.toStandardString(); Line 8 import class Time1 from package com.deitel.jhtp 4.ch08 Line 14 TimeTest1 can declare Time1 object 27 28 29 30 31 32 33 34 35 36 37

38 39 40 // set time with invalid values; append updated time to output time.setTime( 99, 99, 99 ); output += "\n\nAfter attempting invalid settings: " + "\nUniversal time: " + time.toUniversalString() + "\nStandard time: " + time.toStandardString(); JOptionPane.showMessageDialog( null, output, "Testing Class Time1", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } // end main } // end class TimeTest1 TimeTest1.java 8.14 Package Access Package access Variable or method does not have member access modifier 1 2 3 4 5 6 7 8 9 10 11 12 13

14 15 16 17 18 19 20 21 22 23 24 25 26 // Fig. 8.18: PackageDataTest.java // Classes in the same package (i.e., the same directory) can // use package access data of other classes in the same package. Instantiate import javax.swing.JOptionPane; reference PackageDataTest to PackageData object .java public class PackageDataTest { Line 10 Instantiate reference to PackageData object public static void main( String args[] ) { PackageData packageData = new PackageData(); // append String representation of packageData to output

Lines 13-22 String output = "After instantiation:\n" + packageData.toPackageDataString(); PackageDataTestPackageDataTest can // change package access data in packageData packageData.number = 77; packageData.string = "Goodbye"; can access access PackageData object PackageData data, data, because each class because each class shares same package shares same package // append String representation of packageData to output output += "\nAfter changing values:\n" + packageData.toPackageDataString(); JOptionPane.showMessageDialog( null, output, "Package Access", JOptionPane.INFORMATION_MESSAGE ); 27 28 29 30 31 32 33 34 35 36 37

38 39 40 41 42 43 44 45 46 47 48 49 50 System.exit( 0 ); } PackageDataTest .java } // end class PackageDataTest // class with package access instance variables class PackageData { int number; // package-access instance variable String string; // package-access instance variable // constructor public PackageData() { number = 0; string = "Hello"; } Line 33 No access modifier, so class has packageaccess variables No access modifier, so class

has package-access variables // return PackageData object String representation public String toPackageDataString() { return "number: " + number + " string: " + string; } } // end class PackageData

Recently Viewed Presentations

  • APPLYING TO COLLEGE & UNIVERSITIES O APPLICATION DEADLINES

    APPLYING TO COLLEGE & UNIVERSITIES O APPLICATION DEADLINES

    Scholarship opportunities. Residence/Housing . Special Needs consideration - contact schools . Do you need to write an English language proficiency test? ***information session held at SLSS. OSAP information - students need to apply early in Semester 2
  • Umpire Managers - Schua

    Umpire Managers - Schua

    Allow as much advantage as possible. Read the game - do not ball watch. Whistle timing and tone (variation) is important - big loud whistle for big bad offences, quieter whistle for lesser, little offences! TACKLING Watch tackles carefully -...
  • Hazardous Material Safety Program Federal Aviation Administration University

    Hazardous Material Safety Program Federal Aviation Administration University

    Indication of the net mass of the Carbon dioxide, Solid or Dry Ice must be marked on the outside of the package . Shipments prepared in accordance with IATA must display a Class 9 label. Dry Ice - Shipping Perspective
  • Dynasties of China - University of Pittsburgh

    Dynasties of China - University of Pittsburgh

    Han (202 BC-221 AD) (Roman Empire) Legalism replaced by Confucianism. Introduced civil service examination (scholar gentry) Silk Roads developed, opens trade. Capital city was Chang'an, urban, well-planned
  • Analytic Network Process (ANP) slides

    Analytic Network Process (ANP) slides

    The Analytic Network Process (ANP) for Decision Making and Forecasting with Dependence and Feedback With feedback the alternatives depend on the criteria as in a hierarchy but may also depend on each other. The criteria themselves can depend on the...
  • Quantitative Structure Activity Relationships QSAR and 3D-QSAR

    Quantitative Structure Activity Relationships QSAR and 3D-QSAR

    Quantitative Structure Activity Relationships (QSAR) Prof. K. N. RAJINI KANTH M.Pharm.,(PhD) Head, Dept. of Pharmaceutical Analysis Chalapathi Institute of Pharmaceutical Sciences
  • Logical Fallacies

    Logical Fallacies

    AP Language and Composition Introduction Ad Hominem Hasty Generalization False Dilemma Begging the Question Post Hoc False Analogy Definition Logical fallacies are flaws in reasoning that lead to faulty, illogical statements. They are unreasonable argumentative tactics named for what has...
  • Headline goes here - Oxfam-Sol

    Headline goes here - Oxfam-Sol

    2009: het Stockholm Resilience Centre heeft 29 gespecialiseerde wetenschappers in ons ecosysteem bij elkaar gebracht. ... De Donut met haar sociale en planetaire grenzen kan op mondiaal niveau als kompas gebruikt worden om dit doel te verwezenlijken.