438,034 Members | 819 Online Need help? Post your question and get tips & solutions from a community of 438,034 IT Pros & Developers. It's quick & easy.

# Need help writting an apllication 2 prompt for and read double value monetary amount

 P: 2 Need help with writting an application that prompt for and read a double value representing a monetary amount. -------------------------------------------------------------------------------- Hello all, I am new to this Java stuff and, I need help in writting an application will prompt for and read a double value representing a monetary amount.Then determine the fewest numbers of each bill and coin needed to represent that amount, starting with the highest (maximum size needed is a ten dollar bill). For example if the value entered is 47.63 then the program should print: 4 ten dollar bills 1 five dollar bills 2 one dollar bills 2 quarters 1 dimes 0 nickels 3 pennies Help ASAP!!!!!!!!!!!!!!!!!!! Jan 23 '07 #1
6 Replies

 10K+ P: 13,264 Need help with writting an application that prompt for and read a double value representing a monetary amount. -------------------------------------------------------------------------------- Hello all, I am new to this Java stuff and, I need help in writting an application will prompt for and read a double value representing a monetary amount.Then determine the fewest numbers of each bill and coin needed to represent that amount, starting with the highest (maximum size needed is a ten dollar bill). For example if the value entered is 47.63 then the program should print: 4 ten dollar bills 1 five dollar bills 2 one dollar bills 2 quarters 1 dimes 0 nickels 3 pennies Help ASAP!!!!!!!!!!!!!!!!!!! First you need to have an algorithm for this. The most common and perhaps most efficient for this problem is the greedy algorithm. Look it up for the coin problem and understand it. This part has nothing to do with Java and can be found in any material that discusses algorithms. After that you now want to convert your algorithm into a working Java program. This should be easy once the algorithm is right. Just post whatever you will have done and we will be able to help you. Jan 23 '07 #2

 P: 2 Hello all, I am new to this Java stuff and, I need help in writting an application will prompt for and read a double value representing a monetary amount.Then determine the fewest numbers of each bill and coin needed to represent that amount, starting with the highest (maximum size needed is a ten dollar bill). For example if the value entered is 47.63 then the program should print: 4 ten dollar bills 1 five dollar bills 2 one dollar bills 2 quarters 1 dimes 0 nickels 3 pennies This is what I have so far but not sure if I am on the right track. Can any one Help? Expand|Select|Wrap|Line Numbers public class Ch02prog1 {       /**      * @param args      */     public static void main(String[] args) {         //Write an application that prompts for and read          //a double value representing a monetary amount.       }     Scanner Scan = new Scanner(System.in);     int value = Scan.nextInt();     double value =scan.nextdouble();       System.Out.Println(value/1000);     system.out.println(value%/100);     system.out.println(value%/10);     system.out.println(value%/1);       String FirstDigit= Scan.next();     String SecondDigit= Scan.next();     String ThirdDigit= Scan.next();     String FourthDigit=Scan.next();     int Result=       System.out.println(Result);   } Jan 23 '07 #3

 10K+ P: 13,264 Hello all, I am new to this Java stuff and, I need help in writting an application will prompt for and read a double value representing a monetary amount.Then determine the fewest numbers of each bill and coin needed to represent that amount, starting with the highest (maximum size needed is a ten dollar bill). For example if the value entered is 47.63 then the program should print: 4 ten dollar bills 1 five dollar bills 2 one dollar bills 2 quarters 1 dimes 0 nickels 3 pennies This is what I have so far but not sure if I am on the right track. Can any one Help? Expand|Select|Wrap|Line Numbers public class Ch02prog1 {       /**      * @param args      */     public static void main(String[] args) {         //Write an application that prompts for and read          //a double value representing a monetary amount.       }     Scanner Scan = new Scanner(System.in);     int value = Scan.nextInt();     double value =scan.nextdouble();       System.Out.Println(value/1000);     system.out.println(value%/100);     system.out.println(value%/10);     system.out.println(value%/1);       String FirstDigit= Scan.next();     String SecondDigit= Scan.next();     String ThirdDigit= Scan.next();     String FourthDigit=Scan.next();     int Result=       System.out.println(Result);   } Wow. You are definitely not cruising on the right path. Now I'm going to post a very lame attempt at this. The program works but is not precisely correct. The idea is that I want to show you how to write such a program. So I want you to go through it line by line and ask where you do not understand then maybe together we can improve it until it becomes precisely correct. Expand|Select|Wrap|Line Numbers import java.util.Scanner; //You have to import Scanner if you want to use it class One {     public static void main(String[] args) {                     Scanner scanner = new Scanner(System.in);         System.out.print("Enter the amount: ");         double amount = scanner.nextDouble(); //Java is case sensitive so nextDouble() != nextdouble         //You set the coins and/or notes denominations here                         // 1c     5c  10c  25c   50c  \$1  \$5 \$10         double[] coins = {0.01, 0.05, .1,  0.25, 0.5,  1,  5, 10}; //Put your denominations here         double[] soln = new double[coins.length];         double current = 0.0;         int i = coins.length - 1;         while(i >= 0) {             double val = coins[i];             while((val + current) <= amount) {                 current = current + val;                 soln[i] = soln[i] + coins[i];             }             i--;         }         System.out.println(current); // Check this value for amounts containing .1         for(int j = 0;j < coins.length; j++) {             System.out.println(coins[j] + " : " + soln[j] / coins[j]);         }       } }   Jan 24 '07 #4

 P: 6 Wow. You are definitely not cruising on the right path. Now I'm going to post a very lame attempt at this. The program works but is not precisely correct. The idea is that I want to show you how to write such a program. So I want you to go through it line by line and ask where you do not understand then maybe together we can improve it until it becomes precisely correct. Expand|Select|Wrap|Line Numbers import java.util.Scanner; //You have to import Scanner if you want to use it class One {     public static void main(String[] args) {                     Scanner scanner = new Scanner(System.in);         System.out.print("Enter the amount: ");         double amount = scanner.nextDouble(); //Java is case sensitive so nextDouble() != nextdouble         //You set the coins and/or notes denominations here                         // 1c     5c  10c  25c   50c  \$1  \$5 \$10         double[] coins = {0.01, 0.05, .1,  0.25, 0.5,  1,  5, 10}; //Put your denominations here         double[] soln = new double[coins.length];         double current = 0.0;         int i = coins.length - 1;         while(i >= 0) {             double val = coins[i];             while((val + current) <= amount) {                 current = current + val;                 soln[i] = soln[i] + coins[i];             }             i--;         }         System.out.println(current); // Check this value for amounts containing .1         for(int j = 0;j < coins.length; j++) {             System.out.println(coins[j] + " : " + soln[j] / coins[j]);         }       } }   Can you please decribe exact what each section is doing and the purpose of the array? Im very new to java and am trying to understand this problem in terms of translating the algorithm in java format. Sep 9 '08 #5

 Expert 2.5K+ P: 3,112 Can you please decribe exact what each section is doing and the purpose of the array? Im very new to java and am trying to understand this problem in terms of translating the algorithm in java format. OK, it's not my code, but I'll go through it with you. Expand|Select|Wrap|Line Numbers import java.util.Scanner; //You have to import Scanner if you want to use it As the comment (that text behind "//") says, the external class Scanner which is in the package java.util has to be imported, so that our program knows what it is. You could also have an own class called Scanner, that does something completely else, say in org.mystuff.hardware (I just invented that one). Expand|Select|Wrap|Line Numbers class One {     ... } This opens and closes a class called "One". Expand|Select|Wrap|Line Numbers public static void main(String[] args) {     ... } This is the main method. When you run your application, the main method you point to is called. Everything else must be started from here. Expand|Select|Wrap|Line Numbers                     Scanner scanner = new Scanner(System.in); Here we are creating a new Object from the (imported) class Scanner. As you can see in it's API Documentation, the Scanner class has a constructor which takes an InputStream. System.in is the standard input. Expand|Select|Wrap|Line Numbers         System.out.print("Enter the amount: "); Prints the String "Enter the amount: " to the standard output (normally a console window). Expand|Select|Wrap|Line Numbers         double amount = scanner.nextDouble(); //Java is case sensitive so nextDouble() != nextdouble Here, our Scanner object reads a double value. Expand|Select|Wrap|Line Numbers //You set the coins and/or notes denominations here                 // 1c     5c  10c  25c   50c  \$1  \$5 \$10 double[] coins = {0.01, 0.05, .1,  0.25, 0.5,  1,  5, 10}; //Put your denominations here This is just an array with the given double values. Expand|Select|Wrap|Line Numbers double[] soln = new double[coins.length]; double current = 0.0; Setting some variables, that we'll need later Expand|Select|Wrap|Line Numbers int i = coins.length - 1; while(i >= 0) {     double val = coins[i];     while((val + current) <= amount) {         current = current + val;         soln[i] = soln[i] + coins[i];     }     i--; } This is a while loop with another nested while loop inside. The outer loop could have been a for loop too. What happens is: The variable val is created with the value of the i-th coin in the array. Then the while loop checks, if the current amount of money is still small enough to add whatever coin you're using at the moment and still be within the amount given by the user. If so, the current coin is added to the current amount and the variable [i]soln is risen by the value of that coin. This way, the array will show you how much money was paid with by which type of coin. Last but not least, the counter i is decremented (=> i = i - 1). Expand|Select|Wrap|Line Numbers         System.out.println(current); // Check this value for amounts containing .1 for(int j = 0;j < coins.length; j++) {     System.out.println(coins[j] + " : " + soln[j] / coins[j]); } Basically print out your results. There, I hope that helps you understand the program and Java in general. Greetings, Nepomuk Sep 9 '08 #6

 P: 6 Thankyou. That helped my understanding immensly. Sep 9 '08 #7 