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:
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();
}
}
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");
}
}
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.
Re: Problem on multiple creation
Well, you've got Vector imported, so let's use it.
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
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.)
Here's a VERY simple Vector example:
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.
}
}
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 ?
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() ??
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 :
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);
}
}
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");
}
}
Code:
import java.util.Vector;
public class Ball{
protected int numballs;
public Ball(int numballs){
this.numballs = numballs;
}
}
my question is that in class Myprogram in creation() function i want to change this part of code:
Code:
Box b = new Box(numballs);
Box c = new Box(numballs);
Box d = new Box(numballs);
in one, in a vector. Something like Box b[] = new Box(numballs); how to make that change any idea ?