ITEC 120
Interactive Bills and coins

Objectives

After successfully completing this lab you will be able to develop interactive programs.

Assignment

Change the Money program you wrote for the last lab to make it interactive.
That means you will ask the user for input (prompt), read the input,
and print results based on the user input.

To do this, you will use the Scanner class to get input from the keyboard.
At the top of your program, you will need to import the Scanner class, as shown in green:

/**
  * header comments
  */
  import java.util.Scanner;
  public class Money
  {

In in the main method, before you read the first input from the user,
you will need to create a Scanner object :

Scanner scan = new Scanner(System.in);

You will use this Scanner object you just created to call methods of the Scanner class.

 

Prompt for input

Change your program to ask the user to type in a dollar amount. This is called a prompt.
After each prompt, you'll use the Scanner object to read in what the user types. For example:

dollarAmount = scan.nextInt();

Whenever you use an object to call a method, you use the dot operator. obj.method()

The method you call must be defined in the class of whatever type your obj reference variable is.
In this case, that's the Scanner class. The Scanner class is a class defined in the Java API (Application Programming Interface). There are literally thousands of classes written and available for your use in the Java API. You can peruse them here. Here's a link to the Scanner class.

The nextInt()method reads information as a int, so you'll need to store that info in a int variable.

Next, prompt the user for an amount of cents, read that information as an integer, and store it in the appropriate variable.

Compile and run your program.

A sample run might look like this (user input in red):

Enter dollars: 57 
Enter cents: 49

57 dollars and 49 cents is:
2 twenty dollar bills
1 ten dollar bills
1 five dollar bills
2 one dollar bills
1 quarters
2 dimes
0 nickels
4 pennies

This program does the same basic operation as the last lab, however, it will produce different output depending on the user's input. This program is more valuable because the output may change without making a change to the code. You have made this program more usable.

 

Lab Challenge (Optional)

Don't attempt this unless you have done some reading on your own about casting.

Working with doubles can be problematic because you will have to deal with roundoff error, because of the way doubles must be stored in computer memory. If we can solve a problem using ints, it's usually a better choice because integer math is clean and exact. But sometimes, we must use doubles, or, it would be nice to. For an extra challenge, convert your program to read in a decimal number.

Prompt the user only once for dollars and cents, and read the amount as a floating point number (of type double), then report bills and coins needed to make that amount. A run of this program might look like this:

Enter an amount of money: 57.49

$57.49 is:
     2 twenty dollar bills
     1 ten dollar bills
     1 five dollar bills
     2 one dollar bills
     1 quarters
     2 dimes
     0 nickels
     4 pennies

 

Submit Your Assignment

Submit your source code file, Money.java, to the Lab Submission Folder on D2L.