Bölüm 4 – Kontrol İfadeleri :1 .kısım

54
Outline 4.1 Giriş 4.2 Algoritmalar 4.3 Pseudocode 4.4 Kontrol İfadeleri 4.5 if tek-seçimli ifadeler 4.6 if else seçimli ifadeler 4.7 while döngü ifadeleri 4.8 Algoritmaların Tasarımı : Örnek Çalışma 1(Sayaç - Kontrollü Döngüler) 4.9 Algoritmaların Tasarımı: Örnek Çalışma 2 (Sonlandırcı- Kontrollü Döngüler) 4.10 Algoritmaların Tasarımı: Örnek Çalışma 2 (İçiçe Kontrollü Yapılar) 4.11 Birleşik Atama Operatörleri 4.12 Artırma ve Azaltma Operatörleri 4.13 İlkel Tipler Bölüm 4 – Kontrol İfadeleri:1.kısım

description

Bölüm 4 – Kontrol İfadeleri :1 .kısım. - PowerPoint PPT Presentation

Transcript of Bölüm 4 – Kontrol İfadeleri :1 .kısım

Page 1: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

Outline4.1 Giriş4.2 Algoritmalar4.3 Pseudocode4.4 Kontrol İfadeleri4.5 if tek-seçimli ifadeler 4.6 if else seçimli ifadeler 4.7 while döngü ifadeleri 4.8 Algoritmaların Tasarımı : Örnek Çalışma 1(Sayaç - Kontrollü Döngüler) 4.9 Algoritmaların Tasarımı: Örnek Çalışma 2 (Sonlandırcı- Kontrollü Döngüler) 4.10 Algoritmaların Tasarımı: Örnek Çalışma 2 (İçiçe Kontrollü Yapılar)4.11 Birleşik Atama Operatörleri 4.12 Artırma ve Azaltma Operatörleri 4.13 İlkel Tipler

Bölüm 4 – Kontrol İfadeleri:1.kısım

Page 2: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

4.1  Giriş

• Bu bölümde Kontrol İfadelerini öğreneceğiz.– Yapısal programlama prensibi

– Kontrol ifadeleri objelerin yapımına yardım eder. (Chapter 8)

Page 3: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

4.2 Algoritmalar

• Algoritma– Belli bir sırada takip edilen işler

• İşler gerçekleştirilir.

• Sıra, hangi işin gerçekleştireceğini tespit eder.

• Program kontrolü– Kontrol ifadeleri bu sıranın oluşumuna yardım eder.

Page 4: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

4.3 Pseudocode

• Pseudocode (Program tasarımı)– Algoritma geliştirmek için kullanılan esnek bir dil

– Bilgisayarda çalıştırılamaz

– Algoritmayı yapanlara kurallardan bağımsız ifade etmeyi sağlar.

Page 5: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

4.4 Kontrol İfadeleri

• Sıralı çalışma– Program kodları baştan başlayıp satır satır çalışır.

• Kontrolün transferi– 3 tip kontrol ifadesi bu kodlar arasında transfer sağlar.

• Sıralı yapılar

• Seçimli yapılar

• Döngüsel yapılar

• Akış diagramı– İş akışını modeller

• İşi tanımlayan semboller

• İş sırasını gösteren oklar

Page 6: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

Fig 4.1 Sıralı akış diyagramı.

Notu toplama ekle

Sayacı 1 artır

Java karşılığı:toplam = toplam + not;

Java karşılığı:sayac = sayac + 1;

Page 7: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

J ava Keywords abstract assert boolean break byte case catch char class continue default do double else extends final finally float for if implements import instanceof int interface long native new package private protected public return short static strictfp super switch synchronized this throw throws transient try void volatile while Keywords that are reserved, but not currently used const goto Fig. 4.2 J ava keywords.

Page 8: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

4.4 Kontrol İfadeleri

• Java sıralı akışa sahiptir.• Java 3 farklı seçim komutu sağlar.

– if– If…else– switch

• Java 3 farklı döngü komutu sağlar.– while– do…while– for

• Bu komutların herbiri ayrılmış kelimelerdir.

Page 9: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

4.5 if tek-seçimli durum

• Tek-giriş/tek-çıkış kontrol ifadeleri• Şart doğru (true) olduğu zaman komut çalışır.

Page 10: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

Fig 4.3 if tek-seçimli durum iş iş akış diyagramı.

[not >= 60]

[not < 60]

print “Geçti”

Page 11: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

4.6 if…else seçimli durum

• Şart doğru (true) olduğu zaman komut çalışır.• Şart yanlış (false) olduğu zaman başka bir

komut çalışır.• Şart operatörü (?:)• İçiçe if…else ifadeleri

Page 12: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

Fig 4.4 if…else iki seçimli durumlar için akış diyagramı.

[not >= 60][not < 60]print “Kaldı” print “Geçti”

Page 13: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

4.7 while Tekrarlama İfadeleri

• Şart doğru (true) olduğu müddetçe dögüye ait komutları çalıştır.

Page 14: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

Fig 4.5 while tekrarlama ifadelerinin akış dyagramı.

[ürün <= 1000]

[ürün > 1000]

Ürün değerinin 2 katını al

birleşim

karar

Java karşılığı:ürün = 2 * ürün;

Page 15: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

4.8 Algoritmaların Tasarlanması: Örnek Çalışma 1 (Kontrollü Sayaç

Döngüsü)• Sayaç

– İfadelerin kaç defa tekrar ettiğini kontrol eden değişken

• Ortalama1.java not ortalamasını hesaplar.– Döngüyü kontrol için sayaç kullanılır.

Page 16: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

 Toplam değişkenini sıfırlaSayaç değişkenine 1 ata

Syaç 10 ‘a eşit yada küçük olduğu müddetçeDiğer notu girToplamı not kadar artırSayacı 1 artır

Toplamı 10 ‘a bölerek sınıf ortalamasına ataSınıf ortalamasını yazdır

Fig. 4.6 Pseudocode algoritması : sınıf ortalamasını bulmak için kontrollü sayaç döngüsü kullanılır.

Page 17: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

2003 Prentice Hall, Inc. All rights reserved.

17

Outline 1 // Fig. 4.6: GradeBook.java

2 // GradeBook class that solves class-average problem using

3 // counter-controlled repetition.

4 import java.util.Scanner; // program uses class Scanner

5

6 public class GradeBook

7 {

8 private String courseName; // name of course this GradeBook represents

9

10 // constructor initializes courseName

11 public GradeBook( String name )

12 {

13 courseName = name; // initializes courseName

14 } // end constructor

15

16 // method to set the course name

17 public void setCourseName( String name )

18 {

19 courseName = name; // store the course name

20 } // end method setCourseName

21

22 // method to retrieve the course name

23 public String getCourseName()

24 {

25 return courseName;

26 } // end method getCourseName

27

• GradeBook.java

• (1 of 3)

Assign a value to instance variable courseName

Declare method setCourseName

Declare method getCourseName

Page 18: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

2003 Prentice Hall, Inc. All rights reserved.

18

Outline28 // display a welcome message to the GradeBook user

29 public void displayMessage()

30 {

31 // getCourseName gets the name of the course

32 System.out.printf( "Welcome to the grade book for\n%s!\n\n",

33 getCourseName() );

34 } // end method displayMessage

35

36 // determine class average based on 10 grades entered by user

37 public void determineClassAverage()

38 {

39 // create Scanner to obtain input from command window

40 Scanner input = new Scanner( System.in );

41

42 int total; // sum of grades entered by user

43 int gradeCounter; // number of the grade to be entered next

44 int grade; // grade value entered by user

45 int average; // average of grades

46

47 // initialization phase

48 total = 0; // initialize total

49 gradeCounter = 1; // initialize loop counter

50

• GradeBook.java

• (2 of 3)

Declare method displayMessage

Declare method determineClassAverage

Declare and initialize Scanner variable input

Declare local int variables total, gradeCounter, grade and average

Page 19: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

2003 Prentice Hall, Inc. All rights reserved.

19

Outline51 // processing phase

52 while ( gradeCounter <= 10 ) // loop 10 times

53 {

54 System.out.print( "Enter grade: " ); // prompt

55 grade = input.nextInt(); // input next grade

56 total = total + grade; // add grade to total

57 gradeCounter = gradeCounter + 1; // increment counter by 1

58 } // end while

59

60 // termination phase

61 average = total / 10; // integer division yields integer result

62

63 // display total and average of grades

64 System.out.printf( "\nTotal of all 10 grades is %d\n", total );

65 System.out.printf( "Class average is %d\n", average );

66 } // end method determineClassAverage

67

68 } // end class GradeBook

• GradeBook.java

• (3 of 3)

while loop iterates as long as gradeCounter <= 10

Increment the counter variable gradeCounter

Calculate average grade

Display results

Page 20: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

2003 Prentice Hall, Inc. All rights reserved.

23

Outline

• GradeBookTest.java

1 // Fig. 4.7: GradeBookTest.java

2 // Create GradeBook object and invoke its determineClassAverage method.

3

4 public class GradeBookTest

5 {

6 public static void main( String args[] )

7 {

8 // create GradeBook object myGradeBook and

9 // pass course name to constructor

10 GradeBook myGradeBook = new GradeBook(

11 "CS101 Introduction to Java Programming" );

12

13 myGradeBook.displayMessage(); // display welcome message

14 myGradeBook.determineClassAverage(); // find average of 10 grades

15 } // end main

16

17 } // end class GradeBookTest

Welcome to the grade book for CS101 Introduction to Java Programming! Enter grade: 67 Enter grade: 78 Enter grade: 89 Enter grade: 67 Enter grade: 87 Enter grade: 98 Enter grade: 93 Enter grade: 85 Enter grade: 82 Enter grade: 100 Total of all 10 grades is 846 Class average is 84

Create a new GradeBook object

Pass the course’s name to the GradeBook constructor as a string

Call GradeBook’s determineClassAverage method

Page 21: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

4.9 Algoritmaların Tasarlanması: Örnek Çalışma 2 (Sonlandırıcı-

Kontrollü Döngüler)• Sonlandırıcı değer

– Veri girişini sonlandırmak amaçlı kullanılır.

• Ortalama2.java döngünün sayısı belli değil– Kullanıcı sonlandırıcı değere (-1) basarak döngüyü

bitiriyor.

Page 22: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

Toplam değişkenini sıfırlaSayaç değişkenine 1 ata

İlk not girişi yap (sonlandırıcı değer de mümkün)

Kullanıcı sonlandırıcı değer girmediği müddetçe Toplamı not değeri kadar artırSayacı 1 artırYeni notu gir(sonlandırcı değer de mümkün)

Eğer sayaç 0 eşit değilseOrtalamayı toplamı sayaca bölerek bulOrtalamayı yazdır

else“Hiçbir not girilmedi” yazdır

Fig. 4.8 Pseudocode algoritması : sınıf ortalamasını bulmak için sonlandırıcı kontrollü döngüs kullanılır.

Page 23: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

2003 Prentice Hall, Inc. All rights reserved.

26

Outline

• GradeBook.java

• (1 of 3)

1 // Fig. 4.9: GradeBook.java

2 // GradeBook class that solves class-average program using

3 // sentinel-controlled repetition.

4 import java.util.Scanner; // program uses class Scanner

5

6 public class GradeBook

7 {

8 private String courseName; // name of course this GradeBook represents

9

10 // constructor initializes courseName

11 public GradeBook( String name )

12 {

13 courseName = name; // initializes courseName

14 } // end constructor

15

16 // method to set the course name

17 public void setCourseName( String name )

18 {

19 courseName = name; // store the course name

20 } // end method setCourseName

21

22 // method to retrieve the course name

23 public String getCourseName()

24 {

25 return courseName;

26 } // end method getCourseName

27

Assign a value to instance variable courseName

Declare method setCourseName

Declare method getCourseName

Page 24: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

2003 Prentice Hall, Inc. All rights reserved.

27

Outline

• GradeBook.java

• (2 of 3)

28 // display a welcome message to the GradeBook user

29 public void displayMessage()

30 {

31 // getCourseName gets the name of the course

32 System.out.printf( "Welcome to the grade book for\n%s!\n\n",

33 getCourseName() );

34 } // end method displayMessage

35

36 // determine the average of an arbitrary number of grades

37 public void determineClassAverage()

38 {

39 // create Scanner to obtain input from command window

40 Scanner input = new Scanner( System.in );

41

42 int total; // sum of grades

43 int gradeCounter; // number of grades entered

44 int grade; // grade value

45 double average; // number with decimal point for average

46

47 // initialization phase

48 total = 0; // initialize total

49 gradeCounter = 0; // initialize loop counter

50

51 // processing phase

52 // prompt for input and read grade from user

53 System.out.print( "Enter grade or -1 to quit: " );

54 grade = input.nextInt();

55

Declare method displayMessage

Declare method determineClassAverage

Declare and initialize Scanner variable input

Declare local int variables total, gradeCounter and grade and double variable average

Page 25: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

2003 Prentice Hall, Inc. All rights reserved.

28

Outline

• GradeBook.java

• (3 of 3)

56 // loop until sentinel value read from user

57 while ( grade != -1 )

58 {

59 total = total + grade; // add grade to total

60 gradeCounter = gradeCounter + 1; // increment counter

61

62 // prompt for input and read next grade from user

63 System.out.print( "Enter grade or -1 to quit: " );

64 grade = input.nextInt();

65 } // end while

66

67 // termination phase

68 // if user entered at least one grade...

69 if ( gradeCounter != 0 )

70 {

71 // calculate average of all grades entered

72 average = (double) total / gradeCounter;

73

74 // display total and average (with two digits of precision)

75 System.out.printf( "\nTotal of the %d grades entered is %d\n",

76 gradeCounter, total );

77 System.out.printf( "Class average is %.2f\n", average );

78 } // end if

79 else // no grades were entered, so output appropriate message

80 System.out.println( "No grades were entered" );

81 } // end method determineClassAverage

82

83 } // end class GradeBook

while loop iterates as long as grade != the sentinel value, -1

Calculate average grade using (double) to perform explicit conversion

Display average grade

Display “No grades were entered” message

Page 26: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

2003 Prentice Hall, Inc. All rights reserved.

29

Outline

• GradeBookTest.java

1 // Fig. 4.10: GradeBookTest.java

2 // Create GradeBook object and invoke its determineClassAverage method.

3

4 public class GradeBookTest

5 {

6 public static void main( String args[] )

7 {

8 // create GradeBook object myGradeBook and

9 // pass course name to constructor

10 GradeBook myGradeBook = new GradeBook(

11 "CS101 Introduction to Java Programming" );

12

13 myGradeBook.displayMessage(); // display welcome message

14 myGradeBook.determineClassAverage(); // find average of grades

15 } // end main

16

17 } // end class GradeBookTest

Welcome to the grade book for CS101 Introduction to Java Programming! Enter grade or -1 to quit: 97 Enter grade or -1 to quit: 88 Enter grade or -1 to quit: 72 Enter grade or -1 to quit: -1 Total of the 3 grades entered is 257 Class average is 85.67

Create a new GradeBook object

Pass the course’s name to the GradeBook constructor as a string

Call GradeBook’s determineClassAverage method

Page 27: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

4.10 Algoritmaların Tasarlanması: Örnek Çalışma 3 (İçiçe

Kontrol Yapıları)• İçiçe kontrol yapıları

Page 28: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

Gecenler ‘e 0 ilk değeri verKalanlar’ a 0 ilk değeri verOgrenci ‘ye 1 ilk değer ver 

Sayac 10 ‘a eşit yada küçük olduğu müddetçe Sınav sonucu gir

Eğer öğrenci geçmiş ise Gecenlere 1 ekle

else Kalanlara 1 ekle

  Sayacı 1 artır

Kaç kişi geçtiğini yazdırKaç kişi kaldığını yazdır

Eğer 8 kişiden fazla kişi geçmiş ise “Öğretim başarılı” yazdır

Fig 4.10 Pseudocode :sınav sonucu problemi için.

Page 29: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

2003 Prentice Hall, Inc. All rights reserved.

35

Outline

• Analysis.java

• (1 of 2)

1 // Fig. 4.12: Analysis.java

2 // Analysis of examination results.

3 import java.util.Scanner; // class uses class Scanner

4

5 public class Analysis

6 {

7 public void processExamResults

8 {

9 // create Scanner to obtain input from command window

10 Scanner input = new Scanner( System.in );

11

12 // initializing variables in declarations

13 int passes = 0; // number of passes

14 int failures = 0; // number of failures

15 int studentCounter = 1; // student counter

16 int result; // one exam result (obtains value from user)

17

18 // process 10 students using counter-controlled loop

19 while ( studentCounter <= 10 )

20 {

21 // prompt user for input and obtain value from user

22 System.out.print( "Enter result (1 = pass, 2 = fail): " );

23 result = input.nextInt();

24

Declare processExamResults’ local variables

while loop iterates as long as studentCounter <= 10

Page 30: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

2003 Prentice Hall, Inc. All rights reserved.

36

Outline

• Analysis.java

• (2 of 2)

25 // if...else nested in while

26 if ( result == 1 ) // if result 1,

27 passes = passes + 1; // increment passes;

28 else // else result is not 1, so

29 failures = failures + 1; // increment failures

30

31 // increment studentCounter so loop eventually terminates

32 studentCounter = studentCounter + 1;

33 } // end while

34

35 // termination phase; prepare and display results

36 System.out.printf( "Passed: %d\nFailed: %d\n", passes, failures );

37

38 // determine whether more than 8 students passed

39 if ( passes > 8 )

40 System.out.println( "Raise Tuition" );

41 } // end method processExamResults

42

43 } // end class Analysis

Determine whether this student passed or failed and increment the appropriate variable

Determine whether more than eight students passed the exam

Page 31: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

2003 Prentice Hall, Inc. All rights reserved.

37

Outline

• AnalysisTest.java

1 // Fig. 4.13: AnalysisTest.java 2 // Test program for class Analysis. 3 4 public class AnalysisTest 5 { 6 public static void main( String args[] ) 7 { 8 Analysis application = new Analysis(); // create Analysis object 9 application.processExamResults(); // call method to process results 10 } // end main 11 12 } // end class AnalysisTest

Enter result (1 = pass, 2 = fail): 1 Enter result (1 = pass, 2 = fail): 2 Enter result (1 = pass, 2 = fail): 1 Enter result (1 = pass, 2 = fail): 1 Enter result (1 = pass, 2 = fail): 1 Enter result (1 = pass, 2 = fail): 1 Enter result (1 = pass, 2 = fail): 1 Enter result (1 = pass, 2 = fail): 1 Enter result (1 = pass, 2 = fail): 1 Enter result (1 = pass, 2 = fail): 1 Passed: 9 Failed: 1 Raise Tuition Enter result (1 = pass, 2 = fail): 1 Enter result (1 = pass, 2 = fail): 2 Enter result (1 = pass, 2 = fail): 1 Enter result (1 = pass, 2 = fail): 2 Enter result (1 = pass, 2 = fail): 1 Enter result (1 = pass, 2 = fail): 2 Enter result (1 = pass, 2 = fail): 2 Enter result (1 = pass, 2 = fail): 1 Enter result (1 = pass, 2 = fail): 1 Enter result (1 = pass, 2 = fail): 1 Passed: 6 Failed: 4

Create an Analysis object

More than 8 students passed the exam

Page 32: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

4.11 Birleşik Atama Operatörleri

• Atama Operatörleri– Kısaltılmış atama operatörleri

– Herhangi bir atama işlemi• değişken = değişken operatör ifade;

– Şu şekilde de yazılabilir• değişken operatör = ifade;

– Örneğin toplam atama operatörü +=• c = c + 3

– Şu şekilde de yazılabilir• c += 3

Page 33: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

Assignment operator

Sample expression

Explanation Assigns

Assume: int c = 3, d = 5, e = 4, f = 6, g = 12;

+= c += 7 c = c + 7 10 to c -= d -= 4 d = d - 4 1 to d *= e *= 5 e = e * 5 20 to e /= f /= 3 f = f / 3 2 to f %= g %= 9 g = g % 9 3 to g Fig. 4.12 Arithmetic assignment operators.

Page 34: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

4.12 Artırma Azaltma Operatörleri

• Bir artırma (++)– Değer 1 artırır.

• Bir azaltma (--)– Değeri 1 azaltır

• Önceden artırma / önceden azaltma operatör• Sonradan artırma / sonradan-azaltma operatör

Page 35: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

Operator Called Sample expression

Explanation

++ preincrement ++a Increment a by 1, then use the new value of a in the expression in which a resides.

++ postincrement a++ Use the current value of a in the expression in which a resides, then increment a by 1.

-- predecrement --b Decrement b by 1, then use the new value of b in the expression in which b resides.

-- postdecrement b-- Use the current value of b in the expression in which b resides, then decrement b by 1.

Fig. 4.13 The increment and decrement operators.

Page 36: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

Increment.java

Line 13 postincrement

Line 21 preincrement

1 // Fig. 4.14: Increment.java2 // Preincrementing and postincrementing operators.3 4 public class Increment {5 6 public static void main( String args[] )7 {8 int c;9 10 // demonstrate postincrement11 c = 5; // assign 5 to c12 System.out.println( c ); // print 513 System.out.println( c++ ); // print 5 then postincrement14 System.out.println( c ); // print 6 15 16 System.out.println(); // skip a line17 18 // demonstrate preincrement19 c = 5; // assign 5 to c20 System.out.println( c ); // print 521 System.out.println( ++c ); // preincrement then print 622 System.out.println( c ); // print 6 23 24 } // end main25 26 } // end class Increment

556 

566

Line 13 postincrements c

Line 21 preincrements c

Page 37: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

Operators Associativity Type ++ -- right to left unary postfix ++ -- + - (type) right to left unary * / % left to right multiplicative + - left to right additive < <= > >= left to right relational == != left to right equality ?: right to left conditional = += -= *= /= %= right to left assignment Fig. 4.15 Bu zamana kadar öğrendiğimiz tüm operatörler.

Page 38: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

4.13 İlkel Veri Tipleri

• Java da tüm değişkenlerin mutlaka bir veri tipi olması lazımdır.

• Java ilkel veri tipleri

Page 39: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

Type Size in bits Values Standard boolean true or false

[Note: The representation of a boolean is specific to the Java Virtual Machine on each computer platform.]

char 16 '\u0000' to '\uFFFF' (0 to 65535)

(ISO Unicode character set)

byte 8 –128 to +127 (–27 to 27 – 1)

short 16 –32,768 to +32,767 (–215 to 215 – 1)

int 32 –2,147,483,648 to +2,147,483,647 (–231 to 231 – 1)

long 64 –9,223,372,036,854,775,808 to +9,223,372,036,854,775,807 (–263 to 263 – 1)

float 32 Negative range: –3.4028234663852886E+38 to –1.40129846432481707e–45 Positive range: 1.40129846432481707e–45 to 3.4028234663852886E+38

(IEEE 754 floating point)

double 64 Negative range: –1.7976931348623157E+308 to –4.94065645841246544e–324 Positive range: 4.94065645841246544e–324 to 1.7976931348623157E+308

(IEEE 754 floating point)

Fig. 4.16 The J ava primitive types.

Page 40: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

2003 Prentice Hall, Inc. All rights reserved.

48

Diyalog kutusunda metin yazdırmak

• Pencereler ve diyalog kutuları– Bir çok Java uygulaması program çıktısını göstermek

için bunu kullanır.

– JOptionPane sınıfı hazır diyalog kutularını kullanır.

Page 41: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

2003 Prentice Hall, Inc. All rights reserved.

49

Outline

• Dialog1.java

1 // Fig. 3.17: Dialog1.java

2 // Printing multiple lines in dialog box.

3 import javax.swing.JOptionPane; // import class JOptionPane

4

5 public class Dialog1

6 {

7 public static void main( String args[] )

8 {

9 // display a dialog with the message

10 JOptionPane.showMessageDialog( null, "Welcome\nto\nJava" );

11 } // end main

12 } // end class Dialog1

Show a message dialog with text

Import class JOptionPane

Page 42: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

2003 Prentice Hall, Inc. All rights reserved.

50

Diyalog kutusunda metin yazdırmak

• javax.swing paketi– Grafik arabirim oluşturmaya yardım edecek sınıflar

içerir.(GUIs)

– JOptionPane sınıfını içerir• Bir mesaj yazdırmak için showMessageDialog

statik metodu kullanılır.

Page 43: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

2003 Prentice Hall, Inc. All rights reserved.

51

Diyalog kutusundan metin girişi yapmak

• Diyalog girişi– Kullanıcın bilgi girmesini sağlar

– JOptionPane sınıfının showInputDialog metodu kullanılarak oluşturulur

Page 44: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

2003 Prentice Hall, Inc. All rights reserved.

52

Outline

• NameDialog.java

1 // Fig. 3.18: NameDialog.java

2 // Basic input with a dialog box.

3 import javax.swing.JOptionPane;

4

5 public class NameDialog

6 {

7 public static void main( String args[] )

8 {

9 // prompt user to enter name

10 String name =

11 JOptionPane.showInputDialog( "What is your name?" );

12

13 // create the message

14 String message =

15 String.format( "Welcome, %s, to Java Programming!", name );

16

17 // display the message to welcome the user by name

18 JOptionPane.showMessageDialog( null, message );

19 } // end main

20 } // end class NameDialog

Show input dialog

Format a String to output to user

Page 45: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

2003 Prentice Hall, Inc. All rights reserved.

53

Creating Simple Drawings

Page 46: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

2003 Prentice Hall, Inc. All rights reserved.

54

4.14  (Optional) GUI and Graphics Case Study: Creating Simple Drawings

• Java grafiklerinde koordinat sistemi– X- koordinatı ve Y-koordinatı olarak ifade edilir.

• Yatay veya dikey koordinatlar da denilir.

• x-ekseni ve y-ekseninde gösterilir.

– Koordinat birimi pixeldir.

• Graphics class java.awt paketindedir.– Metin ve şekil çizme metodlarını içerir.

• JPanel class javax.swing paketindedir.– Çizim yapma alanı oluşturur.

Page 47: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

2003 Prentice Hall, Inc. All rights reserved.

55

4.14 Basit çizimler oluşturmak

• JPanel class– Her JPanel in bir paintComponent metodu vardır

• paintComponent sistem JPanel i görüntüleyeceği zaman çağrılır.

– getWidth ve getHeight metodları• JPanel’in genişlik ve uzunluğunu gönderir.

– drawLine metodu• İlk 2 argümanın gösterdiği noktadan, diğer 2 argümanın

gösterdiği noktaya bir çizgi çizer.

Page 48: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

2003 Prentice Hall, Inc. All rights reserved.

56Fig. 4.18 | Java koordinat

sistemi

Page 49: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

2003 Prentice Hall, Inc. All rights reserved.

57

4.14  Basit çizimler oluşturmak

• Kalıtım– extends anahtar kelimesi kullanılır

– Altsınıf(subclass) üstsınıf(superclass)tan türer• Altsınıf üstsınıfın tanımladığı data ve metodlara sahip olur ve

kullanabilir.

Page 50: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

2003 Prentice Hall, Inc. All rights reserved.

58

Outline

• DrawPanel.java

1 // Fig. 4.19: DrawPanel.java

2 // Draws two crossing lines on a panel.

3 import java.awt.Graphics;

4 import javax.swing.JPanel;

5

6 public class DrawPanel extends JPanel

7 {

8 // draws an X from the corners of the panel

9 public void paintComponent( Graphics g )

10 {

11 // call paintComponent to ensure the panel displays correctly

12 super.paintComponent( g );

13

14 int width = getWidth(); // total width

15 int height = getHeight(); // total height

16

17 // draw a line from the upper-left to the lower-right

18 g.drawLine( 0, 0, width, height );

19

20 // draw a line from the lower-left to the upper-right

21 g.drawLine( 0, height, width, 0 );

22 } // end method paintComponent

23 } // end class DrawPanel

Import the java.awt.Graphics and the javax.swing.JPanel classes

The DrawPanel class extends the JPanel class

Declare the paintComponent method

Draw the two lines

Retrieve the JPanel’s width and height

Page 51: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

2003 Prentice Hall, Inc. All rights reserved.

59

4.14  Basit çizimler oluşturmak

• JFrame class javax.swing paketindedir.– Programcının bir pencere oluşturmasını sağlar.– setDefaultCloseOperation metod

• JFrame.EXIT_ON_CLOSE arguman olarak uygulamaya gönderir ve pencere kapandığında programın bitmesini sağlar.

– add metod• JPanel i JFrame e ekler.

– setSize metod• JFrame in genişliğini ve uzunluğunu belirler. Birinci parametre

genişlik, ikinci parametre uzunluktur.

Page 52: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

2003 Prentice Hall, Inc. All rights reserved.

60

Outline

• DrawPanelTest.java

1 // Fig. 4.20: DrawPanelTest.java 2 // Application to display a DrawPanel. 3 import javax.swing.JFrame; 4 5 public class DrawPanelTest 6 { 7 public static void main( String args[] ) 8 { 9 // create a panel that contains our drawing 10 DrawPanel panel = new DrawPanel(); 11 12 // create a new frame to hold the panel 13 JFrame application = new JFrame(); 14 15 // set the frame to exit when it is closed 16 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 17 18 application.add( panel ); // add the panel to the frame 19 application.setSize( 250, 250 ); // set the size of the frame 20 application.setVisible( true ); // make the frame visible 21 } // end main 22 } // end class DrawPanelTest

Import the JFrame class from the javax.swing class

Create DrawPanel and JFrame objects

Set the application to terminate when the user closes the window

Add the DrawPanel to the JFrame

Set the size of and display the JFrame

Page 53: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

2003 Prentice Hall, Inc. All rights reserved.

61Fig. 4.21 | Lines fanning from a corner.

Page 54: Bölüm  4 –  Kontrol İfadeleri :1 .kısım

2003 Prentice Hall, Inc. All rights reserved.

62Fig. 4.22 | Line art with loops

and drawLine.