Professional Documents
Culture Documents
Lab2 SkiRental
Lab2 SkiRental
2 – Ski Rental
Learning Goals
Background
Often times, you have a choice between renting or buying something. For
example, many people prefer to rent their skis for each day they go skiing,
because it is far cheaper than buying skis they will only use a few times. But for
someone who plans to ski a lot, buying may be cheaper!
Your Task
Write a program that will ask for the following pieces of information (in this
exact order):
• Item Name
• Purchase price
• Time units of the rental
• Rental rate (price per time unit)
• Anticipated length of time to rent (per time unit)
Help a user decide between renting and buying an item by calculating and
outputting 4 things:
1. How much renting for the anticipated length of time will cost
2. Which option (“RENT” or “BUY”) is cheaper for their anticipated rental time.
(In the case of a tie, output “BUY”)
3. How much money is saved by choosing the better option
4. The “break-even” point of time
Caution: Beware the behavior of Scanner’s nextInt()/nextDouble() followed by a
nextLine() –nextInt()/nextDouble() do NOT consume “newline” characters after
the numbers, so the subsequent nextLine() will read the remainder of the same
line, not the actual next line. Another nextLine() will actually read the following
line.
20 pts – Program requests the required information from the user, in the order
required, using the given item name in subsequent requests appropriately
(see orange text)
20 pts – Program correctly calculates and outputs the cost of the rental.
20 pts – The cheaper option (RENT or BUY) is properly identified, and the amount of
savings is accurately noted.
20 pts – The break-even point is correctly calculated.
10 pts – The program’s output is well formatted – (rounding is NOT required).
10 points – Code contains regular comments for every logical group of code.
Extensions
Want to make this program more interesting/useful? After you finish and submit the
basic assignment, here are some ideas:
1. Some rental agencies also charge a one-time fee, regardless of the length of time
rented. Incorporate this into the program.
2. Many rentals have various rates for different time units/lengths – often if you rent
longer, you get a cheaper rate! Prompt the user to specify multiple rental time
units, lengths, and rates, until they indicate they are done (say, with a blank time
unit). Compare each rental option against the purchase price.
(We will learn how to repeatedly execute code and how to compare the contents of
a string in upcoming lessons)
Note: Do not add extensions to the assignment you submit; the automatic tests will
not expect the changes, and will given errors.
READ MORE: Not only is this common situation relevant for many kinds of
rentable items, it has inspired a whole class of computational problems, where
the question is when to purchase the skis even if you aren’t certain how many
times you’ll ski… see https://en.wikipedia.org/wiki/Ski_rental_problem if you’re
curious