Putting it together
- C# course
- Intermediate
- Lesson 46/116
- 6 min
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.
Practice 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".
Starter code
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()
{
// Add two questions, play and print the score
}
}Sample 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);
}
}Expected output
Başkent? Doğru En büyük şehir? Yanlış 1
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.
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What happens in the integration lesson?
- Classes are deleted.
- A new feature is added.
- All pieces are combined into a working app. Correct answer
- Only the printing is fixed.
Explanation: Integration brings all pieces into a working whole.
2 Match: integration parts
| Term | Meaning |
|---|---|
two questions |
AddQuestion twice |
Run call |
two answers sent |
score |
number correct |
WriteLine |
shows the score |
3 Fill in the blank: print the score
int score = quiz.Run(answers);
Console.WriteLine(____);
int score = quiz.Run(answers);
Console.WriteLine(score);
Explanation: `Console.WriteLine(score);` prints the score.