CodeBlanks Logo Icon CodeBlanks

Proje sonu - Quiz uygulaması

PHP kursu · Orta seviye · Ders 46/115

Tüm parçaları birleştirip çalışan quiz uygulamasını tamamlarsın.

Tüm parçaları birleştir

Son derste tüm parçaları birleştireceksin. Question sınıfı (property'ler, yapıcı metot, isCorrect), Quiz sınıfı (sorular, score) hazır. Şimdi quiz uygulamasını özetleyen bir metot ekleyip sonucu yazdıracaksın.

Quiz sınıfına summary(array $answers): string metodu eklersin. Bu metot $this->score($answers) ile doğru sayısını, count($this->questions) ile toplamı alır ve "2/2" gibi bir özet döndürür. Böylece kullanıcı skorunu tek satırda görür.

summary metodu ile özet çıktı

Özet metodunu çağırmak için echo $quiz->summary([0, 1]); yazarsın. Bu, "2/2" gibi bir çıktı verir: ilk sayı doğru cevap, ikinci sayı toplam soru.

Metot içinde $correct = $this->score($answers); ile skoru alır, $total = count($this->questions); ile toplamı bulur ve return $correct . "/" . $total; ile birleştirip döndürürsün. Bu, var olan score metodunu yeniden kullanmanın güzel bir örneğidir: tekrar yazmak yerine çağırırsın.

Uygulama summary metodunu ekle ve quiz'i çalıştır

Quiz sınıfına public function summary(array $answers): string metodunu ekle. İçinde $correct = $this->score($answers); ile skoru al, $total = count($this->questions); ile toplam soru sayısını bul ve return $correct . "/" . $total; ile özet döndür. Son satırı echo $quiz->score([0, 1]); yerine echo $quiz->summary([0, 1]); olarak değiştir.

Örnek çözümü göster
string { $correct = $this->score($answers); $total = count($this->questions); return $correct . "/" . $total; } ekle. Son echo satırını echo $quiz->summary([0, 1]); olarak değiştir.
Örnek çözüm:
<?php
// Tüm parçaları birleştir: Question + Quiz + özet
class Question {
    public string $text = "";
    public array $options = [];
    public int $answerIndex = 0;

    public function __construct(string $text, array $options, int $answerIndex) {
        $this->text = $text;
        $this->options = $options;
        $this->answerIndex = $answerIndex;
    }

    public function isCorrect(int $choice): bool {
        return $choice === $this->answerIndex;
    }
}

class Quiz {
    public array $questions;

    public function __construct(array $questions = []) {
        $this->questions = $questions;
    }

    public function score(array $answers): int {
        $correct = 0;
        foreach ($this->questions as $i => $q) {
            if ($q->isCorrect($answers[$i] ?? -1)) {
                $correct++;
            }
        }
        return $correct;
    }

    public function summary(array $answers): string {
        $correct = $this->score($answers);
        $total = count($this->questions);
        return $correct . "/" . $total;
    }
}

$quiz = new Quiz([
    new Question("PHP nedir?", ["Betik dili", "Veritabanı", "Tarayıcı"], 0),
    new Question("Hangisi döngü?", ["if", "for", "echo"], 1),
]);
echo $quiz->summary([0, 1]);

Proje tamamlandı

Tebrikler! Quiz uygulaması projesini tamamladın. Beş ders boyunca bir Question sınıfı tasarladın: property, yapıcı metot ve isCorrect metodu ile. Sonra Quiz sınıfı ile foreach döngüsü kullanıp score ve summary metotlarını ekledin.

Artık bu sınıfları başka projelerde kullanabilirsin: soru bankası, çevrim içi test aracı veya eğitici oyun. OOP ile veriyi ve davranışı tek pakette tutmak, kodun tekrar kullanılmasını ve bakımını kolaylaştırır.

Kendini test et

  1. Quiz uygulamasını çalıştırmak için ne gerekir?

    • Question ve Quiz sınıflarını tanımla, örnek oluştur ve metotları çağır
    • Yalnızca echo komutu yaz
    • Veritabanı bağlantısı kur
    • HTML formu olmadan quiz çalışmaz
    Cevabı göster

    Question ve Quiz sınıflarını tanımla, örnek oluştur ve metotları çağır — Doğru. Sınıfları tanımlayıp örnek oluşturmak ve metotları çağırmak yeterli; quiz konsolda çalışır.

  2. Quiz uygulaması kavramlarını eşleştir

    Question
    Soru sınıfı
    Quiz
    Soruları tutan sınıf
    score()
    Doğru sayısını verir
    summary()
    Skoru özetler
  3. summary return ifadesini tamamla

    public function summary(array $answers): string {
        $correct = $this->score($answers);
        $total = count($this->questions);
        return ____;
    }
    Cevabı göster
    public function summary(array $answers): string {
        $correct = $this->score($answers);
        $total = count($this->questions);
        return $correct . "/" . $total;
    }

Orta seviye bölümündeki tüm dersler