Question

In: Computer Science

Provide immediate feedback for each mistyped sentence. To do so, modify the Test class’s present_test method...

Provide immediate feedback for each mistyped sentence. To do so, modify the Test class’s present_test method so that it informs the player a mistake has been made, then display the challenge sentence followed by the player’s sentence so the player can determine where the error lies.

#--------------------------------------------------------------------------

#

# Script Name: TypingChallenge.rb

# Version: 1.0

# Author: Jerry Lee Ford, Jr.

# Date: March 2010

#

# Description: This Ruby script demonstrates how to apply conditional logic

# in order to analyze user input and control script execution

# through the development of a computer typing test that

# evaluates the player's typing skills.

#

#--------------------------------------------------------------------------

# Define custom classes ---------------------------------------------------

#Define a class representing the console window

class Screen

def cls #Define a method that clears the display area

puts ("\n" * 25) #Scroll the screen 25 times

puts "\a" #Make a little noise to get the player's attention

end

  

def pause #Define a method that pauses the display area

STDIN.gets #Execute the STDIN class's gets method to pause script

#execution until the player presses the Enter key

end

  

end

#Define a class representing the typing test

class Test

#This method displays the 8-ball greeting message

def display_greeting

  

Console_Screen.cls #Clear the display area

  

#Display a welcome screen

print "\t\t Welcome to the Ruby Typing Challenge game!" +

"\n\n\n\n\n\n\n\n\n\n\n\n\nPress Enter to " +

   "continue. \n\n: "

  

Console_Screen.pause #Pause the game

end

#Define a method to be used to present test instructions

def display_instructions

  

Console_Screen.cls #Clear the display area

puts "\t\t\tInstructions:\n\n" #Display a heading

#Display the game's instructions

puts %Q{ This test consists of a series of 10 typing challenges.

The challenge sentences are presented one at a time. To respond

correctly, you must retype each sentence exactly as shown and press

the Enter key. Your grade will be displayed at the end of the test.

\n\n

Grading format: 9 or better sentences typed correctly will result in an 'A',

8 sentences typed correctly will result in a 'B',

7 sentences typed correctly will result in a 'C',

6 sentences typed correctly will result in a 'D',

less than 6 will result in an 'F'!

\n\n\n\n\n\n\n\n\n

Press Enter to continue.\n\n}

Console_Screen.pause #Pause the game

  

end

#Define a method to be used to present typing challenges

def present_test(challenge)

  

Console_Screen.cls #Clear the display area

print challenge + "\n\n: " #Display the challenge sentence

result = STDIN.gets #Collect the player's input

result.chop! #Remove the end of line marker

  

#Analyze the player input and see if it is correct

if challenge == result then

  

#Keep track of the number of correctly retyped challenge sentences

$noRight += 1

Console_Screen.cls #Clear the display area

#Keep the player informed

print "Correct!\n\nPress Enter to continue."

Console_Screen.pause #Pause the game

  

else

  

Console_Screen.cls #Clear the display area

#Keep the player informed

print "Incorrect!\n\nPress Enter to continue."

Console_Screen.pause #Clear the game

  

end

  

end

#Define a method to be used to display test results

def determine_grade

  

Console_Screen.cls #Clear the display area

  

#To pass the test the player must correctly retype 6 sentences

if $noRight >= 6 then

  

#Inform the player of the good news

print "You retyped " + $noRight.to_s + " sentence(s) correctly. "

puts "You have passed the typing test!\n\nPress Enter to continue."

  

else #The player has failed the test

  

#Inform the player of the bad news

print "You retyped " + $noRight.to_s + " sentence(s) correctly. "

puts "You have failed the typing test!\n\nPress Enter to continue."

  

end

# Grading Sytem for the Challenge

letter Grade = case $noRight

when 9..10 then "A"

when 8 then "B"

when 7 then "C"

when 6 then "D"

else "F"

end

end

# Main Script Logic -------------------------------------------------------

#Initialize global variable that will be used to keep track of the number

#of correctly retyped sentences

$noRight = 0

Console_Screen = Screen.new #Instantiate a new Screen object

Typing_Test = Test.new #Instantiate a new Test object

#Execute the Test object's display_greeting method

Typing_Test.display_greeting

#Execute the Screen object's cls method in order to clear the screen

Console_Screen.cls

#Prompt the player for permission to begin the test

print "Would you like to test your typing skills? (y/n)\n\n: "

answer = STDIN.gets #Collect the player's response

answer.chop! #Remove any extra characters appended to the string

  

#Loop until the player enters y or n and do not accept any other input.

until answer == "y" || answer == "n"

Console_Screen.cls #Clear the display area

#Prompt the player for permission to begin the test

print "Would you like to test your typing skills? (y/n)\n\n: "

answer = STDIN.gets #Collect the player's response

answer.chop! #Remove any extra characters appended to the string

end

#Analyze the player's response

if answer == "n" #See if the player elected not to play

Console_Screen.cls #Clear the display area

#Invite the player to return and play again

puts "Okay, perhaps another time.\n\n"

else #The player wants to take the test

#Execute the Test object's display_instructions method

Typing_Test.display_instructions

  

#Display typing challenges and grade each answer by calling on the

#Test object's present_test method

Typing_Test.present_test "In the end there can be only one."

Typing_Test.present_test "Once a great plague swept across the land."

Typing_Test.present_test "Welcome to Ruby Principles of Ruby Programming"

Typing_Test.present_test "There are very few problems in the world " +

"that enough M&Ms cannot fix."

Typing_Test.present_test "Perhaps today is a good day to die. Fight " +

"beside me and let us die together."

Typing_Test.present_test "Don't cry because it's over, smile because it happened"

Typing_Test.present_test "Be yourself; everyone else is already taken."

Typing_Test.present_test "So many books, so little time."

Typing_Test.present_test "A room without books is like a body without a soul."

Typing_Test.present_test "You know you're in love when you can't fall asleep because reality is finally better than your dreams."

#Notify the player of the results by executing the Test object's

#display_instructions method

Typing_Test.determine_grade

Console_Screen.pause #Pause the game

  

Console_Screen.cls #Clear the display area

#Thank the player for taking the typing test

puts "Thank you for taking the Ruby Typing Challenge.\n\n"

end

Solutions

Expert Solution

TypingChallenge.rb

class Screen

def cls
puts ("\n" * 30)
puts "\a"
end

def pause
STDIN.gets
end

end

class Test

def display_greeting
Console_Screen.cls
print "\t\t Welcome to the Ruby Typing Challenge Game!" +
"\n\n\n\n\n\n\n\n\n Press Enter to " +
       "continue. \n\n: "

Console_Screen.pause

end

  
def display_instruction

Console_Screen.cls
puts "\t\t\t\tInstruction: \n\n"

puts %Q{This test has 5 typing challenges. Type exactly same as sentences. Your grade will be shown at the end of the test. \n\n\
   press Enter to continue. \n\n }

Console_Screen.pause

end

def present_test(challenge)
Console_Screen.cls
print challenge + "\n\n: "
result = STDIN.gets
result.chop!

if challenge == result then
  
   $noRight += 1
   Console_Screen.cls

print "Corrcet!\n\n Press Enter to continue."
   Console_Screen.pause
else
   Console_Screen.cls
print "Incorrect!\n\n Press Enter to continue."
    Console_Screen.pause
end

end

def determine_grade
Console_Screen.cls

if $noRight >= 3 then
   print "You retyped " + $noRight.to_s + " sentence(s) correctly. "
   puts "You have passed the typing test!\n\n Press Enter to continue."
else
   print "You retyped " + $noRight.to_s + " sentence(s) correctly. "
   puts "You have failed the typing test!\n\nPress enter to continue."
end
end


$noRight = 0

Console_Screen = Screen.new
Typing_Test = Test.new

Typing_Test.display_greeting

Console_Screen.cls

print "Would you like to test your typing skills? (y/n) \n\n: "

answer = STDIN.gets
answer.chop!

until answer =="y" || answer == "n"
Console_Screen.cls
print "Would you like to test your typing skills? (y/n) \n\n: "
answer = STDIN.gets
answer.chop!
end

if answer == "n"
Console_Screen.cls
puts "Okay, perhaps another time.\n\n"

else
Typing_Test.display_instruction
Typing_Test.present_test "In the end there can be only one."
Typing_Test.present_test "hello there."
Typing_Test.present_test "how are you?"
Typing_Test.present_test "Chicago is awesome!!"

Typing_Test.determine_grade
Console_Screen.pause

Console_Screen.cls

puts "Thank you for playing Ruby Typing Challenge game.\n\n"

end

end


Related Solutions

Modify the processFile method so that it will compile and it will not produce a runtime...
Modify the processFile method so that it will compile and it will not produce a runtime error: public static void processFile(File file) { File input = "contents.txt"; String line = null; try { input = new File(file); while ((line = input.readLine()) != null) { System.out.println(line); } return; } finally { if (file != null) { file.close(); } } }
For each of these provide two sentences: first sentence provide a definition of the term, and...
For each of these provide two sentences: first sentence provide a definition of the term, and in the second sentence explain its significance in the context of women’s rights as human rights. Material v symbolic body Sex vs. gender Intersectionality Patriarchy Globalization
How do you provide quality feedback in the ESL classroom?
How do you provide quality feedback in the ESL classroom?
Please write in Bold letters thanks In a sentence or two each, provide examples of each...
Please write in Bold letters thanks In a sentence or two each, provide examples of each of the following terms and explain why the example illustrates the related concept. Do not use examples from the text but use critical thinking to make up your own. (1 point each) Independent events Dependent events Complementary events Union of events Intersection of events
Within Store.java, the refundOrder method has been started for you. Do not modify it’s method signature....
Within Store.java, the refundOrder method has been started for you. Do not modify it’s method signature. Ensure you understand how it works. Complete the refundOrder method (method stub provided for you) so that it: - Searches through the Store’s ArrayList of Order objects (hint: this method is in the same class as the ArrayList attribute) checking if each Order object’s orderNum matches the orderNum passed in as the method’s argument. - If a matching orderNum is found, that Order object’s...
Modify the partition.java program (Listing 7.2) so that the partitionIt() method always uses the highest-index (right)...
Modify the partition.java program (Listing 7.2) so that the partitionIt() method always uses the highest-index (right) element as the pivot, rather than an arbitrary number. (This is similar to what happens in the quickSort1.java program in Listing 7.3.) Make sure your routine will work for arrays of three or fewer elements. To do so, you may need a few extra statements. // partition.java // demonstrates partitioning an array // to run this program: C>java PartitionApp //////////////////////////////////////////////////////////////// class ArrayPar { private...
Java instructions: 1. Modify abstract superclass (Employee10A) so it comment out the abstract payPrint method and...
Java instructions: 1. Modify abstract superclass (Employee10A) so it comment out the abstract payPrint method and uses a toString method to print out it’s instance variables. Make sure toString method cannot be overridden.​​​​​​ Source code below: public abstract class Employee10A {    private String firstName, lastName; static int counter = 0;    public Employee10A(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; }    @Override public String toString() { return ("The employee's full name is " + firstName...
Modify the following 'MessageBoxes' application so it uses a single action listener for each button. This...
Modify the following 'MessageBoxes' application so it uses a single action listener for each button. This will require you to separate the single action listener logic into multiple listeners, one for each button. Then modify the code to provide additional options to two or more buttons. /* * The source code for this assignment started with * a sample from "Thinking in Java" 3rd ed. page 825 * by Bruce Eckel. I have finished adding the rest of the action...
public class CashRegisterPartTwo { // TODO: Do not modify the main method. // You can add...
public class CashRegisterPartTwo { // TODO: Do not modify the main method. // You can add comments, but the main method should look exactly like this // when you submit your project. public static void main(String[] args) { runTransaction(); } // TODO: Define the runTranscation method. // runTransaction runs the cash register process from start to finish. However, // it will not contain ALL Of the code. You are going to have to delegate some of // its functionality to...
Modify the below program so that the following two conditions are met: 1. Each child terminates...
Modify the below program so that the following two conditions are met: 1. Each child terminates abnormally after attempting to write to a location in the read-only text segment. 2. The parent prints output that is identical (except for the PIDs) to the following: child 12255 terminated by signal 11: Segmentation fault child 12254 terminated by signal 11: Segmentation fault Hint: Read the man page for psignal(3) ___________________________________. #include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <string.h> #include <errno.h> #include <sys/types.h>...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT