What Will I Learn?
Greetings, in this tutorial we will continue to extend the function that we generated in the previous tutorial and learn to write a function capable of calculating the money that we need to pay on our gsm operators.
You will learn the basics of Eclipse Java ide,
You will learn algorithm and function generation,
You will learn method,class and void implementation in Java.
Requirements
Eclipse IDE for java developers,
Interest on coding and applications,
Basic knowledge on Java.
Difficulty
Either choose between the following options:
- Intermediate
Tutorial Contents
To begin with it's good to remeber what we did on our previous tutorial. In the previous tutorail, a function was generated in eclipse by using Java programming language capable of calculatng the minimum amount of banknotes required to withdraw an amount of money from ATM machine. To do that briefly we first asked the user enter his/her money then by using mathematical operations proceed that money into banknotes and finally displayed the overall money.
In today's lecture this idea will be extended into European banknotes, double-fractions and an improved print out display screen. To do that we must first run our former, prepared code. Below is the overall code given, written in the previous task,
import java.util.Scanner;
public class q1 {
public static void main(String[] args) {
int money;
Scanner keyboard =new Scanner (System.in);
System.out.println("Enter the amount of money: ");
money=keyboard.nextInt();
System.out.println("Calculating...");
int x,y,z,a,b;
x=money/20;
y=money%20;
z=y/5;
a=y%5;
b=a+x+z;
System.out.println("# of $20: " +x);
System.out.println("# of $5 : " +z);
System.out.println("# of $1 : " +a);
System.out.println("Total number of banknotes: " +b);
}
}
And the output when this above code was runned is given as,
The very first thing we need to change in this code is the "Enter the amount of money" print line. Since we want our application to be useful we will change it into, "Enter the amount of money in Euro"
System.out.println("Enter the amount of money in Euro: ");
Now we need to change intger defined money value in to double since we want users to enter fractional numbers too.
double money;
Moreover, we need to change the integer definiton of money into double,
money=keyboard.nextDouble();
Now we need to change the double fromat into int to calculate the paper banknote side. In other words we will split remainder and the main money to make the calculation easy.
int moneynew = (int) money;
This code will make the moneynew integer equal to the integer, Convert the user entered double value to integer. By obtaining the main money we can now divide it 500,200,100,50,20,10 and 5 Euroes to calculate how many banknotes we need. To do that below code was written,
x=moneynew/500;
y=moneynew%500;
x1=y/200;
y1=y%200;
x2 = y1/100;
y2 = y1%100;
x3 = y2/50;
y3 = y2%50;
x4 = y3/20;
y4 = y3%20;
x5 = y4/10;
y5 = y4%10;
x6 = y5/5;
y6 = y5%5;
This above code will calculate the minimum amount of 500,200,100,50,20,10 and 5 euroes banknotes required the machine to give the user. Morevoer the last integer y6 symbolizes the last remaing after machine gives the 5 euroes banknote.
Then we need to define the user output panel where the output will be directly related to the uesr entered numbers,
System.out.println("# of €500: " +x);
System.out.println("# of €200: " +x1);
System.out.println("# of €100: " +x2);
System.out.println("# of €50: " +x3);
System.out.println("# of €20: " +x4);
System.out.println("# of €10: " +x5);
System.out.println("# of €5: " +x6);
System.out.println("# of €1: " +y6);
After adding the output panel our overall code became,
import java.util.Scanner;
public class q1 {
public static void main(String[] args) {
double money;
Scanner keyboard =new Scanner (System.in);
System.out.println("Enter the amount of money in Euro: ");
money=keyboard.nextDouble();
System.out.println("Calculating...");
int moneynew = (int) money;
int x,y,x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7,b;
x=moneynew/500;
y=moneynew%500;
x1=y/200;
y1=y%200;
x2 = y1/100;
y2 = y1%100;
x3 = y2/50;
y3 = y2%50;
x4 = y3/20;
y4 = y3%20;
x5 = y4/10;
y5 = y4%10;
x6 = y5/5;
y6 = y5%5;
b=x+x1+x2+x3+x4+x5+x6;
System.out.println("# of €500: " +x);
System.out.println("# of €200: " +x1);
System.out.println("# of €100: " +x2);
System.out.println("# of €50: " +x3);
System.out.println("# of €20: " +x4);
System.out.println("# of €10: " +x5);
System.out.println("# of €5: " +x6);
System.out.println("# of €1: " +y6);
}
}
Now its ready for some implementations. Below are the sample results for different entered values.
Curriculum
Posted on Utopian.io - Rewarding Open Source Contributors
Hey @wodsuz, your contribution was rejected by the supervisor @mcfarhat because he found out that it did not follow the Utopian rules.
Upvote this comment to help Utopian grow its power and help other Open Source contributions like this one. Do you want to chat? Join me on Discord.
This post has received a 5.89 % upvote from @boomerang thanks to: @wodsuz
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
please check my note below as to why this had to be rejected
Your contribution cannot be approved because it does not follow the Utopian Rules.
Not sure how this relates to the IDE or automated function generation, but also the coding content is too basic.
Please make sure to pick the proper repository for the coding language you use, and to contribute SOME more elaborate coding.
You can contact us on Discord.
[utopian-moderator]