Results 1 to 5 of 5
Thread: Problem on multiple creation
- 11-04-2012, 02:08 AM #1
Member
- Join Date
- Nov 2012
- Posts
- 4
- Rep Power
- 0
Problem on multiple creation
Hi, i am new in JAVA and i have the following question.
I have a class with name 'box' and a class with name 'ball'.
I want, for example, to create 3 boxes consisting of 5 balls each.
I have 2 questions, yet :
(1) is the code i provide below correct ?
(2) if yes, then how can i call the function boxprint() from the first, second and third box ? (i dont want the b.boxprint(), but something
like b[0].boxprint() for the 1st box, b[1].boxprint() for the 2nd box and b[2].boxprint() for the 3rd box)
i provide below what i have tried so far:
PHP Code:import java.util.Vector; public class Myprogram{ protected int numboxes; protected int numballs; public Myprogram(int numboxes, int numballs){ this.numboxes = numboxes; this.numballs = numballs; } public void creation(){ Box b = new Box(numboxes,numballs); b.boxprint(); // i want something like b[0].boxprint(); //more code here } public static void main(String[] argv ){ int numboxes = 3; int numballs = 5; Myprogram m = new Myprogram(numboxes, numballs); m.creation(); } }PHP Code:import java.util.Vector; public class Box{ protected int numboxes; protected int numballs; protected Vector<Ball> balls; public Box(int numboxes, int numballs){ this.numboxes = numboxes; this.numboxes = numballs; balls = new Vector<Ball>(numballs); } public void boxprint(){ System.out.println("Hello"); } }PHP Code:import java.util.Vector; public class Ball{ protected int numballs; public Ball(int numballs){ this.numballs = numballs; } }
ps i know the title is quite horrible.
Thanks.
- 11-04-2012, 02:30 AM #2
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: Problem on multiple creation
Well, you've got Vector imported, so let's use it.
The capacity of your Vector can increase, but this is good. (Vector by default doubles in size if you just use .add(Object) to implicitly increase the capacity.)Java Code:balls = new Vector<Ball>(numballs); // this is correct, BUT does not create the Ball objects... this initializes a Vector with capacity set to numballs
Here's a VERY simple Vector example:
Java Code:public static void main(String[] args){ Vector<String> vs = new Vector<String>(4); // Create a Vector with initial capacity of 4 for(int i = 0; i < 4; i++){ // Add 4 String objects to the Vector vs.add("New String " + i); } for(int i = 0; i < vs.size(); i++){ // This checks .size() on the Vector to determine how many Strings are in it. This can be larger // than the _initial_ capacity. (but not the _current_ capacity) System.out.println(vs.get(i)); // the .get(i) gets the Object at index i to act on it... in this case, prints it. } }Last edited by SJF; 11-04-2012 at 02:31 AM. Reason: lines truncated
- 11-04-2012, 01:12 PM #3
Member
- Join Date
- Nov 2012
- Posts
- 4
- Rep Power
- 0
Re: Problem on multiple creation
sorry but i still cant understand.
if i want to call the boxprint() function from the second box or the third box in Myprogram class how will i do that ?
i was considering something like b[1].boxprint() for the second box or b[2].boxprint() for the trird box but it doesnt work.
it works only with b.boxprint(); but i cant understand this call in which box refers to ? number one, two or three box ?
i want actually the Box b = new Box(numboxes,numballs); the b to be a vector. i probably need something like vector of objects any idea ?
- 11-04-2012, 01:21 PM #4
Member
- Join Date
- Nov 2012
- Posts
- 4
- Rep Power
- 0
Re: Problem on multiple creation
probably in the class Myprogram should i have : Vector<Box> b = new Vector<Box>(numboxes,numballs);
is that correct or not ? if yes then how to write b[0].boxprint() ??
- 11-04-2012, 03:36 PM #5
Member
- Join Date
- Nov 2012
- Posts
- 4
- Rep Power
- 0
Re: Problem on multiple creation
i ll re-write my question to be more specific.
My target is to create 3 Box, and each Box to consist of 5 Balls.
I changed the code :
Java Code:import java.util.Vector; public class Myprogram{ protected int numboxes; protected int numballs; public Myprogram(int numboxes, int numballs){ this.numboxes = numboxes; this.numballs = numballs; } public void creation(int numboxes){ Box b = new Box(numballs); Box c = new Box(numballs); Box d = new Box(numballs); //b = new Vector<Box>(numboxes,numballs); //b.boxprint(); // i want something like b[0].boxprint(); <---- SOS //more code here } public static void main(String[] argv ){ int numboxes = 3; int numballs = 5; Myprogram m = new Myprogram(numboxes, numballs); m.creation(numboxes); } }Java Code:import java.util.Vector; public class Box{ protected int numboxes; protected int numballs; protected Vector<Ball> balls; public Box(int numballs){ this.numballs = numballs; balls = new Vector<Ball>(numballs); } public void boxprint(){ System.out.println("Hello"); } }my question is that in class Myprogram in creation() function i want to change this part of code:Java Code:import java.util.Vector; public class Ball{ protected int numballs; public Ball(int numballs){ this.numballs = numballs; } }
in one, in a vector. Something like Box b[] = new Box(numballs); how to make that change any idea ?Java Code:Box b = new Box(numballs); Box c = new Box(numballs); Box d = new Box(numballs);
Similar Threads
-
dynamic creation of datasources in weblogic, remote creation of jndi's through java?
By prasanth raju in forum JDBCReplies: 0Last Post: 05-18-2012, 01:29 PM -
Multiple Rows In File Creation
By Alerhau in forum New To JavaReplies: 3Last Post: 06-14-2011, 06:54 PM -
Problem in File Creation
By rajanerode in forum New To JavaReplies: 1Last Post: 03-15-2011, 11:27 AM -
Multiple Process Creation in EJB3.0
By sankarguru in forum Enterprise JavaBeans (EJB)Replies: 1Last Post: 03-25-2009, 05:26 AM -
Java Applet re-creation problem
By Mikalai.Kardash in forum Java AppletsReplies: 0Last Post: 07-23-2007, 02:09 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks