Created
March 20, 2016 21:11
-
-
Save AnEmortalKid/07770e4e0e8454a2858a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ok, so it seems that you have a set of Questions and you have to display all of them. Here's the steps we'll take: | |
1. We are going to create a Question object, which will hold all the data of our questions. It will hold the question, the correct answer, and the options you want to give people. | |
2. Then we are going to create a method that can take this question, and display it. Alright, so let's start. | |
Our Question object is going to hold three fields: | |
1. A String question, which is the text of the question. | |
2. A String correctAnswer, which is the text of the correct answer | |
3. A String[] answerOptions, which are the options we'll show as possible answers, with the correct answer inside it. | |
4. We'll have a Constructor for our Question which takes these three things. And we'll have some getters , to retrieve those values. | |
My Question class looks like this: https://gist.github.com/AnEmortalKid/d7484cbc1129f9f06c2e | |
Now the second step, is to write a method that will take our Question, and show it in a JOptionPane. I am going to write mine in a QuizDisplayer class which will have a main method, and the displayQuestion method: | |
1. displayQuestion will take in a Question as a parameter. | |
2. It will use the Question's question text as the text for the JOptionPane. | |
3. It will use the question's answerOptions as the possible buttons. | |
4. It will also print out the answer the user selected, and whether it is correct or not by comparing it with the Question's correct | |
Our display question method looks something like this: https://gist.github.com/AnEmortalKid/e8a06d3dc750c71fceb2 , and to run it, we would call it in the QuizzDisplayer class, which looks like this: https://gist.github.com/AnEmortalKid/8f69b45948c60faabcaa | |
Now, let's improve it. Instead of displaying if the answer is correct or not, it will just tell us that it was correct or not. We'll do this by changing the return type from void, to boolean. Our method should now look like this: https://gist.github.com/AnEmortalKid/dd7712292d0da5953565 | |
Now, let's combine it together by creating two questions. The same way we created the first. We'll do this in the main method: | |
1. Create a question 2, like you did the first question. | |
2. We're going to call displayQuestion and store the value it returns in a boolean called firstCorrect. | |
3. We'll print it out after the first question, so we know if it was correct or not. | |
4. We'll do the same thing with the second question, to display if it was correct or not as well. | |
Our modified QuizzDisplayer class looks like this: https://gist.github.com/AnEmortalKid/87a2004ff943ecbcaeff | |
Ok, so now that you know how to do 2 questions, you should be able to follow the pattern and do all 10. However, we're going to create an Array of Question to hold all our questions and then we'll use a loop to display them, and add a score if they're correct or not. | |
1: Create a new array of Question[], you said you have 10 so you'll make one with 10. For my purpsoes I'll make one with 3 to show you a sample. | |
2: Step 2 we're going to use a for loop to act on each Question. We'll use an enhanced for loop (see more: https://blogs.oracle.com/CoreJavaTechTips/entry/using_enhanced_for_loops_with) | |
3. We'll check after each question if the answer was correct or not, if it was, we'll add a +1 to a score we keep. | |
4. At the end, we'll display how many they got right out of the number of questions we had. | |
The final sample looks like this: https://gist.github.com/AnEmortalKid/317dd4ae8613e946bafc | |
Hopefully you were able to follow along! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment