Homework 3: Input and Output


  • Use a Scanner to read input of varying data types.
  • Format numerical data to fixed precision for display.

Exercise 3.1   Vacations

In the previous chapter you wrote a simple Mad Lib based on hard-coded values. Now that you've learned how to read keyboard input, let's design a more useful Mad Lib program.

This exercise is based the Vacations Mad Lib (available on the Printables section of MadLibs.com). Write a program named Vacations.java that asks the user to input two adjectives and two nouns:

Adjective: tranquil
Adjective: scandalous
Noun: pancake
Noun: field

Use System.out.print() to display each prompt exactly as shown. The input (shown in this color), should be on the same line as the prompt. There must be exactly one space after each colon. After reading the input, the program should output the following three lines:

[blank line]
A vacation is when you take a trip to some tranquil place with your scandalous family.
Usually you go to some place that is near a/an pancake or up on a/an field.

Note that the first line should be blank; don't print the text "[blank line]" literally! And you don't have to print anything in color; we simply wanted to show you where the input goes.

Exercise 3.2   Tip Calculator

Google has a variety of search tricks that present users with instant results. For example, if you search Google for tip calculator, there is an interactive tool at the top of the results. Write a program named TipCalculator.java that implements a similar tool. Prompt the user for the following input:

Enter bill amount: 43.21
Percentage to tip: 18
Number of people: 2

Then compute the tip amount and total amount for the bill. Display the results using this format:

Tip amount: $7.78
Total amount: $50.99

Tip per person: $3.89
Total per person: $25.49

Your program should format all dollar amounts with two decimal places. Notice that there is a blank line before each section of the output, and there are six lines total.

Exercise 3.3   Add Minutes

Write a program named AddMinutes.java that prompts the user for a starting time and how many minutes to add:

Starting hour (0-23)? 13
Starting minute (0-59)? 25
How many minutes to add? 340

In this example, we are adding 340 minutes (five hours and forty minutes) to 1:25pm; the result is 7:05pm. Your program should output a blank line followed by the result in 24-hour format:

The time is 19:05

The hour displayed must be between 0 and 23. For example, adding 120 minutes to 23:00 should be 1:00 (not 25:00). The minute must be between 00 and 59, and it must be two digits. For example, adding 10 minutes to 8:55 should be 9:05 (not 8:65, not 9:5).

Your solution must NOT use conditionals (Chapter 5) or loops (Chapter 6). Autolab will automatically reject submissions containing words like if, for, and while.

Exercise 3.4   Planet Weights

As part of a future science project, your team is getting ready to send real objects into space! You need to know how much things weigh on other planets, so that you can determine various requirements such as fuel. Fortunately, the math is straightforward: simply multiply the weight of an item by a conversion factor. For example, a 1-pound object on Earth weighs 2.364 pounds on Jupiter.

Mercury 0.378
Venus 0.907
the Moon 0.166
Mars 0.377
Jupiter 2.364
Saturn 1.064
Uranus 0.889
Neptune 1.125
Pluto 0.067


Write a program named PlanetWeights.java that reads in the name of an object, its weight(read this in as a double type) on Earth, and the unit of measurement:

Enter the name of the item: Airbus A380
How much does the item weigh? 560000
What is the unit of measure? kg

The program will then calculate and output the weight of the object on the nine "planets" above. (Yes, we know that the Moon and Pluto are technically not planets.) You do NOT need to declare constants for the conversion factors; just use them when needed. All weights should be formatted with three decimal places and comma separators, as shown in the output below:

On Mercury, the Airbus A380 weighs about 211,680.000 kg.
On Venus, the Airbus A380 weighs about 507,920.000 kg.
On the Moon, the Airbus A380 weighs about 92,960.000 kg.
On Mars, the Airbus A380 weighs about 211,120.000 kg.
On Jupiter, the Airbus A380 weighs about 1,323,840.000 kg.
On Saturn, the Airbus A380 weighs about 595,840.000 kg.
On Uranus, the Airbus A380 weighs about 497,840.000 kg.
On Neptune, the Airbus A380 weighs about 630,000.000 kg.
On Pluto, the Airbus A380 weighs about 37,520.000 kg.

Your solution must NOT use loops (Chapter 6) or arrays (Chapter 7). Autolab will automatically reject submissions containing words like for, while, and double[].

Exercise 3.5   Change Machine

When you shop at a grocery or retail store, someone scans your items so that a computer can determine the total purchase amount (including taxes and discounts). Customers who pay in cash often use whole dollars, rather than provide the exact amount in coins. That's where your program comes into the story. You are to implement an algorithm for an automatic change dispenser.

Write a program named ChangeMachine.java that prompts the user for two values:

Total amount? 18.76
Cash payment? 20

You may assume that the cash paid will always be a whole number (representing dollar bills) that is greater than or equal to the total amount. The program will then calculate and output the amount of change due and how many dollars, quarters, dimes, nickels, and pennies should be dispensed:

Change Due $1.24

  Dollars: 1
 Quarters: 0
    Dimes: 2
  Nickels: 0
  Pennies: 4

To avoid floating-point rounding errors (see Chapter 2), use Math.round when converting the amount of change due to an integer (e.g., $1.24 = 124 cents). You will need to cast the result of Math.round to an int.

Your solution must NOT use conditionals (Chapter 5) or loops (Chapter 6). Autolab will automatically reject submissions containing words like if, for, and while.


Back to Top