1) Write a program that prompts (the user) for and reads a double value represen
ID: 3669114 • Letter: 1
Question
1)
Write a program that prompts (the user) for and reads a double value representing a monetary amount. Then determine the fewest number of each bill and coin needed to represent that amount, starting with the highest (assume that a 10-dollar bill is the maximum size needed). For example, if the value entered is $47.63, then the program should print the equivalent amount as:
4 ten dollar bill(s)
1 five dollar bill(s)
2 one dollar bill(s)
2 quarter(s)
1 dime(s)
0 nickel(s)
3 pennies
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.Scanner; // to be able to read from the keyboard
public class money
{
public static void main(String [] args)
{
double amount; //This displays user's amount
int one, five, ten;
int penny, nickel, dime, quarter;
// Create a Scanner object to read input.
Scanner kb = new Scanner(System.in);
// Get the user's amount
System.out.print("What is the amount? ");
amount = kb.nextDouble();
// Calculations
ten = (int) amount/10;
amount = amount%10;
five = (int) amount/5;
amount = amount%5;
amount/1;
amount = amount%1;
quarter = (int) (amount/0.25);
amount = amount%0.25;
dime = (int) (amount/0.10);
amount = amount%0.10;
nickel = (int) (amount/0.5);
amount = amount%0.5;
penny = (int) (amount/0.01);
amount = amount%0.01;
// Display the results
System.out.println(ten + "tens. ");
System.out.println(five + "fives. ");
System.out.println(one + "ones. ");
System.out.println(quarter + "quarters. ");
System.out.println(dime + "dimes. ");
System.out.println(nickel + "nickels. ");
System.out.println(penny + "pennies. ");
}
}
This is the code I have, but in output the pennies are shown as 2 pennies instead of 3.
What is the amount? [26.63]
2tens.
1fives.
1ones.
2quarters.
1dimes.
0nickels.
2pennies.
2) Write an applet that draws the Olympic logo. The circles in the logo should be colored, from left to right, blue, yellow, black, green and red.
Link to the logo picture: https://www.google.com/search?q=olympic+logo&rls=com.microsoft:en-us:IE-Address&tbm=isch&imgil=dghxErH3AQe26M%253A%253BhW3GG-pc8tta8M%253Bhttp%25253A%25252F%25252Flogodatabase.net%25252Frothschild%2525252Bolympic%2525252Blogo&source=iu&pf=m&fir=dghxErH3AQe26M%253A%252ChW3GG-pc8tta8M%252C_&usg=__M0ntbvtloxRSx_dDo9bXRpF6O_Q%3D&biw=984&bih=633&ved=0ahUKEwj5odGV-f_KAhUFWCYKHW_TDwEQyjcIMg&ei=iQHFVrm5HoWwmQHvpr8I#imgrc=dghxErH3AQe26M%3A
Some additional guidelines for this problem
You will find that the loops overlap and hide parts of another ring. You should minimize the amount of hidden rings.
Sometimes the applet viewer will start off blank. Resize the applet window redraws the image and makes it visible
Make the rings an appropriate size to fit the original size of the applet window. That is, don’t make them too large or too small.
Explanation / Answer
Hi , I have corrected first part. I do not have idea about second part.
import java.util.Scanner; // to be able to read from the keyboard
public class Money
{
public static void main(String [] args)
{
String amountStr;//This displays user's amount
int decimalPart,fractionPart;
int one, five, ten;
int penny, nickel, dime, quarter;
// Create a Scanner object to read input.
Scanner kb = new Scanner(System.in);
// Get the user's amount
System.out.print("What is the amount? ");
amountStr = kb.next(); // reading as String
String s[] = amountStr.split("\."); //breaking around '.'
decimalPart = Integer.parseInt(s[0].trim()); // getting first part
fractionPart =Integer.parseInt(s[1].trim()); // getting second part after decimal point
// Calculations
ten = (int) decimalPart/10;
decimalPart = decimalPart%10;
five = (int) decimalPart/5;
decimalPart = decimalPart%5;
>
quarter = (int) (fractionPart/25);
fractionPart = fractionPart%25;
dime = (int) (fractionPart/10);
fractionPart = fractionPart%10;
nickel = (int) (fractionPart/5);
fractionPart = fractionPart%5;
penny = fractionPart;
// Display the results
System.out.println(ten + "tens. ");
System.out.println(five + "fives. ");
System.out.println(one + "ones. ");
System.out.println(quarter + "quarters. ");
System.out.println(dime + "dimes. ");
System.out.println(nickel + "nickels. ");
System.out.println(penny + "pennies. ");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.