Create a multiple-choice test – Java Example

Hello everybody, welcome to the Java
example section. In this section, I am going to set up a multiple-choice test
game. My idea is like this. I would create a question set with some answers in a
… for each question. And then I can display the questions randomly, together
with the choices of each question randomly. Then I would put the questions
together with the answers into a game instance. And then when I play the game
instance, I can record the number of correct answers for that particular
game in … game instance. Now let us try to handle the Question class first, which is
going to store each question The idea is like this. I’m going to
use the ArrayList data structure, so I have to import it first And after
this import, I’m able to create my ArrayLists Now for my Question class, I have
a an instance variable called question, which is simply the question in a
certain string format For each question, I would provide a number of choices to
the player. And the choices will be stored in an ArrayList of string references For each question, I would have an answer
stored in a Question object Now I set up the Question constructor We can see that a Question object contains the question itself, together with the
choices and the answer. Here, my choices will be stored in an array of string
references. And I can put the content in the array of string references to the
ArrayList that will be shown later Okay, I put the value coming from the formal
parameter of the Question constructor to the instance variable of a particular
Question object Okay, the choice ArrayList is created
here by using the “new” keyword. For now, the ArrayList has nothing inside Now I’m
going to put the contents of … from the choices array to the ArrayList Okay, this dot choices refers to the
ArrayList called choices, and add method is the method of an ArrayList. And what
I’m going to add? I’m going to add the element in my String array into the
choice ArrayList. I’m going to do so one by one. So after this for-loop, the
ArrayList whose reference variable is called choices will be having a number
of String references coming from the formal parameter of the Question
constructor Here I’m going to play some trick. I would like to show each
questions in each game differently. So I can shuffle the questions before showing
up. How can I do that? I’m going to shuffle the contents in the ArrayList
called choices. And when I use the Collection class, I also need to import
the Collection class. And for Eclipse, this import action can be done for me
automatically So the shuffle method comes from the
Collections class. And in in the Java data structure, an ArrayList is a
type of collections. So we can use the shuffle method in an ArrayList, okay Now I simply put the formal parameter
value to the instance variable of the Question object. This finishes the
constructor Now I set up the getQuestion method, which returns the question value
of the instance variable I can also set up a getter method for the choices Okay,
I can also do the same thing for the answer instance variable So this
completes the Question class which stores a particular question Now I can go to the Game class So for
my Game class, I need to store the questions in the form of ArrayLists. So I
need to import the ArrayList class first Okay Now I create the instance variables
for a Game object first I will have a list of Question references, and the
ArrayList is having a name called questionSet Now I set up the constructor for my
Game instance When I call the constructor, I would simply set up the cons … the
ArrayList for my questionSet by using the “new” keyword. Now I can put the
questions into the ArrayList of Question references, okay. How can I do
that? I can do something like this. We can refer to the Question constructor. We
need the question itself, together with the choices in the form of an array, plus
the answer in the form of a String So the question can be like this. I use a
String reference variable called “q” to represent a certain question string Now
I set up an array of String references called
“a”, which means answer Okay, this array has 4 String contents Okay I set up a
particular question in this way. I use the add method pertaining to the ArrayList
class. And here I simply add a new Question object reference to the
elements of my ArrayList whose reference variable is called questionSet. “a” … sorry here here “q” is simply the the question
itself. “a” is simply the array that stores a number of String objects. And
here “East” is the answer which refers to the final formal parameter of the Question
constructor, okay So this completes one question. If I want to set up another
question for my game, I can do similar things like this Now I can reuse the reference variable
that I used previously to store another string. So in this case, the
original String object that has this content will be eliminated by the
garbage collection mechanism of Java Here I use the “new” keyword to create another
String object, which will be put in a reference variable called “a” which
is set up initially. So we have to notice that the “new” keyword is necessary in
this case because this kind of assignment is only applicable for the
initial assignment of a String … array reference variable. If we want to
reuse the original variable, we have to use the “new String array” approach. That
means we create another String array and this String array will be stored in
the variable called “a” So the idea is like this. We have to use “new String
array” to help me reuse the original variable. The reason is like this The array
constants of this format can only be used in … during the initialization
process. If I’m not going to initialize the variable, I need to use the “new
String array” as the beginning of the array constant, okay Okay, I set up another question into my
questionSet ArrayList. And of course, I have to use the “new” keyword here because
I really set up another Question object Okay, I can do the similar things for my
third question Okay, I have to use the “new String array” declaration at the
beginning of my array constant when I set the “a” reference variable Okay, I
have three questions here. And if I want to provide the random order when I show
the questions to the player, I need to shuffle the question set Okay, I can do so by using the shuffle
method coming from the Collections class And I can also tell the shuffle method
to do the shuffling randomly for each time. So in this case, I can put a random
number coming from the Random class. So in this situation, I have to import the Random
class After that, I can create a certain random number
the … using the “new” math keyword. In this case, for each shuff … shuffling, the order
will be different. This is the random number seed. So when I use a
“Random” keyword every time I call the shuffle method, the random number seed
will be different, which means that the question set will be shuffled
differently for each time Well, I can start again by creating a start method I need the user to input the choice number, so I have to create a Scanner
object whose reference variable is called scan. And the actual parameter of
the Scanner construct is System dot in That means I’m going to receive the
import … input from the u … keyboard Of course, I have to import the Scanner
class before I can create a Scanner object Okay. After creating the Scanner object,
I can do the real thing about the game I need to remember the number of correct
answers for each play, so I have to set up a variable called numCorrect, which
is initialized to be 0 Then I can show the questions from the question set. How
can I do this? I can use a for loop I have an index called question I need to
loop over all the questions until the index is equal to the questionSet dot
size. This size method is simply the method that returns the size of an ArrayList Okay here I simply want to print out the
contents of the question from the Question object, okay. So this get method
is simply the get method pertaining to an ArrayList
called questionSet. And when I have got the index of the question, I can put it
into the get method which returns the Question object. And when I get the
Question object, I can use the getQuestion method to get the string that
stores the question itself. So this is the flow of the program. I get a certain
Question object and then from this object, I can use the getQuestion method
to return the string, okay Okay, then the number of choices is simply obtained by
the size of the getChoices ArrayList, okay, which would simply return the number
of choices evaluated during the constructor of the Question class, okay Now I’m going to show the choices after
setting the number of choices available for a particular question The choice index
is simply called choice And for all the choices available for the
particular question, I can do a for loop that prints out the choices Okay, this statement
is quite long. Since our ArrayList would start with 0, if I want to show the
index to start with 1, I have to add 1 to the choice index, okay. And this getChoices method would be simply equal to this one. And when I get the choices ArrayList,
I’m going to get the particular choice from the ArrayList that stores all
the choices for a particular question Now after showing the question and the
choices, I can tell the player to in … input a certain number, which will be read
by the nextInt method of the Scanner object whose reference variable is
called scan. And the value obtained will be stored in the integer primitive
called playerAnswer Now I can set up the choice set for a particular question So now I can save the choices from a
particular question into the variable called choiceSet When I have this
variable, I can get the correct answer from my particular question object that
is playing Okay. The correct answer will be simply
the value coming from the getAnswer method, which is shown in the Question
class here, okay. I simply get the instance variable for a particular
Question object Well, I need to compare the correct answer index with the player
answer index. So how can I obtain the correct answer index? I can do something
like this I simply obtain the choice set first. And
then from that choice set, I’m going to find out the index of the string that
represents the correct answer So I have the coming from the indexOf method For a better name, I would call it
correctAnswerIndex If the player answer is equal to the correct answer
index by one, I will increase the number of correct answers by one Remember the
playerIndex can be one, two, three, or four if you have four choices. But the
correct answer index coming from an indexOf method can start with zero. So I
have to shift it higher by one in order to compare it correctly with the player
answer After receiving the user input, I can
close the Scanner object. And after the closing action, I can simply print out the
number of correct answers obtained by the user Okay, this completes the Game class. let
me save it. And let me go to driver program. The driver program can be quite
simple I simply create a Game object By using
the constructor of the Game class, which is this one, then I can tell it to play by using the start method. So let me save
it, and run it Yes, the first question is like this. Where
is the capital of Japan? I can say three Enter. And the second question is this one.
Who invented the telephone. My answer is choice three. And the third question is this one.
Where does the Sun rise? The answer should be number four.
Yeah, I will get all three answers correct. When I play again, I will have
different order of the question together with the different order of the choices
for each question Let me see Yes, and the first question is the third question in my initial play, and the
choice becomes three instead of four originally. The second question was
originally the first question in my first play, and the choice becomes one
instead of three initially. And finally the third question would be the
remaining questions stored in the questionSet ArrayList. Let me choose
four. So I would get all three answers correct. And I can ensure that the order
of question together with the order of choices in each question can be
different. Why? Because I use the Collections dot shuffle method for
the choices together with the shuffle method for my question, okay. My code is
intuitive and straightforward, but they may not be the best or the most
efficient approach to the problem. If you can suggest any idea to improve my code,
please do so on the comment section below the video. If you like this video,
please give me a like and please subscribe to my channel. Thank you for

Leave a Reply

Your email address will not be published. Required fields are marked *