Integers & Variables
Ok so this project calculates what notes you require to make a certain value.
For example, what notes do I need to make £75, the answer is one £50 note, a £20 note and a £5 note.
- Create a new project and class with the names of BankTeller
- Paste the code below into the code view
- Edit the "int sterling" value to something of your choice
- Run the application
- The console will tell you what notes are required to make that certain value
Bank Teller Code
[java]class BankTeller
{
public static void main ( String args [ ] )
{
//declare and initialise variables
int sterling = 189;
String indent = " " ;
int fifties, twenties, tens, ones ;
ones = sterling;
//perform calculations
fifties = ones / 50 ;
ones = ones % 50 ;
twenties = ones / 20;
ones = ones % 20 ;
tens = ones / 10 ;
ones = ones % 10 ;
//output results
System.out.println( "The notes are: " ) ;
System.out.println( ) ;
System.out.println( indent + "Fifties: " + fifties ) ;
System.out.println( indent + "Twenties: " + twenties ) ;
System.out.println( indent + "Tens: " + tens ) ;
System.out.println( indent + "One pound coins : " + ones ) ;
System.out.println( ) ;
System.out.println( "Total amount: " + sterling + " pounds" ) ;
}
}[/java]
Explaination
This snippet once again makes use of the print line function. I dont think i need to explain this one again. The calculations may need some explaining though.
Basically to work out how many fifties you require, it does the sterling value (189) divided by 50. Which is 3.78, but its a integer value so its actually 3 (3 fifties are required)
It then works out what the remainder is from that last calculation by using the % which I explained in the last tutorial. 39 is the remainder.
The calculations work on from there, so how many twenties go into 39? 1 twenty, 19 is the remainder. How many tens go into 19? 1 ten, 9 is the remainder etc etc.
Challenge
Ok so the BankTeller code tells the BankTeller what notes he requires to make a certain value.
The current code is missing the GBP currency note of Five pounds.
So just look at the code and see if you can modify the code so that it includes five pounds, aswell as the rest already there.
(There should be £50, £20, £10, £5, £1)
Everyone who PMs me the correct working code earns themselves 500 bytes
Thats It! More Tutorials to Come