473,837 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

2 New Member
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 9236
r035198x
13,262 MVP
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
Java1963
2 New Member
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
  1. public class Ch02prog1 {
  2.  
  3.     /**
  4.      * @param args
  5.      */
  6.     public static void main(String[] args) {
  7.         //Write an application that prompts for and read 
  8.         //a double value representing a monetary amount.
  9.  
  10.     }
  11.     Scanner Scan = new Scanner(System.in);
  12.     int value = Scan.nextInt();
  13.     double value =scan.nextdouble();
  14.  
  15.     System.Out.Println(value/1000);
  16.     system.out.println(value%/100);
  17.     system.out.println(value%/10);
  18.     system.out.println(value%/1);
  19.  
  20.     String FirstDigit= Scan.next();
  21.     String SecondDigit= Scan.next();
  22.     String ThirdDigit= Scan.next();
  23.     String FourthDigit=Scan.next();
  24.     int Result= 
  25.  
  26.    System.out.println(Result);
  27.  
  28. }
Jan 23 '07 #3
r035198x
13,262 MVP
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
  1. public class Ch02prog1 {
  2.  
  3.     /**
  4.      * @param args
  5.      */
  6.     public static void main(String[] args) {
  7.         //Write an application that prompts for and read 
  8.         //a double value representing a monetary amount.
  9.  
  10.     }
  11.     Scanner Scan = new Scanner(System.in);
  12.     int value = Scan.nextInt();
  13.     double value =scan.nextdouble();
  14.  
  15.     System.Out.Println(value/1000);
  16.     system.out.println(value%/100);
  17.     system.out.println(value%/10);
  18.     system.out.println(value%/1);
  19.  
  20.     String FirstDigit= Scan.next();
  21.     String SecondDigit= Scan.next();
  22.     String ThirdDigit= Scan.next();
  23.     String FourthDigit=Scan.next();
  24.     int Result= 
  25.  
  26.    System.out.println(Result);
  27.  
  28. }
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
  1. import java.util.Scanner; //You have to import Scanner if you want to use it
  2. class One {
  3.     public static void main(String[] args) {
  4.                     Scanner scanner = new Scanner(System.in);
  5.         System.out.print("Enter the amount: ");
  6.         double amount = scanner.nextDouble(); //Java is case sensitive so nextDouble() != nextdouble
  7.         //You set the coins and/or notes denominations here
  8.                         // 1c     5c  10c  25c   50c  $1  $5 $10
  9.         double[] coins = {0.01, 0.05, .1,  0.25, 0.5,  1,  5, 10}; //Put your denominations here
  10.         double[] soln = new double[coins.length];
  11.         double current = 0.0;
  12.         int i = coins.length - 1;
  13.         while(i >= 0) {
  14.             double val = coins[i];
  15.             while((val + current) <= amount) {
  16.                 current = current + val;
  17.                 soln[i] = soln[i] + coins[i];
  18.             }
  19.             i--;
  20.         }
  21.         System.out.println(current); // Check this value for amounts containing .1
  22.         for(int j = 0;j < coins.length; j++) {
  23.             System.out.println(coins[j] + " : " + soln[j] / coins[j]);
  24.         }
  25.  
  26.     }
  27. }
  28.  
Jan 24 '07 #4
meekstro
6 New Member
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
  1. import java.util.Scanner; //You have to import Scanner if you want to use it
  2. class One {
  3.     public static void main(String[] args) {
  4.                     Scanner scanner = new Scanner(System.in);
  5.         System.out.print("Enter the amount: ");
  6.         double amount = scanner.nextDouble(); //Java is case sensitive so nextDouble() != nextdouble
  7.         //You set the coins and/or notes denominations here
  8.                         // 1c     5c  10c  25c   50c  $1  $5 $10
  9.         double[] coins = {0.01, 0.05, .1,  0.25, 0.5,  1,  5, 10}; //Put your denominations here
  10.         double[] soln = new double[coins.length];
  11.         double current = 0.0;
  12.         int i = coins.length - 1;
  13.         while(i >= 0) {
  14.             double val = coins[i];
  15.             while((val + current) <= amount) {
  16.                 current = current + val;
  17.                 soln[i] = soln[i] + coins[i];
  18.             }
  19.             i--;
  20.         }
  21.         System.out.println(current); // Check this value for amounts containing .1
  22.         for(int j = 0;j < coins.length; j++) {
  23.             System.out.println(coins[j] + " : " + soln[j] / coins[j]);
  24.         }
  25.  
  26.     }
  27. }
  28.  
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
Nepomuk
3,112 Recognized Expert Specialist
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
  1. 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.har dware (I just invented that one).
Expand|Select|Wrap|Line Numbers
  1. class One {
  2.     ...
  3. }
This opens and closes a class called "One".
Expand|Select|Wrap|Line Numbers
  1. public static void main(String[] args) {
  2.     ...
  3. }
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
  1.                     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
  1.         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
  1.         double amount = scanner.nextDouble(); //Java is case sensitive so nextDouble() != nextdouble
Here, our Scanner object reads a double value.
Expand|Select|Wrap|Line Numbers
  1. //You set the coins and/or notes denominations here
  2.                 // 1c     5c  10c  25c   50c  $1  $5 $10
  3. 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
  1. double[] soln = new double[coins.length];
  2. double current = 0.0;
Setting some variables, that we'll need later
Expand|Select|Wrap|Line Numbers
  1. int i = coins.length - 1;
  2. while(i >= 0) {
  3.     double val = coins[i];
  4.     while((val + current) <= amount) {
  5.         current = current + val;
  6.         soln[i] = soln[i] + coins[i];
  7.     }
  8.     i--;
  9. }
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
  1.         System.out.println(current); // Check this value for amounts containing .1
  2. for(int j = 0;j < coins.length; j++) {
  3.     System.out.println(coins[j] + " : " + soln[j] / coins[j]);
  4. }
Basically print out your results.

There, I hope that helps you understand the program and Java in general.

Greetings,
Nepomuk
Sep 9 '08 #6
meekstro
6 New Member
Thankyou. That helped my understanding immensly.
Sep 9 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
4909
by: nooy66 | last post by:
I need to read value type double form my binary file. When i use Hex Workshop software read my binary file. i have data 8 bytes : hex = 3131 3131 3131 3140 string = 1111111@ Float = 2.5784852e-009 Double = 17.192157 When i use php to read file by code:
14
9824
by: wane | last post by:
Hello, I have heard that one should avoid using float and double in monetary calculation because of the lack of preciseness. What is a good alternative? Thanks
1
2651
by: San | last post by:
Hi, I'm using a Managed C++ project with MFC support. I have an ArrayList pointer object alValue (ArrayList* alValue) to this object i want to add all the elements present in Vector<double> vVector to it.. I'm using this line of code.. alValue->Add (new System::Double(vVector)) and I'm getting the following compilation error..
2
2352
by: java06 | last post by:
Hi I need to do a program that 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 Thanks...
2
2204
by: HarisHohkl | last post by:
Hi, I've this function in a class to update the total value.but when i try to remove the these row highlight in Bold it crash, what should i do???? void display_total_value() { double value; double totalvalue=0; int r, c; setcolor (11); for( r=0; r<row; r++ )
4
1535
by: Max80 | last post by:
Hi Everyone, I have a double value (e.g. 2.00) which I would like to convert to string. Simply returning the value using the .ToString() method trims the 0s after the decimal point. I know that its is possible by doing the following double d1 = 2.00; string strValue; strValue = d1.ToString("#,###.00") However the problem is that I do not know how many 0s there will be after the decimal point before hand. The number...
11
2666
by: xz | last post by:
>From the reference of MSDN about hash map: operator Inserts an element into a hash_map with a specified key value. And such example is given: hash_map <int, inthm1; hm1 = 40;
2
1763
by: blazzer | last post by:
Hi, I need help. I just wondering whether i can set the double value using random class..i even use if statement to set it..but nothing..i wanna set between 0.5 n 0.7 value.. double planetMass = 0.0; double sunMass = 0.0; Random rdm = new Random(); double rand = rdm.nextDouble(); if (rand >= 0.5 && rand <= 0.7){
1
1384
by: Ian Norris | last post by:
I have a button on a form which is used to issue and print a certificate. The certificate contains an expiry date. In most cases I can calculate the expiry date from the current date, but for one case I need to prompt the user for the expiry date. I have tried to do this by creating a form containing just the expiry date field which the user can enter. Unfortunately my issuing code does not wait for the user to enter the expiry date. I'm sure...
0
9843
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9683
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10882
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10630
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10275
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7811
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5670
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5851
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4476
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.