• ARCHIVES 
  • » Hello there I need help ): I'm still a beginner when it comes to java o.o and so I still have this kind of difficulty in making such kind of java programs... here's the little background of my progra

Pages: 1

Hello there I need help ): I'm still a beginner when it comes to java o.o and so I still have this kind of difficulty in making such kind of java programs... here's the little background of my progra

mystic rain
» FTalkWhiz
FTalk Level: zero
2963
0
1969-12-31

Hello there I need help ): I'm still a beginner when it comes to java o.o and so I still have this kind of difficulty in making such kind of java programs... here's the little background of my progra

Hello there I need help ): I'm still a beginner when it comes to java o.o and so I still have this kind of difficulty in making such kind of java programs... here's the little background of my program. The program will display its' item to sell and also its' prices. The use will then choose what item he/she will buy after choosing one item... the user will then decide if how many items he/she should buy. After that... the program will then display how much is his/her payment... [quote]import java.util.Scanner; import java.util.Arrays; public class Practice{ public static void main(String [] args){ Scanner inputted=new Scanner(System.in); String[] items=new String [5]; items[0]="Jacket"; items[1]="Pants"; items[2]="Blouse"; items[3]="Bag"; items[4]="Shoes"; Double[] price= new Double[5]; price[0]=10.00; price[1]=25.00; price[2]=35.00; price[3]=15.00; price[4]=5.00; int numberOfItems=0; double total=0.0; System.out.println("What is your name?"); String yourName=inputted.nextLine(); System.out.println("\nWelcome to Rain's Store "+ yourName); System.out.println("Items:"); for(int y=0;y<x.length;y++){ System.out.println(x[y] + " = "+ price[y]+"pesos"); System.out.println("your chosen item:"); String yourItem=inputted.nextLine(); for(int y=0;y<x.length;y++) { if(yourItem==item[y]) { System.out.println("How many"+ item[y]+ "do you like?"); int numberOfItems=lol.nextInt(); for(int x=1;x<=numberOfItems;x++) double total=total + price[y]; break; } break; } } }[/quote] corrections about my program are well appreciated or in other words your help.
nanix84
» SuperFTalker
FTalk Level: zero
7847
0
1969-12-31

Re: Hello there I need help ): I'm still a beginner when it comes to java o.o and so I still have this kind of difficulty in making such kind of java programs... here's the little background of my progra

[quote=mystic rain;#3643504;1277915955]corrections about my program are well appreciated or in other words your help.[/quote] Great!! you almost got it right! :D All you need is practice to have a better looking and error free programs :) First question I would like to ask... [b]Did you run your sample program? and find it working?[/b] The best way to create good programs is to start in trial and error basis :lol3: most programmers start that way... even I always do this until now :lol3: So I guess it would be much better if you have your computer with you to run those codes :) one error I found in your code is this [quote]int numberOfItems=[b]lol[/b].nextInt();[/quote] where did you get lol? hehe... anyways, its ok... coz you are still starting you progress :yes: but if you have your pc with you and compiled your codes... you can easily see errors :yes: [b]NEXT:[/b] Part of being a programmer is to write their codes in a much cleaner.. and readable way... and you are doing great on that! :thumbsup: APLAUSE! however, you still need to practice code conventions. putting constant variables in your class :) Since you have constant items and prices... I think you need to put them in the class as its variables. here is an example: [quote]import java.util.Scanner; import java.util.Arrays; public class Practice { final String [] items = {"Jacket","Pants","Blouse","Bag","Shoes"}; final Double [] prices = {10.00,25.00,35.00,15.00,5.00}; public static void main(String [] args){ // your codes here later on } }[/quote] A much cleaner code aint it? :) [b]NEXT:[/b] If you create a class... that has some process.. or there will be functionality for a class in OOP terms... method.. you need to create one if it will perform a process. So, since this class will have some processes: -display items -get input item -get item quantity -get item Total Price Then you need to create methods. The methods will be [b]displayItems[/b], [b]getInputItem[/b], [b]getItemQuantity[/b], [b]getTotal[/b] [quote]import java.util.Scanner; import java.util.Arrays; public class Practice { final String [] items = {"Jacket","Pants","Blouse","Bag","Shoes"}; final Double [] prices = {10.00,25.00,35.00,15.00,5.00}; Scanner inputted=new Scanner(System.in); public static void main(String [] args){ // your codes here later on } public void displayItems() { // your codes here to display the items } public String getInputItem() { // your codes here to get input } public int getItemQuantity() { // your codes here to get the item quantity } public Double getTotal() { // your codes here to get the Total } }[/quote] [i]You asked: [/i]Why do we need to create methods? and how can we identify them? [i]Answer:[/i] We need to create methods in order to re-use them... for example... in your code... you need to ask another item again.. then you can re-use that method to get an item. How do we identify them? Simple.. just know what you need to do... or the process that was involved in your program :) [b]NEXT:[/b] In the void Main, you need to fill it up first... then you will know what those methods will do. It will have a series of method calls to make your program work [quote]import java.util.Scanner; import java.util.Arrays; public class Practice { final String [] items = {"Jacket","Pants","Blouse","Bag","Shoes"}; final Double [] prices = {10.00,25.00,35.00,15.00,5.00}; Scanner inputted=new Scanner(System.in); public static void main(String [] args){ displayItems(); // display the items String itemSelected = getInputItem(); // selected by user int howMany = getItemQuantity(); // will get quantity Double total = getTotal(itemSelected, howMany); System.out.println("The total price would be: "+total); } public void displayItems() { // your codes here to display the items } public String getInputItem() { // your codes here to get input } public int getItemQuantity() { // your codes here to get the item quantity } public Double getTotal(String itemName, int quantity) { // your codes here to get the Total } }[/quote] [b]NEXT:[/b] Since you already have the structure of the Class and its method.. Then you are ready to fill-in the BLANKS for your program to work :) I know you can do it on your own... so I leave that work to you :D Just replace those comment tags with your codes and it should be working great! If you got any questions... please feel free to post it here :) Thank you :D My own version of this program: <">

Last edited by nanix84 (2010-07-03 00:35:31)

mystic rain
» FTalkWhiz
FTalk Level: zero
2963
0
1969-12-31

Re: Hello there I need help ): I'm still a beginner when it comes to java o.o and so I still have this kind of difficulty in making such kind of java programs... here's the little background of my progra

Geez, thanks for answering my question nanix ((: Well, our professor is a little bit slow in discussing OOP terms and others :D that's why i'm having an advance study on some parts about OOP (java) So, you should be expecting that I have tons of question from my mind today :P haha. [quote=nanix84;#3644942;1278127017]import java.util.*;[/quote] What is this for? Can you explain what's the purpose of this code for the program? |: and hey, I did have changes of my codes: <">but still, it has one error :\ I'm just a little bit confuse in using array in java ): I prefer using C program than this one aaaaaargh! hahaha! it's getting on my nerves now |: hmpf! continuation for my questions (: 1.[quote=nanix84;#3644942;1278127017]HashMap itemPrice = new HashMap(); itemPrice.put("Jacket", new Double(10.00)); itemPrice.put("Pants", new Double(25.00)); itemPrice.put("Blouse", new Double(35.00)); itemPrice.put("Bag", new Double(15.00));[/quote] Hey, what is hashmap for? |: and this one also -- itemPrice.put what is .put for? o.o 2. [quote=nanix84;#3644942;1278127017]Iterator It = itemKeys.iterator();[/quote] The Iterator thing is new for me o.o can you explain it also o.o what does it do to the program |: 3. [quote=nanix84;#3644942;1278127017]if(itemPrice.containsKey(yourItem))[/quote] that one also -- > the itemprice.containskey o.o what is .containskey for? o.o Thanks again (:

Last edited by mystic rain (2010-07-05 07:48:50)

nanix84
» SuperFTalker
FTalk Level: zero
7847
0
1969-12-31

Re: Hello there I need help ): I'm still a beginner when it comes to java o.o and so I still have this kind of difficulty in making such kind of java programs... here's the little background of my progra

[quote=mystic rain;#3646906;1278330503]Well, our professor is a little bit slow in discussing OOP terms and others :D that's why i'm having an advance study on some parts about OOP (java)[/quote] Wow! :lol3: you are excited to learn more with regards to programming eh? and not on terms :lol3: But you know what... terminologies are quite useful too when you will be having interviews :lol3: that's why I keep on reviewing terms when I will have an interview :lol3: [quote=mystic rain;#3646906;1278330503][b]import java.util.*;[/b] What is this for? Can you explain what's the purpose of this code for the program? |:[/quote] '*' asterisk means... it will import all the content within the package 'util' so instead of writing: [quote]import java.util.Scanner; import java.util.Arrays;[/quote] you will just write: [quote]import java.util.*;[/quote] [quote=mystic rain;#3646906;1278330503]but still, it has one error :\ I'm just a little bit confuse in using array in java ): I prefer using C program than this one aaaaaargh! hahaha! it's getting on my nerves now |: hmpf![/quote] The error in your code is in this part: [quote]double total=[color=red][b]double[/b][/color] total * numberOfItems;[/quote] And why are you confused in using arrays in java? lol :lol3: What could be their difference? [quote=mystic rain;#3646906;1278330503]Hey, what is hashmap for? |: and this one also -- itemPrice.put what is .put for? o.o[/quote] The example that I wrote.. is a bit complex and needs more understanding :) Hashmap is an object much more similar to an array. instead of having indexes... Hashmap uses "key" in which you can access your value. so hashmap always goes with "key-value pair" In your program.. since we have a key "item name" with a corresponding value "item price" For me, Hashmap is the ideal Object to use for this program. and the [b].put[/b] is a method used for Hashmap to add items in it. In this program.. I used a String "key" and a "Double" value, "String" for item Name and "Double" for item price. Hope you got this right :) hehe [quote=mystic rain;#3646906;1278330503]The Iterator thing is new for me o.o can you explain it also o.o what does it do to the program |:[/quote] Hey! Im not a teacher :lol3: joke As far as I know :lol3: Iterator is an object that will traverse the elements being put in a Set or Collection I cannot really elaborate what the Iterator is.. Just Read this article http://leepoint.net/notes-java/data/collections/iterators.html [quote=mystic rain;#3646906;1278330503]that one also -- > the itemprice.containskey o.o what is .containskey for? o.o[/quote] [b]containskey[/b] is a method for Hashmap... in which it will accept a parameter "key" and will return true or false if the hashmap really cointains that key. like what we have in our program. when we added "Jacket" in our Hashmap.. with it's corresponding value 10.00 so when we do itemPrice.containskey("Jacket") this will return true.. coz we added "key" Jacket in our Hashmap. Did you get this right? hehe
  • ARCHIVES 
  • » Hello there I need help ): I'm still a beginner when it comes to java o.o and so I still have this kind of difficulty in making such kind of java programs... here's the little background of my progra

Pages: 1

Board footer

© 2025 F Talk

Current time is 03:10

[ 12 queries - 0.313 second ]
Privacy Policy