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

Tasks Write a program that prints and calls two methods: 1. Input the number of

ID: 3785545 • Letter: T

Question

Tasks

Write a program that prints

and calls two methods:

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

2. 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.

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.

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.

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

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

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

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

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

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

Explanation / Answer


// Lab2.java
import java.util.*;

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

public static void totalDollars( )
{
    System.out.print("Enter the number quarters: ");
    int quarters = CONSOLE.nextInt( );
    System.out.print("Enter the number dimes: ");
    int dimes = CONSOLE.nextInt( );
    System.out.print("Enter the number nickels: ");
    int nickels = CONSOLE.nextInt( );
    System.out.print("Enter the number pennies: ");
    int pennies = CONSOLE.nextInt( );

    double amount = (quarters*25.0 + dimes*10.0 + nickels*5.0 + pennies*1.0)/100;
    System.out.println("Total amount: $" + amount);
}

public static void centsToOther( )
{
    System.out.print(" Enter the number of cents: ");
    int cents = CONSOLE.nextInt();

    int quarters = cents/25;
    int remaining = cents - quarters*25;
    int dimes = remaining/10;
    remaining = remaining - dimes*10;
    int nickels = remaining/5;
    int pennies = remaining - nickels*5;

    System.out.println("Quarters: " + quarters + " Dimes: " + dimes + " Nickels: " + nickels + " Pennies: " + pennies);
  
}

public static void main(String[] args)
{
      totalDollars();
      centsToOther();
}


}

/*
output:

Enter the number quarters: 7
Enter the number dimes: 12
Enter the number nickels: 5
Enter the number pennies: 10
Total amount: $3.3

Enter the number of cents: 99
Quarters: 3
Dimes: 2
Nickels: 0
Pennies: 4


*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote