Pages: 1
5.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.
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
most programmers start that way... even I always do this until now
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
but if you have your pc with you and compiled your codes... you can easily see errors
[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!
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
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
My own version of this program:
<">
Last edited by nanix84 (2010-07-03 00:35:31)
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
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)
that's why i'm having an advance study on some parts about OOP (java)[/quote]
Wow!
you are excited to learn more with regards to programming eh? and not on terms
But you know what... terminologies are quite useful too when you will be having interviews
that's why I keep on reviewing terms when I will have an interview
[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
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
joke
As far as I know
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
Pages: 1