C#
Putting it together
Combine the Quiz class with a console menu.
Integration
In the final lesson we combine all the pieces: the Question model, the Quiz class, AddQuestion, Run and the score tracking. In Main we will add two questions and play the quiz.
To add two questions we call AddQuestion twice. The Run method takes an array of answers; this array must have two elements. We capture the returned score and print it with Console.WriteLine(score);. All pieces work together.
Two questions, two answers
To add two questions we call AddQuestion twice in a row. The Run method processes these two questions in order; the first answer maps to the first question, the second to the second.
The answer array length must match the question count. new[] { "Ankara", "Izmir" } sends two answers. They are compared with the expected answers; each correct one increments the score. Wrong answers do not affect the score.
Exercise Integrate the quiz
In Main, create a Quiz, add two questions and call quiz.Run(new[] { "Ankara", "Izmir" }). Print the returned score with Console.WriteLine(score);. Questions: Text = "Capital?", Answer = "Ankara" and Text = "Largest city?", Answer = "Istanbul".
Show example solution
`var quiz = new Quiz(); quiz.AddQuestion(new Question { Text = "Capital?", Answer = "Ankara" }); quiz.AddQuestion(new Question { Text = "Largest city?", Answer = "Istanbul" }); int score = quiz.Run(new[] { "Ankara", "Izmir" }); Console.WriteLine(score);`
Example solution:
using System;
using System.Collections.Generic;
class Question
{
public string Text;
public string Answer;
}
class Quiz
{
private List<Question> questions = new List<Question>();
public void AddQuestion(Question q)
{
questions.Add(q);
}
public int Count()
{
return questions.Count;
}
public int Run(string[] answers)
{
int score = 0;
for (int i = 0; i < questions.Count; i++)
{
Console.WriteLine(questions[i].Text);
if (answers[i] == questions[i].Answer)
{
Console.WriteLine("Correct");
score++;
}
else
{
Console.WriteLine("Wrong");
}
}
return score;
}
}
class Program
{
static void Main()
{
var quiz = new Quiz();
quiz.AddQuestion(new Question { Text = "Capital?", Answer = "Ankara" });
quiz.AddQuestion(new Question { Text = "Largest city?", Answer = "Istanbul" });
int score = quiz.Run(new[] { "Ankara", "Izmir" });
Console.WriteLine(score);
}
}
Quiz complete
Congratulations! At the end of five lessons you have a working Quiz application. You defined the Question model, built the Quiz class, added questions, played the quiz and computed the score. All pieces work together.
In the next groups we will extend this structure: defining contracts with interfaces, sharing common behavior with abstract classes and handling different question types in the same collection with polymorphism.
Test yourself
-
What happens in the integration lesson?
- All pieces are combined into a working app.
- A new feature is added.
- Classes are deleted.
- Only the printing is fixed.
Show answer
All pieces are combined into a working app. — Correct. Integration brings all pieces into a working whole.
-
Match: integration parts
- two questions
- AddQuestion twice
- Run call
- two answers sent
- score
- number correct
- WriteLine
- shows the score
-
Fill in the blank: print the score
int score = quiz.Run(answers);
Console.WriteLine(____);
Show answer
int score = quiz.Run(answers);
Console.WriteLine(score);
Next lesson: Defining interfaces