In: Computer Science
WRITE THIS JAVA CODE IN PSEUDOCODE!! import java.util.Scanner; public class License { public static void main(String[] args) { char correctAnswers[] = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'}; char userAnswers[] = new char[correctAnswers.length]; Scanner scanner = new Scanner(System.in); for (int i = 0; i < userAnswers.length; i++) { String answer = ""; System.out.printf("Question #%d. Enter your answer( A, B, C or D): ", i + 1); do { answer = scanner.nextLine(); if (!(answer.equals("A") || answer.equals("B") || answer.equals("C") || answer.equals("D"))) { System.out.print("You didnt enter it A, B, C or D. Enter again: "); } } while (!(answer.equals("A") || answer.equals("B") || answer.equals("C") || answer.equals("D"))); userAnswers[i] = answer.charAt(0); } int correctlyAnswered = getCorrectAnswerCount(correctAnswers, userAnswers); System.out.printf("\nYou answered %d questions correctly.\n", correctlyAnswered); System.out.printf("You answered %d questions incorrectly.\n\n", userAnswers.length - correctlyAnswered); if (correctlyAnswered != userAnswers.length) { displayIncorrectlyAnsweredQuestions(correctAnswers, userAnswers); } if(correctlyAnswered>=15) System.out.println("You have cleared the exam. Congratulations."); else System.out.println("You have failed the exam. Sorry!"); } private static void displayIncorrectlyAnsweredQuestions(char[] correctAnswers, char[] userAnswers) { System.out.print("Questions Wrongly Answered: "); for (int i = 0; i < correctAnswers.length; i++) { if (correctAnswers[i] != userAnswers[i]) System.out.print((i + 1) + " "); } System.out.println(); } private static int getCorrectAnswerCount(char[] correctAnswers, char[] userAnswers) { int count = 0; for (int i = 0; i < correctAnswers.length; i++) { if (correctAnswers[i] == userAnswers[i]) count += 1; } return count; } }
PSEUDOCODE WITHOUT COMMENTS
START
Initialize char array correctAnswers = {'B', 'D', 'A',
'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C',
'C', 'B', 'D', 'A'}
Declare char array userAnswers with length of
correctAnswers
FOR i From 0 to length of userAnswers -1
Declare answer as String
DO
Read
answer
IF answer not
equals A, B, C, or D
Display Error Message
END IF
WHILE (answer not equals A, B, C,
or D)
Set userAnswers[i] = answer
END FOR
Declare correctlyAnswered as int
correctlyAnswered =
getCorrectAnswerCount(correctAnswers, userAnswers)
Display correctlyAnswered
Display length of userAnswers -
correctlyAnswered
IF correctlyAnswered not equals length of
userAnswers
displayIncorrectlyAnsweredQuestions(correctAnswers,
userAnswers)
END IF
IF correctlyAnswered greater than or equals 15
Display Passed
ELSE
Display Failed
END IF
FUNCTION
displayIncorrectlyAnsweredQuestions(correctAnswers,
userAnswers)
FOR i From 0 to length of
correctAnswers - 1
IF
correctAnswers[i] not equals userAnswers[i]
Display i + 1
END IF
END FOR
END displayIncorrectlyAnsweredQuestions
FUNCTION getCorrectAnswerCount(correctAnswers,
userAnswers)
Declare count as int and initialize
as 0
FOR i From 0 to length of
correctAnswers - 1
IF
correctAnswers[i] equals userAnswers[i]
Increment count
END IF
END FOR
Return count
END getCorrectAnswerCount
END
SCREEN SHOT
PSEUDOCODE WITH COMMENTS
START
//declaring and initializing array
correctAnswers
Initialize char array correctAnswers = {'B', 'D', 'A',
'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C',
'C', 'B', 'D', 'A'}
//declaring array userAnswers
Declare char array userAnswers with length of
correctAnswers
//loop to read user answers
FOR i From 0 to length of userAnswers -1
//variable to read answer
Declare answer as String
//loop to read answer
DO
//read
answer
Read
answer
//if invalid
answer
IF answer not
equals A, B, C, or D
Display Error Message
END IF
//repeat until valid answer
entered
WHILE (answer not equals A, B, C,
or D)
//storing answer in array
userAnswers
Set userAnswers[i] = answer
END FOR
//variable to store number of correct answers
Declare correctlyAnswered as int
//calling getCorrectAnswerCount
correctlyAnswered =
getCorrectAnswerCount(correctAnswers, userAnswers)
//displaying number of correct answers
Display correctlyAnswered
//displaying number of wrong answers
Display length of userAnswers -
correctlyAnswered
//if there is wrong answers
IF correctlyAnswered not equals length of
userAnswers
//calling
displayIncorrectlyAnsweredQuestions
displayIncorrectlyAnsweredQuestions(correctAnswers,
userAnswers)
END IF
//if correct answers greater than or equals 15
IF correctlyAnswered greater than or equals 15
Display Passed
//if correct answers less than 15
ELSE
Display Failed
END IF
//function displayIncorrectlyAnsweredQuestions
FUNCTION
displayIncorrectlyAnsweredQuestions(correctAnswers,
userAnswers)
//loop to display wrong
answers
FOR i From 0 to length of
correctAnswers - 1
//if correct
answer not equals user answer
IF
correctAnswers[i] not equals userAnswers[i]
//display question number of wrong answer
Display i + 1
END IF
END FOR
END displayIncorrectlyAnsweredQuestions
//function getCorrectAnswerCount
FUNCTION getCorrectAnswerCount(correctAnswers,
userAnswers)
//variable to store number of
correct answers
Declare count as int and initialize
as 0
//loop to count number of correct
answers
FOR i From 0 to length of
correctAnswers - 1
//if correct
answer equals user answer
IF
correctAnswers[i] equals userAnswers[i]
//increments count
Increment count
END IF
END FOR
//returns count
Return count
END getCorrectAnswerCount
END
SCREEN SHOT