Programming Assignment Unit 1
Student Name: Musozi Marvin
Instructor: Rajesh Soni
import [Link];
public class QuizGame {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int correctAnswers = 0;
[Link]("Welcome to the Java Quiz Game!");
[Link]("------------------------------");
// Question 1
[Link]("Question 1: What is the capital of France?");
[Link]("A. London B. Berlin C. Paris D. Rome");
[Link]("Your answer: ");
String answer1 = [Link]().toUpperCase();
switch (answer1) {
case "C":
correctAnswers++;
break;
default:
break;
}
// Question 2
1
[Link]("\nQuestion 2: Which data type is used to create a variable that
should store text?");
[Link]("A. int B. String C. char D. float");
[Link]("Your answer: ");
String answer2 = [Link]().toUpperCase();
if ([Link]("B")) {
correctAnswers++;
}
// Question 3
[Link]("\nQuestion 3: Who developed Java?");
[Link]("A. Dennis Ritchie B. James Gosling C. Bjarne Stroustrup D.
Guido van Rossum");
[Link]("Your answer: ");
String answer3 = [Link]().toUpperCase();
switch (answer3) {
case "B":
correctAnswers++;
break;
default:
break;
}
// Question 4
[Link]("\nQuestion 4: What does JVM stand for?");
[Link]("A. Java Very Machine B. Java Variable Method");
[Link]("C. Java Virtual Machine D. Java Verified Mode");
[Link]("Your answer: ");
String answer4 = [Link]().toUpperCase();
if ([Link]("C")) {
2
correctAnswers++;
}
// Question 5
[Link]("\nQuestion 5: Which keyword is used to define a class in Java?");
[Link]("A. define B. create C. new D. class");
[Link]("Your answer: ");
String answer5 = [Link]().toUpperCase();
switch (answer5) {
case "D":
correctAnswers++;
break;
default:
break;
}
// Final Score Calculation
double scorePercent = (correctAnswers / 5.0) * 100;
[Link]("\nYou got " + correctAnswers + " out of 5 correct!");
[Link]("Final Score: " + scorePercent + "%");
[Link]();
}
}
3
Explanation of Key Features:
Input Validation: Accepts A/B/C/D and converts to uppercase to reduce errors.
Logic & Computation: Tracks correct answers and computes the percentage.
Use of if and switch: Demonstrates both control structures.
Clear Output: Questions are clearly formatted for readability.
Comments: Comments are included inline to explain each part of the code (add more
if required for grading).