Sunum yükleniyor. Lütfen bekleyiniz

Sunum yükleniyor. Lütfen bekleyiniz

BLP 4210 PLATFORM BAĞIMSIZ PROGRAMLAMA

Benzer bir sunumlar


... konulu sunumlar: "BLP 4210 PLATFORM BAĞIMSIZ PROGRAMLAMA"— Sunum transkripti:

1 BLP 4210 PLATFORM BAĞIMSIZ PROGRAMLAMA
Dr. Aslı Ergün 1

2 Erişim Türleri Private: Sınıfa özel değişkenlerdir.
Public: Herkese açık olan değişkenlerdir. Protected: Alt sınıflara türeyenlere ve aynı pakette olanlara açık olan değişkenlerdir.

3 Main.java package com.company; import com.company.TestA.kisi1; public class Main { public static void main(String[] args) { kisi1 ali = new kisi1(); System.out.println(ali.var2); personel veli = new personel(); System.out.println(veli.var2); System.out.println(veli.var3); guvenlik g1 = new guvenlik(); System.out.println(g1.var2); System.out.println(g1.var3); } }

4 kisi1.java package com.company.TestA; public class kisi1 { private String var1; public String var2; protected String var3; public kisi1() { var1 = "kisi1 private değişken"; var2 = "kisi1 public değişken"; var3 ="kisi1 protected değişken"; } }

5 personel.java package com.company; public class personel { private String var1; public String var2; protected String var3; public personel() { var1 = "personel private değişken"; var2 = "personel public değişken"; var3 ="personel protected değişken"; } }

6 guvenlik.java package com.company; public class guvenlik extends personel{ private String var1; public String var2; protected String var3; public guvenlik() { var1 = "guvenlik private değişken"; var2 = "guvenlik public değişken"; var3 = "guvenlik protected değişken"; } }

7 Kapsülleme Sınıfın bazı özellik ve metodlarına dışarıdan erişim sınıfın güvenliği açısından tehlikeli olabilir. Bir sınıf içeriğinin, onun üyelerini kullananlar tarafından bilinmesine gerek duymadan sadece metodun verdiği hizmetin gösterilmesi işlemidir. Kapsülleme ile bir sınıf, kendi iç bütünlüğünü gizleyebilir ve koruyabilir.

8 public class Kisi { private String ad; private String soyad; private int yas; public String getAd() { return ad; } public void setAd(String ad) { this.ad = ad; public String getSoyad() { return soyad; public void setSoyad(String soyad) { this.soyad = soyad; public int getYas() { return yas; public void setYas(int yas) { if(yas>0) this.yas= yas;

9 Kurucu Metod eklenmesi(IntelliJ)

10

11

12

13 Kapsülleme(Getters, Setters)

14

15

16 public class EncapsTest{ public static void main(String args[]){
class Ogrenci{ private int tc; private String adsoy; private int yas; //Getter and Setter methods public int getTC(){ return tc; } public String getAdsoy(){ return adsoy; public int getYas(){ return yas; public void setYas(int newValue){ yas = newValue; public void setAdsoy(String newValue){ adsoy = newValue; public void setTC(int newValue){ tc = newValue; } } public class EncapsTest{ public static void main(String args[]){ Ogrenci obj = new Ogrenci(); obj.setAdsoy("Ali"); obj.setYas(32); obj.settc( ); System.out.println("Adı: " + obj.getAdsoy()); System.out.println("TC: " + obj.getTC()); System.out.println("Yas: " + obj.getYas()); }

17 Try-catch-finally try { //hesaplanmak istenen ifade } catch
//Bir hata türü tespit edilince verilmesi gereken mesaj //başka Bir hata türü tespit edilince verilmesi gereken mesaj finally //her durumda çalıştırılacak olan kod parçası

18 Hata Ayıklama(Exception)
System tanımlı Kullanıcı tanımlı

19 System Exceptions Checked Exception (pembe renkli)Java’da kod geliştirme esnasında try-catch-finally bloğu içerisinde yakalanan ve daha sonra işlem yapılabilen, istenildiği takdirde bypass edilerek programın çalışmasına belkide kullanıcının hiç haberi olmadan devam edilmesini sağlayan exception çeşitleridir. Unchecked exception (mavi renkli) tipinde Runtime Exception Java’da yine kod geliştirme esnasında try-catch-finally bloğuyla yakalanabilir ve checked exception tipinde olduğu gibi istenildiği gibi işlem devam ettirilebilir. Fakat kodu geliştirme esnasında IDE yazılımcıyı uyarmaz.

20 Built-in Exceptions Arithmetic Exception It is thrown when an exceptional condition has occurred in an arithmetic operation. ArrayIndexOutOfBoundException It is thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array. ClassNotFoundException This Exception is raised when we try to access a class whose definition is not found FileNotFoundException This Exception is raised when a file is not accessible or does not open. IOException It is thrown when an input-output operation failed or interrupted InterruptedException It is thrown when a thread is waiting , sleeping , or doing some processing , and it is interrupted. NoSuchFieldException It is thrown when a class does not contain the field (or variable) specified NoSuchMethodException It is thrown when accessing a method which is not found. NullPointerException This exception is raised when referring to the members of a null object. Null represents nothing NumberFormatException This exception is raised when a method could not convert a string into a numeric format. RuntimeException This represents any exception which occurs during runtime. StringIndexOutOfBoundsException It is thrown by String class methods to indicate that an index is either negative than the size of the string

21 Arithmetic exception // Java program to demonstrate ArithmeticException class ArithmeticException_Demo { public static void main(String args[]) try { int a = 30, b = 0; int c = a/b; // cannot divide by zero System.out.println ("Result = " + c); } catch(ArithmeticException e) { System.out.println ("Can't divide a number by 0");

22 StringIndexOutOfBound Exception
// Java program to demonstrate StringIndexOutOfBoundsException class StringIndexOutOfBound_Demo { public static void main(String args[]) try { String a = "This is like chipping "; // length is 22 char c = a.charAt(24); // accessing 25th element System.out.println(c); } catch(StringIndexOutOfBoundsException e) { System.out.println("StringIndexOutOfBoundsException");

23 File Not Found Exception
public void ExceptionTryExample() { FileInputStream inputStream = null; try { File file = new File("./tmp.txt"); inputStream = new FileInputStream(file); // use the inputStream to read a file } catch (FileNotFoundException e) { log.error(e); } finally { if (inputStream != null) { inputStream.close(); } catch (IOException e) { }

24 Kullanıcı Tanımlı Exception
class MyException extends Exception { //store account information private static int accno[] = {1001, 1002, 1003, 1004}; private static String name[] = {"Nish", "Shubh", "Sush", "Abhi", "Akash"}; private static double bal[] = { , , , , }; // default constructor MyException() { } // parametrized constructor MyException(String str) { super(str); } // write main() public static void main(String[] args) try { // display the heading for the table System.out.println("ACCNO" + "\t" + "CUSTOMER" + "\t" + "BALANCE");

25 Kullanıcı Tanımlı Exception
// display the actual account information for (int i = 0; i < 5 ; i++) { System.out.println(accno[i] + "\t" + name[i] + "\t" + bal[i]); // display own exception if balance < 1000 if (bal[i] < 1000) MyException me = new MyException("Balance is less than 1000"); throw me; } } //end of try catch (MyException e) { e.printStackTrace();

26 Text Dosya Okuma InputStreamReader, FileReader, BufferedReader ciasses to read text files. Reader is the abstract class for reading character streams. It implements the following fundamental methods: read(): reads a single character. read(char[]): reads an array of characters. skip(long): skips some characters. close(): closes the stream.

27 FileReader package net.codejava.io; import java.io.FileReader;
import java.io.FileReader; import java.io.IOException; /**  * This program demonstrates how to read characters from a text file.  *  */ public class TextFileReadingExample1 {     public static void main(String[] args) {         try {             FileReader reader = new FileReader("MyFile.txt");             int character;             while ((character = reader.read()) != -1) {                 System.out.print((char) character);             }             reader.close();         } catch (IOException e) {             e.printStackTrace();         }     } }

28 FileInputStream-1 24.04.2019 package net.codejava.io;
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; /**  * This program demonstrates how to read characters from a text file using  * a specified charset.  *  */ public class TextFileReadingExample2 {     public static void main(String[] args) {         try {             FileInputStream inputStream = new FileInputStream("MyFile.txt");             InputStreamReader reader = new InputStreamReader(inputStream, "UTF-16");             int character;             while ((character = reader.read()) != -1) {                 System.out.print((char) character);             }             reader.close();         } catch (IOException e) {             e.printStackTrace();         }     } }

29 FileInputStream-2 import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.charset.StandardCharsets; public class FileInputStreamEx3 { public static void main(String[] args) throws FileNotFoundException, IOException { String fileName = "/home/janbodnar/tmp/bigfile.txt"; try (FileInputStream fis = new FileInputStream(fileName)) { int i = 0; do { byte[] buf = new byte[1024]; i = fis.read(buf); String value = new String(buf, StandardCharsets.UTF_8); System.out.print(value); } while (i != -1); }

30 BufferedReader 24.04.2019 package net.codejava.io;
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; /**  * This program demonstrates how to read characters from a text file  * using a BufferedReader for efficiency.  *  */ public class TextFileReadingExample3 {     public static void main(String[] args) {         try {             FileReader reader = new FileReader("MyFile.txt");             BufferedReader bufferedReader = new BufferedReader(reader);             String line;             while ((line = bufferedReader.readLine()) != null) {                 System.out.println(line);             }             reader.close();         } catch (IOException e) {             e.printStackTrace();         }     } }

31 Text Dosya Yazma Writer is the abstract class for writing character streams. It implements the following fundamental methods: write(int): writes a single character. write(char[]): writes an array of characters. write(String): writes a string. close(): closes the stream.

32 FileWriter 24.04.2019 package net.codejava.io;
import java.io.FileWriter; import java.io.IOException; /**  * This program demonstrates how to write characters to a text file.  *  */ public class TextFileWritingExample1 {     public static void main(String[] args) {         try {             FileWriter writer = new FileWriter("MyFile.txt", true);             writer.write("Hello World");             writer.write("\r\n");   // write new line             writer.write("Good Bye!");             writer.close();         } catch (IOException e) {             e.printStackTrace();         }     } }

33 Wrapped BufferedWriter
package net.codejava.io; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; /**  * This program demonstrates how to write characters to a text file  * using a BufferedReader for efficiency.  *  */ public class TextFileWritingExample2 {     public static void main(String[] args) {         try {             FileWriter writer = new FileWriter("MyFile.txt", true);             BufferedWriter bufferedWriter = new BufferedWriter(writer);             bufferedWriter.write("Hello World");             bufferedWriter.newLine();             bufferedWriter.write("See You Again!");             bufferedWriter.close();         } catch (IOException e) {             e.printStackTrace();         }     } }

34 OutputStreamWriter ve BufferedWriter
package net.codejava.io; import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; /**  * This program demonstrates how to write characters to a text file using  * a specified charset.  *  */ public class TextFileWritingExample3 {     public static void main(String[] args) {         try {             FileOutputStream outputStream = new FileOutputStream("MyFile.txt");             OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-16");             BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);                           bufferedWriter.write("Xin chào");             bufferedWriter.newLine();             bufferedWriter.write("Hẹn gặp lại!");             bufferedWriter.close();         } catch (IOException e) {             e.printStackTrace();         }               } }

35 Arayüzler(Interface)
Java dilinde interface adını taşıyan, tasarım şablonlarında ve modellemede kullanılan sınıflar tanımlamak mümkündür Arayüz kullanarak, bir sınıfın neler yapacağını belirlerken, onları nasıl yapacağını gizleyebiliriz. Interface içerisinde sadece fonksiyon, prosedür tanimlamasi yapabiliriz. Interface içerisinde degisken tanimlamasi yapilmaz!!!

36

37 public interface IKisiBilgileri { public String getAdSoyad(); publicvoid setAdSoyad(String adSoyad); // public String getEPosta(); publicvoid setEPosta(String ePosta); public String getTelefon(); publicvoid setTelefon(String telefon); }

38 Public class Ogrenci implements IKisiBilgileri { private String adSoyad; private String ePosta; private String telefon; public String getAdSoyad() { returnthis.adSoyad; } publicvoid setAdSoyad(String adSoyad) { this.adSoyad = adSoyad; public String getEPosta() { returnthis.ePosta; publicvoid setEPosta(String ePosta) { this.ePosta = ePosta; public String getTelefon() { returnthis.telefon; publicvoid setTelefon(String telefon) { this.telefon = telefon;

39 Çoklu Kalıtım Diamond Problemi: Bir sınıf iki sınıfın alt sınıfı ise ve aynı adlı metod bulunursa hata verir. Bunun yerine çoklu kalıtımda interface kullanılır.

40 Sınıf ve alt Interface önceliği vardır
Aynı adlı metodların öncelik sırasına göre override etmesi beklenir.

41 Multiple Inheritance Bu ornekte ClassA'daki setMessage metodu çalışır.

42 Arayüz ve Abstract arasında fark
Abstract sınıflar: Kod içerisinde “new” anahtar sözcüğü ile nesne haline getirilemezler. Bir sınıf sadece bir abstract sınıfı türetilebilir( inherit alabilir). Abstract sınıfda method ve değişkenler tanımlanabilir. Interface sınıflar: Bir sınıf birden fazla interface implemente edebilir. Interface içerisine sadece boş method’lar tanımlanabilir.


"BLP 4210 PLATFORM BAĞIMSIZ PROGRAMLAMA" indir ppt

Benzer bir sunumlar


Google Reklamları