0% found this document useful (0 votes)
1 views6 pages

Coding Project

The document is a Java program for an ArrayList Review Game that tests users' knowledge of ArrayLists through a series of questions. Players can answer multiple-choice questions and receive feedback on their answers, with the option to play multiple rounds. The game tracks the player's score and provides explanations for correct and incorrect answers.

Uploaded by

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

Coding Project

The document is a Java program for an ArrayList Review Game that tests users' knowledge of ArrayLists through a series of questions. Players can answer multiple-choice questions and receive feedback on their answers, with the option to play multiple rounds. The game tracks the player's score and provides explanations for correct and incorrect answers.

Uploaded by

aashmitchandra
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import [Link].

ArrayList;

import [Link];

import [Link];

public class ArrayListReviewGame {

static Scanner input = new Scanner([Link]);

static int score = 0;

public static void main(String[] args) {

[Link]("=== Welcome to THE ArrayList Review game!


===");

[Link]("Test your knowledge of ArrayLists. See if you’re


goated or not. Choose a letter (a, b, c, d) for your answer.\n");

do {

score = 0; // Reset score for each round

playGame();

[Link]("\nDo you want to play again BUM? (y/n): ");

} while ([Link]().trim().equalsIgnoreCase("y"));

[Link]("\nThanks for playing BUM! Goodbye!");

[Link]();

public static void playGame() {

ArrayList<Question> questions = new ArrayList<>();


addQuestions(questions);

[Link](questions);

for (Question q : questions) {

boolean correct = askQuestion(q);

if (!correct) {

[Link]("IDIOT! You got that one wrong.");

[Link]("\n=== Game Over! ===");

[Link]("Your score: " + score + " out of " +


[Link]());

[Link]("You got " + (score * 100 / [Link]()) + "%


correct!");

public static boolean askQuestion(Question q) {

[Link]([Link]);

for (String option : [Link]) {

[Link](option);

String userAnswer = [Link]().trim().toLowerCase();

if ([Link]() == 1 && "abcd".contains(userAnswer)) {

if ([Link](0) == [Link]) {
[Link]("✅ Correct! " + [Link]);

score++;

return true;

} else {

[Link]("❌ Incorrect. " + [Link]);

return false;

} else {

[Link]("Please choose a valid answer (a, b, c, or d).");

return false;

public static void addQuestions(ArrayList<Question> questions) {

[Link](new Question(

"1. How do you declare an ArrayList of Strings?",

new String[]{

"a) ArrayList<String> list = new ArrayList<>();",

"b) ArrayList list = new ArrayList<String>();",

"c) String list = new ArrayList<>();",

"d) new ArrayList<String> list = ArrayList();"

},

'a',

"The correct syntax is: ArrayList<String> list = new ArrayList<>();"

));

[Link](new Question(
"2. Which method adds an element to an ArrayList?",

new String[]{

"a) append()",

"b) add()",

"c) insert()",

"d) put()"

},

'b',

"The correct method to add an element is add()."

));

[Link](new Question(

"3. What happens if you try to access an invalid index in an


ArrayList?",

new String[]{

"a) It returns null",

"b) It adds a default value",

"c) It throws an IndexOutOfBoundsException",

"d) It returns -1"

},

'c',

"An invalid index throws an IndexOutOfBoundsException."

));

[Link](new Question(

"4. What does [Link]() return?",

new String[]{
"a) The last index of the list",

"b) The number of elements in the list",

"c) The maximum capacity",

"d) 0 if the list is empty"

},

'b',

"[Link]() returns the number of elements in the list."

));

[Link](new Question(

"5. What is the best loop for going through each element in an
ArrayList?",

new String[]{

"a) while loop",

"b) do-while loop",

"c) enhanced for loop",

"d) switch loop"

},

'c',

"The enhanced for loop is the best choice for iterating through an
ArrayList."

));

class Question {

String prompt;
String[] options;

char correctAnswer;

String explanation;

public Question(String prompt, String[] options, char correctAnswer, String


explanation) {

[Link] = prompt;

[Link] = options;

[Link] = correctAnswer;

[Link] = explanation;

You might also like