Results 1 to 8 of 8
- 06-18-2012, 05:14 AM #1
Member
- Join Date
- Jun 2012
- Posts
- 3
- Rep Power
- 0
Accessing an array from a class and passing to another class through main()
Hey guys, I am having some issues putting my head around passing an array and accessing it as function argument.
More specificaly:
I have a class called Pizza
[code]
class Pizza
{
//then I have a private array
String []PizzaSize;
//I have allocated memory for the array in my constructor
Pizza()
{
PizzaSize = new String[3];
}
//In my accessor I have set 3 values for the array
public void getSize()
{
PizzaSize[0] = "Small";
PizzaSize[1] = "Medium";
PizzaSize[2] = "Large";
}
}
Then I have another class:
public class CustomerOrder
{
//with a function called placeOrder
public String placeOrder()
{
}
}
My question: what parameters should I provide to placeOrder function, so that I can pass user's choice (i.e. 0 for small, 1 for Medium....) and then call this function from the main so that I get the corressponding value as output.
Thank you.
- 06-18-2012, 08:19 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Accessing an array from a class and passing to another class through main()
The natural choice is to pass an integer as the parameter for the placeOrder() method. But there is no restriction to use a string for instance as the parameter type. You, as the programmer, are the one that should know better which one is the most suitable for your program. Using an integer you can use it directly to access the index of the pizza size array that you have defined before.
Java Code:public String placeOrder(int size) { ... }Website: Learn Java by Examples
- 06-18-2012, 07:03 PM #3
Member
- Join Date
- Jun 2012
- Posts
- 3
- Rep Power
- 0
Re: Accessing an array from a class and passing to another class through main()
Sorry I wasn't clear enough. I know I can pass an integer, but how do I pass the PizzaSize array? I mean if I don't pass the PizzaSize[] to the placeOrder function when I try printing it in the function, it does not recognize it.
But when I call the function from main the PizzaSize[] is not recognized?
Thank you
s
- 06-18-2012, 08:48 PM #4
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 3
Re: Accessing an array from a class and passing to another class through main()
I think you should declare PizzaSize[] as public, and you can use <your pizza instance>.PizzaSize[] as you please, or you can make PizzaSize[] as static and call it Pizza.PizzaSize[] if its the same for every pizza, which means it doesn't change from one pizza instance to another.
Hope this helped :)
- 06-18-2012, 09:21 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,379
- Blog Entries
- 7
- Rep Power
- 17
Re: Accessing an array from a class and passing to another class through main()
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-18-2012, 09:23 PM #6
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 3
- 06-18-2012, 09:29 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,379
- Blog Entries
- 7
- Rep Power
- 17
Re: Accessing an array from a class and passing to another class through main()
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-18-2012, 09:31 PM #8
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 3
Similar Threads
-
Passing an array to another class in a JFrame
By George5432 in forum New To JavaReplies: 27Last Post: 05-06-2012, 06:13 AM -
main not passing information to created class
By Teclis in forum New To JavaReplies: 2Last Post: 04-19-2011, 08:58 PM -
Passing array to model class
By keshaba in forum IntelliJ IDEAReplies: 0Last Post: 04-20-2010, 01:43 PM -
problem in accessing array values of one class in to jframe class
By cenafu in forum AWT / SwingReplies: 8Last Post: 03-21-2009, 09:34 AM -
Accessing array in other class
By ce3c in forum New To JavaReplies: 8Last Post: 02-22-2009, 11:07 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks