Assignment #4: Superman Quiz Summary: Create the Superman Quiz program. The solu
ID: 3849154 • Letter: A
Question
Assignment #4: Superman Quiz
Summary: Create the Superman Quiz program.
The solution file should be named SupermanQuiz_ExerciseE.rb.
Use the instructions on page 178 of the textbook.
Run your program until it works and the output looks nice.
178 CHAPTER 5 Working with Loops 19. True/False) The for in loop processes collections of data, such as arrays or a range of values 20. Which of the following methods belongs to the Kernel module and executes forever unless you find a way to terminate its execution. a. loop b. step C. each d. None of the above Reinforcement Exercises The following exercises are designed to further your understanding of Ruby programming by challenging you to make improvements to the chapter's game project, the Superman Movie Trivia Quiz 1. Currently, the Superman Movie Trivia Quiz consists of five questions. Make the quiz more challenging by increasing its size to 10 questions. To implement this change, copy the last of the five statements that call on the Quiz object's disp q method to pose a quiz question. Add five copies of the statement immediately after the copied statement's question (along with the corresponding answers. You will also have to adjust the way the method calculates the number of questions missed. To do so, modify the determine grade method so that six or more questions must be correctly answered to pass the quiz. 2. Give players the opportunity to take the quiz again if they fail. To implement this change, add the appropriate statements in a loop within the script's Main Script Logic section (added to the program file in Step 10). Prompt the player to take the quiz again if fewer than six questions are answered correctly If the player not to retake the quiz, terminate the execution of the loop and allow the quiz to end. 3. Modify the quiz so that if the player decides not to take the quiz, the game still displays the text displayed by the display credits method. To implement this change, replace the statements at the end of the program file that invite the player to play again with a statement that executes the Quiz object's display credits method.Explanation / Answer
SupermanQuiz_ExerciseE.rb
====================================
class Screen
def cls
puts (" " * 25)
puts ""
end
def pause
STDIN.gets
end
end
class Quiz
def display_greeting
Console_Screen.cls
print " Welcome to the Superman Movie Trivia Quiz!" +
" Press Enter to " +
"continue."
Console_Screen.pause
end
def display_instructions
Console_Screen.cls
puts "INSTRUCTIONS: "
puts "You will be oresented with some multiple choice questions"
puts "To answer a question type the corresponding letter"
puts "Then press the Enter key"
puts "Your grade will be displayed at the end of the test"
puts "Good luck! "
print "Press Enter to continue."
Console_Screen.pause
end
def disp_q(question, q_A, q_B, q_C, q_D, answer)
loop do
Console_Screen.cls
puts question + " "
puts q_A
puts q_B
puts q_C
puts q_D
print " Type the letter representing your answer: "
reply = STDIN.gets
reply.chop!
if answer == reply then
$noRight += 1
end
if reply == "a" or reply == "b" or reply == "c" or reply == "d" then
break
end
end
end
def determine_grade
Console_Screen.cls
if $noRight >= 3 then
print "You correctly answered " + $noRight.to_s + " questions(s). "
puts "You have passed this trivia quiz! "
puts "You earned a rank of:Good Citizen" if $noRight == 3
puts "You earned a rank of:Side Kick" if $noRight == 4
puts "You earned a rank of:Superhero" if $noRight == 5
print " Press Enter to continue."
else
print "You missed " + (5 - $noRight).to_s + " questions. "
puts "You have failed the trivia quiz."
puts "retake the quiz"
print " Press Enter to continue."
end
Console_Screen.pause
end
def display_credits
Console_Screen.cls
puts " Thankyou for taking the quiz"
end
#Main Script Logic
$noRight = 0
Console_Screen = Screen.new
SQ = Quiz.new
SQ.display_greeting
answer = " "
loop do
Console_Screen.cls
print "Are you ready to take the quiz? (y/n): "
answer = STDIN.gets
answer.chop!
break if answer == "y" || answer == "n"
end
#Analyze the players input
if answer == "n"
Console_Screen.cls
puts "Okay, perhaps another time. "
else
SQ.display_instructions
SQ.disp_q("What is the name of the Daily Planet's ace photographer?",
"a. Jimmy Olsen", "b. Clark Kent", "c. Lex Luthor", "d. Louis Lane",
"a")
SQ.disp_q("What is the name of Clark Kent's home town?",
"a. Metropolis", "b. Gotham City", "c. Smallville", "d. New York",
"c")
SQ.disp_q("In which movie did Superman battle General Zod?",
"a. Superman", "b. Superman II", "c. Superman III", "d. Superman IV",
"b")
SQ.disp_q("What is the name of Superman's father?",
"a. Nimo", "b. Jarrell", "c. Lex Luthor", "d. Krypton",
"b")
SQ.disp_q("Where had Superman been at the start of Superman returns?",
"a. Moon", "b. Fortress of Solitude", "c. Earth's Core", "d. Krypton",
"d")
SQ.determine_grade
SQ.display_credits
end