Topics Covered: Data Types, Booleans, Control flow and logic
Read chapter 9 from Learn to Program and review chapter 6
Note: All exercises must live inside of your s0203_exercises/firstname_lastname/d2
folder
While inside of your s0203_exercises
directory, you can git add .
then git commit -m "Helpful commit message here"
and git push origin master
to submit your homework to GitHub. (Don't forget to use git status
along the way to check the state of your files in git)
Note: To run your program in terminal, navigate to the directory holding the file and run the command ruby <filename.rb>
WARNING Exercises are taken from Learn to Program. Please do NOT look at the answer key in the e-book, you would only be cheating yourself. If you run into a problem, first ask a classmate, if they can't help you then Google, if you're still stumped please ask one of the TA's for help.
Full name greeting. Write a program that asks for a person’s first name, then middle, and then last. Finally, it should greet the person using their full name
Bigger, better favorite number. Write a program that asks for a person’s favorite number. Have your program add 1 to the number, and then suggest the result as a bigger and better favorite number. (Do be tactful about it, though.)
Angry boss. Write an angry boss program that rudely asks what
you want. Whatever you answer, the angry boss should yell it
back to you and then fire you. For example, if you type in I want
a raise, it should yell back like this:
WHADDAYA MEAN "I WANT A RAISE"?!? YOU'RE FIRED!!
“99 Bottles of Beer on the Wall.” Write a program that prints out the lyrics to that beloved classic, “99 Bottles of Beer on the Wall.”
Leap years. Write a program that asks for a starting year and an ending year and then puts all the leap years between them (and including them, if they are also leap years). Leap years are years divisible by 4 (like 1984 and 2004). However, years divisible by 100 are not leap years (such as 1800 and 1900) unless they are also divisible by 400 (such as 1600 and 2000, which were in fact leap years). What a mess!
Deaf grandma. Whatever you say to Grandma (whatever you type
in), she should respond with this: HUH?! SPEAK UP, SONNY!
unless you shout it (type in all capitals). If you shout, she can
hear you (or at least she thinks so) and yells back: NO, NOT SINCE 1938!
To make your program really believable, have Grandma shout a different year each time, maybe any year at random between 1930 and 1950. (This part is optional and would be much easier if you read the section on Ruby’s random number generator on page 38 of Learn to Program.)
You can’t stop talking to Grandma until you shout BYE.
Hint 1: Don’t forget about the .strip
(or .chomp
) method! 'BYE' with an Enter at the end is
not the same as 'BYE' without one!
Hint 2: Try to think about what parts of your program should happen over and over again. All of those should be in your while loop.
Hint 3: How can we make rand give us a number in a range not starting at zero? Well, we can’t; rand just doesn’t work that way. We’ll have to do something to the number rand returns to us.
Note: This is not required, but if the exercises were not challenging enough for you, try doing them again; this time using JavaScript instead of Ruby.