Need A SMART person for this excercise (beginner)
:(handshake):
Write an application that prompts 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.Then determine the fewest number of each bill and coin needed to represent that amount,starting with the highest (assume that a ten dollar bill is the maximum size need).For example , if tbhe value entered is 47.63 (forty-seven dollars and sixty-three cents).then the program should print the equivalent amount as :
4 ten dollar bills
1 five dollar bills
2 one dollar bills
2 quarters
1 dime
0 nickles
3 pennies
Re: Need A SMART person for this excercise (beginner)
Re: Need A SMART person for this excercise (beginner)
This actually sounds fun to code. What do you have completed so far? What do you need help with?
If you need help starting out, you should be thinking about
a)Casting doubles to integers
b)Using modulus to determine remaining amount
Re: Need A SMART person for this excercise (beginner)
Code:
/**
* AWT Sample application
*
* @author
* @version 1.00 12/10/11
*/
public class java {
public static void main(String[] args) {
double MONEY, QUARTERS, DIMES, NICKELS, PENNIES;
int DOLLAR_100, DOLLAR_50, DOLLAR_20, DOLLAR_10, DOLLAR_5, DOLLAR_1;
Scanner Scan = new Scanner(System.in);
System.out.print("Enter the monetary amount in xx.xx: " );
MONEY = Scan.nextFloat();
DOLLAR_100 = (int) (MONEY / 100);
DOLLAR_50 = (int) (MONEY % 100 / 50);
DOLLAR_20 = (int) (MONEY % 100 % 50 / 20);
DOLLAR_10 = (int) (MONEY % 100 % 50 % 20 / 10);
DOLLAR_5 = (int) (MONEY % 100 % 50 % 20 % 10 / 5);
DOLLAR_1 = (int) (MONEY % 100 % 50 % 20 % 10 % 5);
QUARTERS = Math.round( (MONEY % 100 % 50 % 20 % 10 % 5 % 1 / 0.25));
DIMES = Math.round((MONEY % 100 % 50 % 20 % 10 % 5 % 1 % 0.25 / 0.10));
NICKELS = Math.round((MONEY % 100 % 50 % 20 % 10 % 5 % 1 % 0.25 % 0.10 / .05));
PENNIES = Math.round((MONEY % 100 % 50 % 20 % 10 % 5 % 1 % 0.25 % 0.10 % .05 / .01));
System.out.println(DOLLAR_100 + " hundred dollar bills" );
System.out.println(DOLLAR_50 + " fifty dollar bills" );
System.out.println(DOLLAR_20 + " twenty dollar bills" );
System.out.println(DOLLAR_10 + " ten dollar bills" );
System.out.println(DOLLAR_5 + " five dollar bills" );
System.out.println(DOLLAR_1 + " one dollar bills" );
System.out.println((int) QUARTERS + " quarters" );
System.out.println((int) DIMES + " dimes" );
System.out.println((int) NICKELS + " nickels" );
System.out.println((int) PENNIES + " pennies" );
System.out.println("Money left after % by 100: " + MONEY % 100);
}
}
OUTPUT:symbol: class Scanner
location: class java
C:\Users\Windows 7\Documents\JCreator Pro\MyProjects\java\src\java.java:15: error: cannot find symbol
Scanner Scan = new Scanner(System.in);
^
symbol: class Scanner
location: class java
2 errors
Process completed.
Re: Need A SMART person for this excercise (beginner)
Hello. I'm a rank beginner with Java, so be sure to consult an expert.
But I think you need to import the Scanner class -> import java.util.Scanner;
Re: Need A SMART person for this excercise (beginner)
Hi khavanian. When you post code use the "code" tags. Put [code] at the start of the code and [/code] at the end: that way the code will be readable when it appears here.
Quote:
Code:
C:\Users\Windows 7\Documents\JCreator Pro\MyProjects\java\src\java.java:15: error: cannot find symbol
Scanner Scan = new Scanner(System.in);
^
symbol: class Scanner
location: class java
The compiler is saying that it doesn't know what "Scanner" is. (The compiler calls it a "symbol" which it can't "find"). You have to tell the compiler where to find the Scanner class. And you do that with an import statement at the start of the code. Look at any code you have that use scanner and you'll find examples of this.
---
It's a good idea (again for readability) to follow standard Java coding conventions and begin variables with a lowercase letter - scan, money, etc. Classes begin with a capital letter, and both classes and variables should be descriptive. A better name for the class might be ChangeApp. Also compile often so that you can pick up useful messages like this from the compiler as soon as possible.
Re: Need A SMART person for this excercise (beginner)
OH ,YES ,I TOTTALY FORGOT THAT THANK YOU :d
Re: Need A SMART person for this excercise (beginner)
For one, you should really delete the:
Code:
/**
* AWT Sample application
*
* @author
* @version 1.00 12/10/11
*/
So that people might think you actually wrote it. Lol.
All the code is correct, minus what Bogart said, but man is that hard to read/understand. If I didn't know what it was doing, from the instructions, I would be pretty lost. If I were to have written it, it would have followed the format of:
Code:
num10 = (int) total/10;
total = total%10;
System.out.println("10: " + num10);
num5 = (int) total/5;
total = total%5;
System.out.println("5: " + num5);
num1 = (int) total/1;
total = total%1;
System.out.println("1: " + num1
It just seems a lot easier to read, in my eyes.
Re: Need A SMART person for this excercise (beginner)
Whenever you see repeated code the word "loop" should be screaming inside your head.
Re: Need A SMART person for this excercise (beginner)
Quote:
Originally Posted by
Junky
Whenever you see repeated code the word "loop" should be screaming inside your head.
I was thinking because of the randomness of the denominations, that they couldn't be mathematically computed, that it really wasn't an option. I am glad that you pointed, and got me thinking in that direction. You thinking something along the lines of:
Code:
double[] denom = {10, 5, 1, .25, .10, .05, .01};
System.out.println("What is the total amount?");
total = keyboard.nextDouble();
for(int ix=0; ix<denom.length; ix++){
subTot = (int) (total/denom[ix]);
total = total%denom[ix];
if(denom[ix] == .01)
subTot++;
System.out.println(denom[ix] + ": " + subTot);
}
Is this the most optimized way of writing it?
This isn't 'spoonfeeding', as the OP has already found different code to steal. Lol.
Re: Need A SMART person for this excercise (beginner)
Quote:
Is this the most optimized way of writing it?
Maybe for readabilty (because the initialisation is all in one place):
Code:
for(int denom :new int[] {1000, 500, 100, 25, 10, 5, 1}) {
// etc
(I prefer pennies.)
Re: Need A SMART person for this excercise (beginner)
And the advantage of pennies is that you won't bump into floating point maths problems.