Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #a class that checkes if values are numeric
- class String
- def is_number?
- true if Float(self) rescue false
- end
- end
- #1 Prompt for a number. If the number is greater than 70, print PASSING; otherwise, print NOT PASSING.
- # retrieve grade
- print "Enter a grade number:"
- grade = gets.chomp
- #method to check if grade is numeric
- if !grade.is_number?
- #if grade is not numeric, ask user for numeric value
- while !grade.is_number?
- print "Looks like you didn't enter a number. Re-enter a number: "
- grade = gets.chomp
- end
- end
- #change numeric string to integer
- grade = grade.to_i
- #check if passing
- if grade > 70
- print "PASSING"
- else print "NOT PASSING"
- end
- puts
- #2 Prompt for a string. If the string is equal to green, print GO, otherwise, print STOP.
- print "Pick a color:"
- color = gets.chomp.downcase
- if color === "green"
- print "GO"
- else print "STOP"
- end
- puts
- #3 Prompt for a number. If the number is even, print EVEN, otherwise, print ODD.
- print "Enter a random number:"
- number = gets.chomp.to_i
- #check to be sure number is numerical
- if number % 2 === 0
- print "EVEN"
- else print "ODD"
- end
- puts
- #4 Prompt for a number. If the number is evenly divisible by 5, print MULTIPLE OF 5, otherwise, print NOT A MULTIPLE OF 5.
- print "Enter another number:"
- num = gets.chomp
- #check to be sure number is numerical
- while !num.is_number?
- print "Looks like you didn't enter a number. Re-enter a number. "
- num = gets.chomp
- end
- #num string to integer
- num = num.to_i
- if num % 5 === 0
- print "MULTIPLE OF 5"
- else print "NOT A MULTIPLE OF 5"
- end
- puts
- #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.
- print "Enter one more number:"
- digits = gets.chomp
- #check to be sure number is numerical
- while !digits.is_number?
- print "Looks like you didn't enter a number. Re-enter a number. "
- digits = gets.chomp
- end
- #numerical string to integer
- digits = digits.to_i
- if digits < 10
- print "ONE DIGIT"
- elsif digits >= 100
- print "THREE DIGITS"
- else print "TWO DIGITS"
- end
- puts
- #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.
- print "Enter a jersey number:"
- jersey_num = gets.chomp
- #check to be sure number is numerical
- while !jersey_num.is_number?
- print "Looks like you didn't enter a number. Re-enter a number. "
- jersey_num = gets.chomp
- end
- #change nemeric string to integer
- jersey_num = jersey_num.to_i
- if jersey_num === 12 || jersey_num === 71 || jersey_num === 80
- puts "That number is retired from the Seattle Seahawks!"
- end
- puts
- #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!
- print "Enter a U.S. State:"
- state = gets.chomp.downcase
- if state === "washington"
- print "This state is in the PNW!"
- elsif state === "oregon"
- print "This state is in the PNW!"
- elsif state === "idaho"
- print "This state is in the PNW!"
- else print "You should move to the PNW; it's great here!"
- end
- puts
- #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).
- print "Which size drink would you like?: short, tall, grande, or venti?"
- size = gets.chomp.downcase
- #if size is not a correct value, ask for correct value
- while size != "short" && size != "tall" && size != "grande" && size != "venti"
- print "Looks like you didn't enter a matching size. Please re-enter:"
- size = gets.chomp.downcase
- end
- if size === "short"
- print "8 ounces"
- elsif size === "tall"
- print "12 ounces"
- elsif size === "grande"
- print "16 ounces"
- elsif size === "venti"
- print "20 ounces"
- end
- puts
- #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).
- print "How many hours did you work this week?"
- hours = gets.chomp
- #check to be sure number is numerical
- while !hours.is_number?
- print "Looks like you didn't enter a number. Re-enter a number. "
- hours = gets.chomp
- end
- #string to integer
- hours = hours.to_i
- print "How much do you get paid an hour?"
- pay_rate = gets.chomp
- #check to be sure number is numerical
- while !pay_rate.is_number?
- print "Looks like you didn't enter a number. Re-enter a number. "
- pay_rate = gets.chomp
- end
- #string to integer
- pay_rate = pay_rate.to_i
- overtime = (hours - 40) * pay_rate * 1.5
- if hours > 40
- gross_pay = 40 * pay_rate + overtime
- print "Your gross pay is: $" + gross_pay.to_s
- else gross_pay = hours * pay_rate
- print "Your gross pay is: $" + gross_pay.to_s
- end
- puts
- #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.
- print "How many hours did you work this week?"
- hours = gets.chomp
- #check to be sure number is numerical
- while !hours.is_number?
- print "Looks like you didn't enter a number. Re-enter a number. "
- hours = gets.chomp
- end
- #string to integer
- hours = hours.to_i
- print "How much do you get paid an hour?"
- pay_rate = gets.chomp
- #check to be sure number is numerical
- while !pay_rate.is_number?
- print "Looks like you didn't enter a number. Re-enter a number. "
- pay_rate = gets.chomp
- end
- #string to integer
- pay_rate = pay_rate.to_i
- overtime = (hours - 40) * pay_rate * 1.5
- if hours > 60
- print "Please see manager."
- elsif hours > 40
- gross_pay = 40 * pay_rate + overtime
- print "Your gross pay is: $" + gross_pay.to_s
- else gross_pay = hours * pay_rate
- print "Your gross pay is: $" + gross_pay.to_s
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement