0% found this document useful (0 votes)
11 views

VB Practical Assignment 3

Uploaded by

Nathanel Rupondo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

VB Practical Assignment 3

Uploaded by

Nathanel Rupondo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

COURSE: VISUAL PROGRAMMING CONCEPTS

CORSE CODE: ICS 1201


GROUP TWO COMPUTER SCIENCE
PRACTICAL ASSIGNMENT
TAYIYA T H230264Z
MUGABE TINOTENDA M H230066N
PERCEIVE MATEKEDE H230240N
TAPIWA TENDAYI H230452P
JOWA TAKUNDANASHE H230137R
MASHORE DEEJAY H230315H
CHABIKWA DENNEL H230305T
JSON FILE SELECTION

ANSWERING THE QUESTION CORRECT

ANSWERING THE QUESTION THREE TIMES TILL IT TERMINATES THE PROGRAM


FORM 1 CODE

public partial class Form1 : Form


{
public static string Data;
public int i = 1;

public Form1()
{
InitializeComponent();

private void button1_Click(object sender, EventArgs e)


{

if (i == 1)
{
i = i + 1;
using (var openFileDialog = new OpenFileDialog())
{
openFileDialog.Filter = "JSON files (*.json)|*.json|All files
(*.*)|*.*";
openFileDialog.RestoreDirectory = true;

if (openFileDialog.ShowDialog() == DialogResult.OK)
{
var filePath = openFileDialog.FileName;
var fileContent = File.ReadAllText(filePath);
Data = fileContent;
var fileName = Path.GetFileName(filePath);
textBox1.Text = fileName;
}
}

} else
{

this.Hide();
Form2 form2 = new Form2();
form2.Show();
}

private void button2_Click(object sender, EventArgs e)


{
this.Hide();
Form2 form2 = new Form2();
form2.Show();
}
}
}
FORM 2 CODE

public partial class Form2 : Form


{
private List<Question> questions;
private Question currentQuestion;
private int wrongAnswersCount;
public Form2()
{
InitializeComponent();
string json = Form1.Data;
var quiz = JsonConvert.DeserializeObject<Quiz>(json);

questions = quiz.Questions;
DisplayRandomQuestion();
this.button1.Click += new System.EventHandler(this.buttonOption_Click);
this.button2.Click += new System.EventHandler(this.buttonOption_Click);
}

private void Form2_Load(object sender, EventArgs e)


{

}
private void DisplayRandomQuestion()
{
Random random = new Random();
int index = random.Next(questions.Count);

currentQuestion = questions[index];
richTextBox1.Text = currentQuestion.QuestionText;
button1.Text = currentQuestion.Options[0];
button2.Text = currentQuestion.Options[1];
}

private void buttonOption_Click(object sender, EventArgs e)


{

Button clickedButton = (Button)sender;


string selectedOption = clickedButton.Text;

if (selectedOption == currentQuestion.Answer)
{
MessageBox.Show(currentQuestion.CorrectFeedback, "Correct!",
MessageBoxButtons.OK);
wrongAnswersCount = 0;
}
else
{
MessageBox.Show(currentQuestion.WrongFeedback, "Wrong!",
MessageBoxButtons.OK);
wrongAnswersCount++;
}

if (wrongAnswersCount >= 3)
{
MessageBox.Show("You have answered incorrectly 3 times in a row. The
program will now terminate.", "Failure", MessageBoxButtons.OK);
Application.Exit();
}
else
{
DisplayRandomQuestion();
}

private void button1_Click(object sender, EventArgs e)


{

}
}
}
public class Quiz
{
[JsonProperty("questions")]
public List<Question> Questions { get; set; }
}

public class Question


{
[JsonProperty("question")]
public string QuestionText { get; set; }

[JsonProperty("options")]
public List<string> Options { get; set; }

[JsonProperty("answer")]
public string Answer { get; set; }

[JsonProperty("correct_feadback")]
public string CorrectFeedback { get; set; }

[JsonProperty("wrong_feadback")]
public string WrongFeedback { get; set; }

FORM 1 CODE EXPLANITION

 public static string Data;: This variable stores the content of the selected JSON file as a string.
 button1_Click: This event handler is triggered when the first button (button1) is clicked.

Opening the File:

 It checks if i is 1 (presumably to handle the first click and avoid repetitive file selection).
 If i is 1, it creates an OpenFileDialog object.
 This dialog allows the user to browse and select a JSON file.
 If the user selects a file and clicks "OK" (DialogResult.OK), the code proceeds.

Reading the File:

 The code retrieves the selected file path (filePath).


 It reads the entire content of the chosen file using File.ReadAllText(filePath) and stores it in
the fileContent variable.
 This fileContent is then assigned to the static variable Data, making it accessible to Form2.
 The filename is extracted using Path.GetFileName(filePath) and displayed in the textBox1.
 button2_Click: This event handler is triggered when the second button (button2) is clicked
(likely intended for a similar purpose as button1_Click). However, it directly hides Form1 and
shows Form2 without any file selection logic.
Overall:

This code allows the user to select a JSON file containing quiz data using button1. The selected file's
content is then stored in the Data variable and potentially used by Form2 for the quiz functionality.
There's no code that modifies or terminates the original JSON file itself.

FORM 2 CODE EXPLANITION

Classes:
 Form2: This class represents the second form of the application which displays the
quiz questions and handles user interaction.
Fields:
 questions: A list to store Question objects representing the quiz questions.
 currentQuestion: A Question object to hold the currently displayed question.
 wrongAnswersCount: An integer to track the number of consecutive incorrect
answers.
Constructor (Form2()):
 Initializes the form components using InitializeComponent().
 Retrieves the JSON data string from Form1.Data (assuming Form1 provides the data).
 Deserializes the JSON string into a Quiz object using JsonConvert.DeserializeObject.
 Extracts the questions list from the Quiz object and stores it in questions.
 Calls DisplayRandomQuestion() to show the first question.
 Attaches the buttonOption_Click event handler to both button1 and button2 for
handling option clicks.
Quiz: This class represents the overall quiz structure.
Field:
 Questions: A list of Question objects representing the individual questions in the
quiz.
Question: This class represents a single question within the quiz.
Fields:
 QuestionText: The text of the question.
 Options: A list of strings containing the answer options.
 Answer: The correct answer string.
 CorrectFeedback: Feedback message displayed for a correct answer.
 WrongFeedback: Feedback message displayed for an incorrect answer.
Methods:
Form2.DisplayRandomQuestion():
 Creates a Random object.
 Generates a random index within the questions list's range.
 Sets currentQuestion to the question object at the generated index.
 Updates the richTextBox1 text with the current question's text.
 Sets the text of button1 and button2 to the first two options from
currentQuestion.Options.
 Form2.buttonOption_Click(object sender, EventArgs e): (Event handler for both
option buttons)
 Casts the sender object (the clicked button) to a Button.
 Extracts the selected option text from the clicked button's text property.
 Checks if the selected option matches the currentQuestion.Answer.
Correct Answer:
 Displays a message box with the correctFeedback using MessageBox.Show.
 Resets the wrongAnswersCount to 0.
Incorrect Answer:
 Displays a message box with the wrongFeedback using MessageBox.Show.
 Increments the wrongAnswersCount.
 Checks if wrongAnswersCount is greater than or equal to 3:
 If yes, displays a message box indicating termination and exits the
application using Application.Exit().
 If no, calls DisplayRandomQuestion() to show a new question.
Overall Functionality:
The code creates a quiz application based on data provided in a JSON format. Users are
presented with randomly chosen questions from the list and select an answer from the
provided options. Feedback is provided based on the chosen answer. The application
terminates after three consecutive incorrect answers.

You might also like