Java Questions: Creating objects on demand?

airbornflghtairbornflght Houston, TX Icrontian
edited October 2006 in Internet & Media
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]

Comments

  • edited October 2006
    So I understand that you want to create x instances of the computer class, where x is the number of different computer models someone has?

    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
  • airbornflghtairbornflght Houston, TX Icrontian
    edited October 2006
    So I understand that you want to create x instances of the computer class, where x is the number of different computer models someone has?

    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?
  • edited October 2006
    In the above example you would be able to retrieve each computer from the array. You don't need to increment the cID in this example.

    To retrieve each computer (cID object) you would use the ArrayList get command.

    Regards,
    Scott
  • airbornflghtairbornflght Houston, TX Icrontian
    edited October 2006
    oh, ok, you would grab it by using the index, gotcha.
  • edited October 2006
    That's correct, you're spot on!

    Regards,
    Scott
  • airbornflghtairbornflght Houston, TX Icrontian
    edited October 2006
    so, if I wanted to get the object, and then use a method of the obect, I could say:

    [PHP]System.out.print((array1.get(1))cID.getSpeed());[/PHP]

    would that be how you do it?
  • edited October 2006
    You may get an issue with having the case the computer object in the array. The array just holds objects - Java doesn't know it's a computer type of object.

    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]
  • edited October 2006
    or if you are using java 1.5 you can do this :

    [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.
Sign In or Register to comment.