Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.75 KB | None | 0 0
  1. #a class that checkes if values are numeric
  2. class String
  3. def is_number?
  4. true if Float(self) rescue false
  5. end
  6. end
  7.  
  8. #1 Prompt for a number. If the number is greater than 70, print PASSING; otherwise, print NOT PASSING.
  9.  
  10. # retrieve grade
  11. print "Enter a grade number:"
  12. grade = gets.chomp
  13.  
  14. #method to check if grade is numeric
  15. if !grade.is_number?
  16. #if grade is not numeric, ask user for numeric value
  17. while !grade.is_number?
  18. print "Looks like you didn't enter a number. Re-enter a number: "
  19. grade = gets.chomp
  20. end
  21. end
  22.  
  23. #change numeric string to integer
  24. grade = grade.to_i
  25.  
  26. #check if passing
  27. if grade > 70
  28. print "PASSING"
  29. else print "NOT PASSING"
  30. end
  31. puts
  32.  
  33. #2 Prompt for a string. If the string is equal to green, print GO, otherwise, print STOP.
  34.  
  35. print "Pick a color:"
  36. color = gets.chomp.downcase
  37.  
  38. if color === "green"
  39. print "GO"
  40. else print "STOP"
  41. end
  42. puts
  43.  
  44. #3 Prompt for a number. If the number is even, print EVEN, otherwise, print ODD.
  45.  
  46. print "Enter a random number:"
  47. number = gets.chomp.to_i
  48.  
  49. #check to be sure number is numerical
  50.  
  51. if number % 2 === 0
  52. print "EVEN"
  53. else print "ODD"
  54. end
  55. puts
  56.  
  57. #4 Prompt for a number. If the number is evenly divisible by 5, print MULTIPLE OF 5, otherwise, print NOT A MULTIPLE OF 5.
  58.  
  59. print "Enter another number:"
  60. num = gets.chomp
  61.  
  62. #check to be sure number is numerical
  63. while !num.is_number?
  64. print "Looks like you didn't enter a number. Re-enter a number. "
  65. num = gets.chomp
  66. end
  67.  
  68. #num string to integer
  69. num = num.to_i
  70.  
  71. if num % 5 === 0
  72. print "MULTIPLE OF 5"
  73. else print "NOT A MULTIPLE OF 5"
  74. end
  75. puts
  76.  
  77. #5 Prompt for a number. If the number is less than 10, print ONE DIGIT. If the number is 100 or greater, print THREE DIGITS, otherwise print TWO DIGITS.
  78.  
  79. print "Enter one more number:"
  80. digits = gets.chomp
  81.  
  82. #check to be sure number is numerical
  83. while !digits.is_number?
  84. print "Looks like you didn't enter a number. Re-enter a number. "
  85. digits = gets.chomp
  86. end
  87.  
  88. #numerical string to integer
  89. digits = digits.to_i
  90.  
  91. if digits < 10
  92. print "ONE DIGIT"
  93. elsif digits >= 100
  94. print "THREE DIGITS"
  95. else print "TWO DIGITS"
  96. end
  97. puts
  98.  
  99. #6 Prompt for a jersey number. If that number is 12, 71, or 80, print That number is retired from the Seattle Seahawks!, otherwise do nothing.
  100.  
  101. print "Enter a jersey number:"
  102. jersey_num = gets.chomp
  103.  
  104. #check to be sure number is numerical
  105. while !jersey_num.is_number?
  106. print "Looks like you didn't enter a number. Re-enter a number. "
  107. jersey_num = gets.chomp
  108. end
  109.  
  110. #change nemeric string to integer
  111. jersey_num = jersey_num.to_i
  112.  
  113. if jersey_num === 12 || jersey_num === 71 || jersey_num === 80
  114. puts "That number is retired from the Seattle Seahawks!"
  115. end
  116. puts
  117.  
  118. #7 Prompt for a state. If the state is Washington, Oregon, or Idaho, print This state is in the PNW, otherwise print You should move to the PNW; it’s great here!
  119.  
  120. print "Enter a U.S. State:"
  121. state = gets.chomp.downcase
  122.  
  123. if state === "washington"
  124. print "This state is in the PNW!"
  125. elsif state === "oregon"
  126. print "This state is in the PNW!"
  127. elsif state === "idaho"
  128. print "This state is in the PNW!"
  129. else print "You should move to the PNW; it's great here!"
  130. end
  131. puts
  132.  
  133. #8 Prompt for a one of the following: SHORT, TALL, GRANDE, VENTI. Print out the number of ounces that drink includes (8, 12, 16, 20 respectively).
  134.  
  135. print "Which size drink would you like?: short, tall, grande, or venti?"
  136. size = gets.chomp.downcase
  137.  
  138. #if size is not a correct value, ask for correct value
  139. while size != "short" && size != "tall" && size != "grande" && size != "venti"
  140. print "Looks like you didn't enter a matching size. Please re-enter:"
  141. size = gets.chomp.downcase
  142. end
  143.  
  144. if size === "short"
  145. print "8 ounces"
  146. elsif size === "tall"
  147. print "12 ounces"
  148. elsif size === "grande"
  149. print "16 ounces"
  150. elsif size === "venti"
  151. print "20 ounces"
  152. end
  153. puts
  154.  
  155. #9 Prompt for rate of pay and hours worked. Calculate gross pay. Provide time-and-a-half for hours worked beyond 40 (e.g., if you get paid $10/hr and work 45 hours in a week, you would gross $475 (40 x 10 + 5 x 15).
  156.  
  157. print "How many hours did you work this week?"
  158. hours = gets.chomp
  159.  
  160. #check to be sure number is numerical
  161. while !hours.is_number?
  162. print "Looks like you didn't enter a number. Re-enter a number. "
  163. hours = gets.chomp
  164. end
  165.  
  166. #string to integer
  167. hours = hours.to_i
  168.  
  169. print "How much do you get paid an hour?"
  170. pay_rate = gets.chomp
  171.  
  172. #check to be sure number is numerical
  173. while !pay_rate.is_number?
  174. print "Looks like you didn't enter a number. Re-enter a number. "
  175. pay_rate = gets.chomp
  176. end
  177.  
  178. #string to integer
  179. pay_rate = pay_rate.to_i
  180.  
  181. overtime = (hours - 40) * pay_rate * 1.5
  182.  
  183. if hours > 40
  184. gross_pay = 40 * pay_rate + overtime
  185. print "Your gross pay is: $" + gross_pay.to_s
  186. else gross_pay = hours * pay_rate
  187. print "Your gross pay is: $" + gross_pay.to_s
  188. end
  189. puts
  190.  
  191. #10 Rewrite the solution to the previous problem adding this modification: do not process any employee if their hours worked is greater than 60, instead display the message Please see manager.
  192.  
  193. print "How many hours did you work this week?"
  194. hours = gets.chomp
  195.  
  196. #check to be sure number is numerical
  197. while !hours.is_number?
  198. print "Looks like you didn't enter a number. Re-enter a number. "
  199. hours = gets.chomp
  200. end
  201.  
  202. #string to integer
  203. hours = hours.to_i
  204.  
  205. print "How much do you get paid an hour?"
  206. pay_rate = gets.chomp
  207.  
  208. #check to be sure number is numerical
  209. while !pay_rate.is_number?
  210. print "Looks like you didn't enter a number. Re-enter a number. "
  211. pay_rate = gets.chomp
  212. end
  213.  
  214. #string to integer
  215. pay_rate = pay_rate.to_i
  216.  
  217. overtime = (hours - 40) * pay_rate * 1.5
  218.  
  219. if hours > 60
  220. print "Please see manager."
  221. elsif hours > 40
  222. gross_pay = 40 * pay_rate + overtime
  223. print "Your gross pay is: $" + gross_pay.to_s
  224. else gross_pay = hours * pay_rate
  225. print "Your gross pay is: $" + gross_pay.to_s
  226. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement