Question

In: Computer Science

The user will input a dollar amount as a floating point number; read it from input...

The user will input a dollar amount as a floating point number; read it from input using a Scanner and the .nextDouble() method. Convert the number of dollars into a number of whole (integer) cents, e.g., $1.29 = 129 cents. Determine and output the optimal number of quarters, dimes, nickels, and pennies used to make that number of cents.

Ex: if the input is

1.18

The output should be

4
1
1
3

for 4 quarters, 1 dime, 1 nickel, 3 pennies.

Hints

  1. Remember that a double multiplied by an int gives back a double. You will need to cast the result to int if you want to do integer arithmetic, like modulo.
  2. % is modulo.
  3. There is no "f-string" equivalent in Java.

(STARTING CODE)

import java.util.Scanner;

public class Change {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

/* Type your code here. */
}
}

Solutions

Expert Solution

Hello,

Please find the below code and inline comments:

import java.util.Scanner;

public class Change {
   public static void main(String[] args) {
       int leftover; /* used to hold amount as it decreases */
       int numquarters; /* how many quarters */
       int numdimes; /* how many dimes */
       int numnickels; /* how many nickels */
       int numpennies; /* how many pennies */

       Scanner input = new Scanner(System.in);

       double dollars; // Total value of all the coins, in dollars.

       /* Ask the user for the number of each type of coin. */

       System.out.print("Enter the dollars? :");
       dollars = input.nextDouble();
       int cents = (int) Math.round(100 * dollars);
//       System.out.println(cents);
       leftover = cents;

       numquarters = leftover / 25;
       leftover = leftover % 25;

       /* computer the number of dimes and how much is left over */
       numdimes = leftover / 10;
       leftover = leftover % 10;
       /* computer the number of nickels and how much is left over */
       numnickels = leftover / 5;
       leftover = leftover % 5;
       /* whatever is left over is the number of pennies */
       numpennies = leftover;
       /* print the result */
       System.out.println(numquarters);
       System.out.println(numdimes);
       System.out.println(numnickels);
       System.out.println(numpennies);

   }
}

Project Structure:

Test Result:

Let me know if you have any doubts in the comments sections.

Thanks


Related Solutions

JAVA Write a program that translates a user-input floating-point number between 0 and 4 into the...
JAVA Write a program that translates a user-input floating-point number between 0 and 4 into the closest letter grade. For example, the number 2.8 (which might have been the average of several grades) would be converted to B–. Break ties in favor of the better grade; for example 2.85 should be a B. NOTE: Your implementation must include the definition/implementation and use/calling of the method numberGradeToLetterGrade that is passed a double numeric grade to its parameter variable, and returns the...
Write a program that asks the user to input a set of floating-point values. When the...
Write a program that asks the user to input a set of floating-point values. When the user enters a value that is not a number, give the user a second chance to enter the value. After two chances, quit reading input. Add all correctly specified values and print the sum when the user is done entering data. Use exception handling to detect improper inputs.5 pts Your code with comments A screenshot of the execution Test Case:       Enter float: 1.0...
Read three integers from user input.
Read three integers from user input.
Convert the following floating-point number (stored using IEEE floating-point standard 754) to a binary number in...
Convert the following floating-point number (stored using IEEE floating-point standard 754) to a binary number in non-standard form. 0100_0001_1110_0010_1000_0000_0000_0000
Write a program that read the input from user and convert it to binary and hex...
Write a program that read the input from user and convert it to binary and hex in C language.
1. Read a line of input from the user (as a string) and find out if...
1. Read a line of input from the user (as a string) and find out if there are any vowels in the string. Use a break or continue keyword (whichever is appropriate) to notify that a vowel has been found. Prompt for another string and search again until the user enters 'exit' into the program. 2. Ask the user how many random numbers they would like to see. Ask the user to provide the lowest number they would like to...
//Java Language Read an integer number from the user. If the number is not positive, change...
//Java Language Read an integer number from the user. If the number is not positive, change its sign.    1   Count the digits of the number 2   Count the odd digits of the number 3   Calculate the sum of the digit values of the number
3. IEEE Floating Point Representation What decimal number does the 32-bit IEEE floating point number 0xC27F0000...
3. IEEE Floating Point Representation What decimal number does the 32-bit IEEE floating point number 0xC27F0000 represent? Fill in the requested information in the blanks below. What is the sign of the number (say positive or negative): What is the exponent in decimal format: What is the significand in binary: What is the value of the stored decimal number in decimal (final answer): Credit will be given for your final answer in the blanks and the work shown below.
Assume that you have a 12-bit floating point number system, similar to the IEEE floating point...
Assume that you have a 12-bit floating point number system, similar to the IEEE floating point standard, with the format shown below and a bias of 7. The value of a floating point number in this system is represented as    FP = (-1)^S X 1.F X 2^(E-bias) for the floating point numbers A = 8.75 and B = -5.375. The binary representation of A is given as A = 0101 0000 1100 Show the hexidecimal representation of B.
Using the simple model for representing binary floating point numbers A floating-point number is 14 bits...
Using the simple model for representing binary floating point numbers A floating-point number is 14 bits in length. The exponent field is 5 bits. The significand field is 8 bits. The bias is 15 Represent -32.5010 in the simple model.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT