Answer to Question #327692 in Java | JSP | JSF for Dreaper

Question #327692

The Program shall:

  • contain an arrow of 10 multiple choice questions with three (3) choices each
  • requires the user to choose among A, B, or C;

Note: Cases are ignored. Lowercase letters are acceptable (a, b, c).

Create a try-catch structure to handle three (3) exceptions. These are when the user inputs the following:

  • an invalid letter (not A, B, or C)
  • a number or any special character
  • blank (no answer)

Prompt the user that he can answer again if any of the three (3) exceptions is thrown

Display the score.


1
Expert's answer
2022-04-12T13:26:33-0400
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Questionnaire {

    public static void main(String[] args) {
        Question question1 = new Question("Question 1 correct answer B");
        question1.addAnswer('A', "Answer A", false);
        question1.addAnswer('B', "Answer B", true);
        question1.addAnswer('C', "Answer C", false);

        Question question2 = new Question("Question 2 correct answer B");
        question2.addAnswer('A', "Answer A", false);
        question2.addAnswer('B', "Answer B", true);
        question2.addAnswer('C', "Answer C", false);

        Question question3 = new Question("Question 3 correct answer B");
        question3.addAnswer('A', "Answer A", false);
        question3.addAnswer('B', "Answer B", true);
        question3.addAnswer('C', "Answer C", false);

        Question question4 = new Question("Question 4 correct answer B");
        question4.addAnswer('A', "Answer A", false);
        question4.addAnswer('B', "Answer B", true);
        question4.addAnswer('C', "Answer C", false);

        Question question5 = new Question("Question 5 correct answer B");
        question5.addAnswer('A', "Answer A", false);
        question5.addAnswer('B', "Answer B", true);
        question5.addAnswer('C', "Answer C", false);

        Question question6 = new Question("Question 6 correct answer B");
        question6.addAnswer('A', "Answer A", false);
        question6.addAnswer('B', "Answer B", true);
        question6.addAnswer('C', "Answer C", false);

        Question question7 = new Question("Question 7 correct answer B");
        question7.addAnswer('A', "Answer A", false);
        question7.addAnswer('B', "Answer B", true);
        question7.addAnswer('C', "Answer C", false);

        Question question8 = new Question("Question 8 correct answer B");
        question8.addAnswer('A', "Answer A", false);
        question8.addAnswer('B', "Answer B", true);
        question8.addAnswer('C', "Answer C", false);

        Question question9 = new Question("Question 9 correct answer B");
        question9.addAnswer('A', "Answer A", false);
        question9.addAnswer('B', "Answer B", true);
        question9.addAnswer('C', "Answer C", false);

        Question question10 = new Question("Question 10 correct answer B");
        question10.addAnswer('A', "Answer A", false);
        question10.addAnswer('B', "Answer B", true);
        question10.addAnswer('C', "Answer C", false);

        Question[] questions = {
                question1,
                question2,
                question3,
                question4,
                question5,
                question6,
                question7,
                question8,
                question9,
                question10
        };

        int score = 0;
        int i = 0;
        Scanner scanner = new Scanner(System.in);
        while (i < 10) {
            System.out.print(i+1 + ". ");
            System.out.println(questions[i]);
            System.out.print("Enter your answer -> ");

            String userAnswer = scanner.nextLine();
            try {
                validateBlank(userAnswer);
                validateNumberOrAnySpecialCharacter(userAnswer);
                validateIsNotABC(userAnswer);

                char answer = userAnswer.toUpperCase().charAt(0);
                if (questions[i].isCorrectAnswer(answer)) {
                    score++;
                }
                i++;
            } catch (UserAnswerIsBlankException | UserAnswerIsNumberOrAnySpecialCharacterException | UserAnswerIsNotABCCharacterException e) {
                System.out.println("Answer is not allowed. \n");
            }
        }

        System.out.println("Your score is : " + score);
    }

    private static void validateBlank(String userAnswer) {
        if (userAnswer.isBlank()) {
            throw new UserAnswerIsBlankException();
        }
    }

    private static void validateNumberOrAnySpecialCharacter(String userAnswer) {
        Character character = userAnswer.charAt(0);
        if ((character < 'A' || character > 'Z') && (character < 'a' || character > 'z')) {
            throw new UserAnswerIsNumberOrAnySpecialCharacterException();
        }
    }

    private static void validateIsNotABC(String userAnswer) {
        Character character = userAnswer.toUpperCase().charAt(0);
        if ((character < 'A' || character > 'C')) {
            throw new UserAnswerIsNotABCCharacterException();
        }
    }


}

class Question {
    private String question;
    private Map<Character, Answer> answers = new HashMap<>();

    public Question(String question) {
        this.question = question;
    }

    public void addAnswer(Character character, String answer, boolean isCorrect) {
        answers.put(character, new Answer(answer, isCorrect));
    }

    public boolean isCorrectAnswer(Character character) {
        Answer answer = answers.get(character);
        return answer.isCorrect();
    }

    @Override
    public String toString() {
        return question
                + " \n A:" + answers.get('A')
                + " \n B:" + answers.get('B')
                + " \n C:" + answers.get('C');
    }
}

class Answer {
    private String answer;
    private boolean isCorrect;

    public Answer(String answer, boolean isCorrect) {
        this.answer = answer;
        this.isCorrect = isCorrect;
    }

    public boolean isCorrect() {
        return isCorrect;
    }

    @Override
    public String toString() {
        return answer;
    }
}

class UserAnswerIsBlankException extends RuntimeException {
}

class UserAnswerIsNotABCCharacterException extends RuntimeException {
}

class UserAnswerIsNumberOrAnySpecialCharacterException extends RuntimeException {
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS