What Will I Learn?
- You will learn usage of java
- You will learn how to generate array.
- You will learn how to generate a simple program by using eclipse and java.
Requirements
- Java latest version setup.
- Eclipse
- Basic knowledge of coding on java and eclipse.
Difficulty
- Intermediate
Tutorial Contents
Hello,in this course i am starting a new course.In this course i am going to show you how to code Java on Eclipse.I made a combine of java and eclipse called "Jeclipse" for my tutorials.In this tutorial we are going to make a program for a greengrocerer,this program will calculate costumers total price.
Let's begin with including library.As you know "package greengrocerer" is my programs name.
"import javax.swing.JOptionPane" is used for to have a input box or message box on java language.
package greengrocerer;
import javax.swing.JOptionPane;
Now i am defining class and starting the code.
public class Greengrocerer {
public static void main(String[] args) {
Now i am going to define a new array called String Prices[]. String type is used for letters etc. as you know.I make my array have 7 members.Then starting from 0 to 6 I define arrays members named "prices" and write greengrocerers items and prices.
String Prices[] = new String [7];
Prices[0] ="Tomato Kilo ; 1.50$ ";
Prices[1] ="Cucumber Kilo ; 2.50$ ";
Prices[2] ="Cabbage Each ; 1.50$ ";
Prices[3] ="Parsley Each ; 1.50$ ";
Prices[4] ="Mandarin Kilo ; 3.50$ ";
Prices[5] ="Orange Kilo ; 1.75$ ";
Prices[6] ="Eggplant Kilo ; 1.75$ ";
Now i will make code show the prices to the user on screen by using Joptionpane.JOptionPane makes it easy to pop up a standard dialog box that prompts users for a value or informs them of something.
JOptionPane.showMessageDialog(null, Prices[0] + "n" +Prices[1] + "n"
+ Prices[2] + "n" + Prices[3] + "n" + Prices[4] + "n" + Prices[5]
+ "n" + Prices[6]);
Now in these steps we will define each fruit and vegetables as float type because the prices are fractional.After that i am defining "howmanykilo" or "how many" in float type again and asking user by using "Float.valueOf"."Float.valueOf"Returns a Float object holding the float value represented by the argument string S.If s is null, then a NullPointerException is thrown.
In the final step i am going to define "result" and equalize it to fruit or vegetable's * kilogram or *each.I am going to do this for each 7 members of my array.
float Tomatokilo = (float) 1.50;
float dhowmanykilo=Float.valueOf(JOptionPane.showInputDialog(null,"How Many Kilos Tomato ?"));
float dresult = Tomatokilo * dhowmanykilo ;
You can see as i told above i define float type variable for my first member of array "tomato" named it as "tomatokilo" then wrote down the price.In the second line i defined a new float "dhowmanykilo" in this step i am asking user how many kilos of tomato they wanted to buy."d" in the begging of float i add it because i am going to define six more "howmanykilo" and they can't be the same.
After that i equalize it to "Float.valueOf(JOptionPane.showInputDialog(null,"How Many Kilos Tomato ?"));" after asking user how many kilos they want to buy at above now in this code line we make user to enter how many kilos they want to buy.
"float dresult = Tomatokilo * dhowmanykilo ;" And finally in this line we define a new float "result" i added "d" letter in the begging for the same reason as "dresult".After user entered a random kilo we multiply it with the price of the product.We are going to do same thing for six more times for the six members of array below.
float CucumberKilo = (float) 2.50;
float showmanykilo=Float.valueOf(JOptionPane.showInputDialog(null,"How Many Kilos Cucumber ?"));
float sresult = CucumberKilo * showmanykilo ;
float CabbageEach = (float) 1.50;
int lhowmany =Integer.valueOf(JOptionPane.showInputDialog(null , "How Many Cabbage ?"));
float lresult = CabbageEach * lhowmany ;
float ParsleyEach = (float) 1.50;
int mhowmany =Integer.valueOf(JOptionPane.showInputDialog(null , "How Many Kilos Parsley ?"));
float mresult = ParsleyEach * mhowmany ;
float MandarinKilo = (float) 3.50;
float mahowmanykilo =Float.valueOf(JOptionPane.showInputDialog(null , "How Many Kilos Mandarin ?"));
float maresult = MandarinKilo * mahowmanykilo ;
float OrangeKilo = (float) 1.75;
float mmhowmanykilo =Float.valueOf(JOptionPane.showInputDialog(null , "How Many Kilos Orange ?"));
float mmresult = OrangeKilo * mmhowmanykilo ;
float EggplantKilo = (float) 1.75;
float phowmanykilo =Float.valueOf(JOptionPane.showInputDialog(null , "How Many Kilos Eggplant ?"));
float presult = EggplantKilo * phowmanykilo ;
As you can see above i defined new "result" and new "howmany" in a new letter in every case.This is because we have 7 different members in array and all members have diffrent cost.
Now in the final step we are going to use "JOptionPane.showMessageDialog" to write the final result on the screen.As you can see below i wrote "tomato" first then do the addition with "dresult" which is "tomato's" Final result.
I am going to do that for every member of array.Finally i have to mention "$" sign.I put dollar sign in after every each member of array after the addition for the visuality.
JOptionPane.showMessageDialog(null , " Tomato ; " + dresult + " $" + "n Cucumber ; "
+ sresult + " $" + "n Cabbage ; " + lresult + " $" + "n Parsley ; " + mresult + " $"
+ "n Mandarin ; " + maresult + " $" + "n Orange ; " + mmresult + " $" + "n Eggplant ; "
+ presult + " $" + "n n Total Price ; " + TotalPrice + " $");
All of the code ;
Outputs
Final Result
In the final result program shows me each fruit and vegetabel's total price and in the end it calculates the "total price".
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
thank you very much!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hey @shreyasgune, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
please include the code as text. ( in a code box)
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Fixed,thank you for the warning!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
You got a 1.44% upvote from @mercurybot courtesy of @milliar!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hey @milliar I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit