Lab Activity 4 – Make change Create a new class called MonetaryChange and define
ID: 3889981 • Letter: L
Question
Lab Activity 4 – Make change
Create a new class called MonetaryChange and define a static main method as follows:
Declare local variables for quarters, dimes, nickels, and pennies.
Declare a local variable called totalCents to hold the total value for a collection of coins.
Declare a local variable called remainderInCents.
Create an instance of the Scanner class, called keyboard, to read totalCents from the keyboard.
Include statements to:
initialize remainderInCents to totalCents.
assign to quarters the largest number of quarters that can be dispensed for remainderInCents.
assign a new value to remainderInCents that would be the value left after the current remainderInCents would be dispensed as quarters.
calculate dimes and recalculate remainderInCents.
calculate nickels and recalculate remainderInCents.
calculate pennies and recalculate remainderInCents.
print the value of totalCents to the BlueJ Terminal window.
print the number of quarters, dimes, nickels, and pennies.
Explanation / Answer
MonetaryChangeTest.java
import java.util.Scanner;
public class MonetaryChangeTest {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the total cents: ");
int totalCents = keyboard.nextInt();
MonetaryChange m = new MonetaryChange(totalCents);
System.out.println("Total number of cents: "+m.getTotalCents());
System.out.println("Quarters: "+m.getQuarters());
System.out.println("Dimes: "+m.getDimes());
System.out.println("Nickels: "+m.getNickels());
System.out.println("Pennies: "+m.getPennies());
}
}
MonetaryChange.java
public class MonetaryChange {
private int quarters, dimes, nickels, pennies, totalCents , remainderInCents;
public MonetaryChange(int totalCents) {
this.totalCents = totalCents;
remainderInCents = totalCents;
quarters = remainderInCents/25;
remainderInCents = remainderInCents%25;
dimes = remainderInCents/10;
remainderInCents = remainderInCents%10;
nickels = remainderInCents/5;
remainderInCents = remainderInCents%5;
pennies = remainderInCents;
}
public int getQuarters() {
return quarters;
}
public int getDimes() {
return dimes;
}
public int getNickels() {
return nickels;
}
public int getPennies() {
return pennies;
}
public int getTotalCents() {
return totalCents;
}
public int getRemainderInCents() {
return remainderInCents;
}
}
Output:
Enter the total cents:
42
Total number of cents: 42
Quarters: 1
Dimes: 1
Nickels: 1
Pennies: 2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.