Write an application that creates a quiz, which contains at least five questions
ID: 3687419 • Letter: W
Question
Write an application that creates a quiz, which contains at least five questions about a hobby, popular music, astronomy, or any other personal interest. Each question should be a multiple choice question with at least three options. When a user answers a question correctly, display a congratulatory message. If a user responds to a question incorrectly, display an appropriate message as well as a correct answer. At the end of the quiz display the number of correct and incorrect answers and the percentage of correct answers. Save the file as Quiz.java.
Also, I need to accomplish this without arrays.
The questions/answers can be simple for sake of a working code. I will come up with the actual questions when a working code is available.
Explanation / Answer
import java.util.InputMismatchException;
import java.util.Scanner;
public class Quiz
{
public static void main(String[] args)
{
System.out.println("IMAGE quiz. ");
int correctAnswersNumber = 0;
int incorrectAnswersNumber = 0;
Question questions[] = initializeQuestions();
Scanner userInput;
int option;
for (int i = 0; i < questions.length; i++)
{
System.out.println(questions[i].getQuestionText());
questions[i].printAnswers();
userInput = new Scanner(System.in);
try
{
option = userInput.nextInt();
}
catch (InputMismatchException e)
{
option = -99;
}
if (option == questions[i].getCorrectAnswer())
{
System.out.println("Congratulations! That was the correct answer. ");
correctAnswersNumber++;
}
else
{
System.out.println("Wrong answer. The correct one is: " + questions[i].getCorrectAnswer() + " ");
incorrectAnswersNumber++;
}
}
System.out.println("Number of correct answers: " + correctAnswersNumber);
System.out.println("Number of incorrect answers: " + incorrectAnswersNumber);
System.out.println("Percentage of correct answers: " + ((100 * correctAnswersNumber) / (correctAnswersNumber + incorrectAnswersNumber)) + "%");
}
private static Question[] initializeQuestions()
{
Question questions[] = new Question[6];
questions[0] = new Question("1. In photography, filters are used to:");
questions[0].addAnswer(new Answer("keep dirt and grime away from the film surface "));
questions[0].addAnswer(new Answer("modify light.", true));
questions[0].addAnswer(new Answer("keep used photons from collecting inside the camera."));
questions[0].addAnswer(new Answer("clean the air entering the camera through the PC terminal."));
questions[1] = new Question("2. Depth of field refers to:");
questions[1].addAnswer(new Answer("the thickness of the film."));
questions[1].addAnswer(new Answer("the distance from the rear nodal point of the lens and the film plane."));
questions[1].addAnswer(new Answer("landscape photographs made with a wide angle lens."));
questions[1].addAnswer(new Answer("the areas behind and in front of the point of focus that are also acceptably sharp.", true));
questions[2] = new Question("3. An over-exposed slide will:");
questions[2].addAnswer(new Answer("be too light.", true));
questions[2].addAnswer(new Answer("be too dark."));
questions[2].addAnswer(new Answer("be orange colored."));
questions[2].addAnswer(new Answer("have a pronounced blue cast."));
questions[3] = new Question("4. A zoom lens:");
questions[3].addAnswer(new Answer("is faster than a regular lens."));
questions[3].addAnswer(new Answer("is usually sharper than a non-zoom lens."));
questions[3].addAnswer(new Answer("has a variable focal length.", true));
questions[3].addAnswer(new Answer("should never be used with a filter."));
questions[4] = new Question("5. Compared to a film with an ISO of 400, a film with an ISO of 100:");
questions[4].addAnswer(new Answer("requires less exposure."));
questions[4].addAnswer(new Answer("is better for action subjects."));
questions[4].addAnswer(new Answer("will produce grainer prints."));
questions[4].addAnswer(new Answer("requires more exposure.", true));
questions[5] = new Question("6. Which of the following are especially suitable for use in low-light photography?");
questions[5].addAnswer(new Answer("slow film (low ISO rating)"));
questions[5].addAnswer(new Answer("fast shutter speed."));
questions[5].addAnswer(new Answer("wide aperture.", true));
questions[5].addAnswer(new Answer("polarizing filter."));
questions[5].addAnswer(new Answer("zoom lens."));
return questions;
}
}
class Question
{
private String questionText;
private Answer[] answers = null;
public Question(String questionText)
{
this.questionText = questionText;
}
public void printAnswers()
{
if (answers != null && answers.length > 0)
{
for (int i = 0; i < answers.length; i++) {
System.out.println(" [" + i + "] " + answers[i].getText());
}
}
}
public void addAnswer(Answer answer)
{
if (answers == null)
{
answers = new Answer[1];
answers[0] = answer;
}
else
{
Answer[] temp = answers;
answers = new Answer[temp.length + 1];
for (int i = 0; i < temp.length; i++)
{
answers[i] = temp[i];
}
answers[temp.length] = answer;
}
}
public int getCorrectAnswer()
{
for (int i = 0; i < answers.length; i++)
{
if (answers[i].isCorrectAnswer())
{
return i;
}
}
return -1;
}
public String getQuestionText()
{
return questionText;
}
}
class Answer
{
private String text;
private boolean correctAnswer;
public Answer(String answerText)
{
this.text = answerText;
this.correctAnswer = false;
}
public Answer(String answerText, boolean correctAnswer)
{
this.text = answerText;
this.correctAnswer = correctAnswer;
}
public String getText()
{
return text;
}
public boolean isCorrectAnswer()
{
return correctAnswer;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.