Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab 2: Counting Coins and Making Change The Coins Class Objectives Use methods.

ID: 3880105 • Letter: L

Question

Lab 2: Counting Coins and Making Change

The Coins Class

Objectives

Use methods.

Use local variables.

Use arithmetic expressions.

Use Scanner to input values.

Use a class constant.

Hand-in Requirements

All projects and laboratories will be submitted electronically through Blackboard. Zip up your entire lab directory to submit as the source. (Right click on the lab folder and follow Send To > Compressed (zipped) Folder or 7-Zip > Add to "lab2.zip"). The lab folder should include the following:

Coins.java

CoinsOutput.txt

Tasks

Write a program that prints

and calls two methods:

Input the number of quarters, dimes, nickels, and pennies from the user. Print out the number of coins and total value in dollars.

Input the number of cents from the user. Determine and print out the number of quarters, dimes, nickels, and pennies to add up to that number of cents. No, you can't use all pennies.

Details

Getting Input from the User

You can get input from the user using a Scanner object. Here is an example program with key lines in bold.

The import statement tells Java that we want to use Java's java.util package (Scanner is part of this package).

The statement:

declares a special kind of variable (a class constant) named CONSOLE of type Scanner and stores an "object" for input from the keyboard in CONSOLE.  CONSOLE can be used anywhere in the class.

The first two statements of the halfDollarAmount method:

prompt the user for information, and

input a number from the keyboard and stores the number in a variable named halfDollar.

You only need the declaration/assignment for CONSOLE once in the class. On the other hand, you need prompt and input statements for each value you want the user to enter.

Local Variables

You should have local variables for each value that is entered and each value that you calculate. For example, for the first method there should be a local variable that stores the total dollar amount of the change, as well as a local variable for the total number of coins.

Making Change

For the second method, we can use integer division and the mod operator to make change. Suppose the user enters 99 as the number of cents. The sequence of calculations should be as follows.

The integer division 99 / 25 can be used to determine the number of quarters (3).

Using the mod operator 99 % 25 determines the remaining number of cents to be converted into change (24).

Integer division 24 / 10 can be used again to determine the number of dimes (2).

The mod operator 24 % 10 can be used again to determine the remaining amount (4).

Next, for nickels, integer division 4 / 5 for the number of nickels (0) and a mod operation 4 % 5 for the remaining amount (4).

Whatever is left is the number of pennies (4).

Of course the values that were calculated (3, 24, 2, 4, 0, 4) would be different if the user enters something different from 99. This means we should have variables to store all of these values. For example, we might have a variable named cents to hold the value that the user entered, a variable named remainingAfterQuarters for the remaining amount after the quarters are determined, which could be assigned by:

Output File

Include the output of your program in a file named CoinsOutput.txt.

Rubric

Your program should compile without any errors. A program with more than one or two compile errors will likely get a zero for the whole assignment.

The following criteria will also be used to determine the grade for this assignment:

[1 Point] If the main method of your program prints "Lab 2 written by [...]".

[1 Point] If your submission was a Zip file named lab2.zip containing a folder named lab2, which contains the other files.

[1 Point] If your Java program was in a file named Coins.java.

[1 Point] If the output of your Java program was in a file named CoinsOutput.txt.

[1 Point] If your program contains a comment that describes what the program does and contains a comment for each method.

[1 Point] If your program is indented properly.

[7 points] If your main method calls a method that does the following:

[3 Points] If it prompts the user for the number of quarters, dimes, nickels, and pennies.

[4 Points] If it prints out the total number of coins and their value in dollars.

[7 points] If your main method calls another method that does the following:

[3 Points] If it prompts the user for the number of cents.

[4 Points] If it prints out the number of quarters, dimes, nickels, and pennies necessary to add up to that number.

Explanation / Answer

program to convert coins to number of quarters, dimes, nickels, and pennies and vice versa.

coding:

import java.util.Scanner;

public class Coins {

public static final Scanner CONSOLE = new Scanner(System.in);

public static void main(String[] args) {
//program name to print in the console
System.out.print(" lab 2 Written by ....");
tocoins(); //calls the method to convert quarters, dimes, nickels ans pennies to coins
coinstopennies(); // calls the method to convert coins to quarters, dimes, nickels ans pennies

}

public static void tocoins() {
//prompt the user to enter the quarter dimes nickel pennies
System.out.print(" Enter the number of quarters : ");
int quarters = CONSOLE.nextInt();
System.out.print(" Enter the number of dimes : ");
int dimes = CONSOLE.nextInt();
System.out.print(" Enter the number of nickels : ");
int nickel = CONSOLE.nextInt();
System.out.print(" Enter the number of pennies : ");
int pennies = CONSOLE.nextInt();
float coins;
//convert into coins
coins = (float) ((quarters * .25) + (dimes * .10) + (nickel * .5) + (pennies * .1));
System.out.println(" coins is :" + coins); //display the coins

}
//method to convert coins to quarters, dimes, nickels ans pennies

public static void coinstopennies() {
System.out.println("Enter total number of cents (positive integer): ");
int cents = CONSOLE.nextInt();
System.out.println();

// compute total amount of quarter, dimes, nickels, and pennies
int Quarters = cents / 25;
int remainingafterquarter = cents % 25;
int Dimes = remainingafterquarter / 10;
int remainingafterdimes = remainingafterquarter % 10;
int Nickels = remainingafterdimes / 5;
int remainingafternickel = remainingafterdimes % 5;

// display resulting number of quarters, dimes, nickels ans pennies inthe coin
System.out.print(" For your total cents of " + cents);
System.out.println(" you have:");

System.out.println("#quarters = " + Quarters);

System.out.println("#dimes = " + Dimes);

System.out.println("#nickels = " + Nickels);

System.out.println("#Pennies = " + remainingafternickel);

}
}

output:

run:
lab 2 Written by ....Enter the number of quarters : 300
Enter the number of dimes : 40
Enter the number of nickels : 20
Enter the number of pennies : 30
coins is :92.0
Enter total number of cents (positive integer):
99

For your total cents of 99 you have:
#quarters = 3
#dimes = 2
#nickels = 0
#Pennies = 4
BUILD SUCCESSFUL (total time: 44 seconds)