Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Lab 1.

2 – Ski Rental
Learning Goals

1. Develop your ability to write and run programs.


2. Develop your ability to read input and give output via the console
3. Develop your ability to perform arithmetic and conditional operations.

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!

Deciding whether renting or buying is better, of course, depends on how many


days you plan to rent. There is always some “break-even” point of time, at
which the cost of renting matches the purchase price; for any length of time
beyond the “break-even” point, it would be better to purchase.

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.

Note: Ideally, money should be rounded to 2 decimals. The length of time


would ideally also be rounded. However, we will learn to format numbers to
specific decimal places later, so don’t worry about that here.
Sample Runs
What are you buying/renting?
skis
How much does it cost to BUY the skis?
599.95
What is the rental time unit?
day
How much does it cost to RENT the skis per day?
104.99
How many days do you plan to RENT?
4

Total expected rental cost: $419.96


It is cheaper to RENT; you'd save $179.99001220703127
The break-even point is 5.7143537479759985 days
_____________________________________________________

What are you buying/renting?


apartment
How much does it cost to BUY the apartment?
150000
What is the rental time unit?
month
How much does it cost to RENT the apartment per month?
1600
How many months do you expect to rent?
120

Total expected rental cost: $192000.0


It is cheaper to BUY; you'd save $42000.0
The break-even point is 93.75 months
Grading
This assignment will be graded out of 100 points.

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

You might also like