Java Questions: Creating objects on demand?
airbornflght
Houston, TX Icrontian
Can you do it? Look at the code, and I think you will figure out what I am trying to do, haven't even tried to compile it yet, because I know it wont, and I know that I'm doing it wrong, but there has to be a way to do it, doesn't there?
[PHP]public class computers {
public static void main(String [] args){
int ID=0, number=0,x,y,z;
Scanner sc = new Scanner(System.in);
//Figure out how many times to run the loop
System.out.print("How different models of computers do you have: ")
number = sc.nextInt();
//Run the loop, while getting all the instance data
for(int counter; counter<number; counter++ ){
System.out.print("What is x: ");
x = sc.nextInt();
System.out.print("What is y: ");
y = sc.nextInt();
System.out.print("What is z: ");
z = sc.nextInt();
computer cID = new computer(x,y,z);//the ID is the variable
ID++;
//Increment the ID, so that you end with objects like: c0, c1, c2, c3
//Depending on how many you need.
}
}
}[/PHP]
[PHP]public class computers {
public static void main(String [] args){
int ID=0, number=0,x,y,z;
Scanner sc = new Scanner(System.in);
//Figure out how many times to run the loop
System.out.print("How different models of computers do you have: ")
number = sc.nextInt();
//Run the loop, while getting all the instance data
for(int counter; counter<number; counter++ ){
System.out.print("What is x: ");
x = sc.nextInt();
System.out.print("What is y: ");
y = sc.nextInt();
System.out.print("What is z: ");
z = sc.nextInt();
computer cID = new computer(x,y,z);//the ID is the variable
ID++;
//Increment the ID, so that you end with objects like: c0, c1, c2, c3
//Depending on how many you need.
}
}
}[/PHP]
0
Comments
So if I have one computer, you will create on instance of the computer class. If I have 10 computer, you will create 10 instances of the computer class.
A solution would be that within the for loop you create an instance of each computer and put each one into an ArrayList ( http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html ).
The computers array would then contain all the computers that someone has.
[PHP]ArrayList computers = new ArrayList();
for(int counter; counter<number; counter++ ){
System.out.print("What is x: ");
x = sc.nextInt();
System.out.print("What is y: ");
y = sc.nextInt();
System.out.print("What is z: ");
z = sc.nextInt();
computer cID = new computer(x,y,z);//the ID is the variable
computers.add(cID);
} [/PHP]
Regards,
Scott
yeh, pretty much, I have the class built to hold the data about the computer, and also how many of that model you have. so if you have 15 computers, but only 3 different models, and 5 of each, you would only create 3 instances of the class, and when I get the quantity instance data, you would put 5 for the quantity.
my question was would it recognize the "ID" as a variable, or would it think it was just part of the object name? and if it will, I have to increment the "ID" every time, dont I?
To retrieve each computer (cID object) you would use the ArrayList get command.
Regards,
Scott
Regards,
Scott
[PHP]System.out.print((array1.get(1))cID.getSpeed());[/PHP]
would that be how you do it?
Note it is good practice to capitalize the first letter of the class type as well i.e. Computer not computer
This should work -
[PHP]Computer cID = (Computer)array1.get(1);
System.out.print(cID.getSpeed());[/PHP]
[PHP]
//make a list that will hold objects of type 'Person'
List<Person> people = new ArrayList<Person>();
//make a new Person
Person person1 = new Person();
//add it to the list
people.add(person1);
//make another new Person
Person person2 = new Person();
//add it to the list
people.add(person2);
//get person1
Person personFromList = people.get(0);
[/PHP]
of course you can still create a large amount of 'Person' objects and stuff them in the list in a 'for' or 'while' loop. the difference between this way and your way is that this way you don't have to cast the object you pull from the list as a Person, the vm already 'knows' what kind of object the list holds because the list is typed.
brief intro to collections in java 1.5 for you.