Lab Activity 3 -- Calculate monetary value Create a new class called Money and i
ID: 3889531 • Letter: L
Question
Lab Activity 3 -- Calculate monetary value
Create a new class called Money and include a static main method. Define main by the following:
Declare local variables for quarters, dimes, nickels, and pennies.
Create an instance of the Scanner class, called keyboard, to read data from the keyboard.
Include pairs of statements to:
Prompt the user for the number quarters.
Assign the value entered by the user to the variable quarters.
Etc.
Print the total monetary value of these coins to the BlueJ Terminal window.
Explanation / Answer
import java.util.Scanner;
public class Money {
public static void main(String[] args) {
Scanner scanner = null;
try {
scanner = new Scanner(System.in);
// declaration of local variables
int quarters, dimes, nickels, pennies;
// prompt to enter the money in quarters
System.out.print("Enter the money in Quarters:");
quarters = scanner.nextInt();
// convert to dimes
dimes = (quarters %= 25) / 10;
// convert to nickels
nickels = (quarters %= 10) / 5;
// convert to pennies
pennies = quarters % 5;
// print the the money
System.out.println(dimes + " Dimes, " + nickels + " Nickels, "
+ pennies + " Pennies");
} catch (Exception e) {
// TODO: handle exception
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.