I need help with Java, please.
Results 1 to 8 of 8

Thread: I need help with Java, please.

  1. #1

    I need help with Java, please.

    I have been trying to figure this out.. I need to create an object using a variable..

    This is what I have tried:

    Class objectName(variable) = new Class();
    Class (objectName + variable) = new Class();
    Class (variable) = new Class();
    Class variable = new Class();

    If you understand what I'm trying to do, as in.. If the user were to type in 5 let's say I want the object to be labeled.. 5 or even object5..

    I've already searched through my textbook's index.. And no luck yet.. Please help me..

    Thank you.

  2. #2
    Member
    Join Date
    Sep 2002
    Posts
    86
    kraftwerk, you can't do that. The compiler has to be able to resolve all of the variable references at compile time, so it won't let you do what you want to. Besides, the name of the variable is only useful to the programmer and anyone maintaining your code. Using a variable as a class name would probably just confuse someone who has to maintain your code.

    Why do you want to do it? Perhaps we can figure something out. If you want to be able to identify a particular object of a class why not build in a variable that you can set with a constructor and when you need to identify an object you just retrieve that variable value.

  3. #3
    My reason for wanting this is for database retrieval/changes... I haven't done arrays in Java yet.. But I'm familiar with them in C++ and VB.. If I have two accounts let's say, I need to identify the one that I'm retrieving or changing.. My program has a user input which allows them to select their account, and then make deposits/withdrawals.. but I have two accounts that I have to change.. And I want my program to be able to handle 1000 accounts if necessary, but without having to change any code.. Also, I just started Java this semester.. I'm picking it up very fast.. Which leads me to be constantly interested in learning more.. And my class moves slow.. I have a couple of questions if you have enough time... But let's stick with this for now..
    Thanks.

  4. #4
    Member
    Join Date
    Sep 2002
    Posts
    86
    The main difference between arrays in C++ and Java is that arrays are objects in Java. With that said, identifying which account you are working with really isn't any different than it would be in C++. There are actually a couple of different ways to do what you want to do, bit if you want to minimize code changes I would say that you are going to want to have instance variables that you can set via a constructor that an "account object" can use to identify itself and the account. Your problem is really more of a design issue than a coding issue. If you account for scalability by including the proper identification properties then you shouldn't have any trouble.

    Also, if you want to be able to scale upward with the number of objects you are handling you may want to use a vector to hold account objects. A vector is an object that can hold an unlimited number of objects of different types. You just have to cast each object you retrieve from the vector to it's proper type before you use it again.

  5. #5
    Senior Member
    Join Date
    Jan 2002
    Posts
    187
    i'm not real sure what you're after.....i think you want to create an array of objects, so that way you can access them threw the array subscript.

    for(int i; i< array.length; i++)
    Object array[i] = new Object(argument list);


    maybe that helps?
    U suk at teh intuhnet1!!1!1one

  6. #6
    Member
    Join Date
    Sep 2002
    Posts
    86
    If I knew how many objects I was going to limit my program to use I would use an array. That way you don't have to cast the objects because they are already referenced as the proper class.

    If I didn't know how many objects I was going to have to track, then I probably wouldn't define an array, simply because it can't grow as needed.

    When you define the array you would have to do something like

    AccountOject[] accountArray = new AccountObject[1000];

    If objects are being created via user input then there wouldn't be a for loop, you would add them to the array as they are created, so if the last one added is number 1001 you will get a runtime exception unless you check the number of array elements. This solution isn't very scalable because you've set the limit and you will need to change code to add more elements.

    By using a Vector collection object you can put a virtually unlimited number of AccountObject objects into it and grow the Vector as needed. For example:

    import java.util.*;

    Vector accountVector = new Vector(1000, 100);

    Now you have a collection of objects that starts at 1000, but it grows by 100 when you add the 1001th record. The reason to grow it by a set number is that it takes resources to extend a Vector, so if you do it in acceptable increments you aren't increasing the size for each add. Another benefit is that you can easily search for and replace values in a Vector.

    [Edit] The only catch to a Vector is that when you retrieve an object it is stored as type Object, so you have to cast it back to an AccountObject before you use it, or you could get an exception.

    When it comes to scalability the Collections feature in Java is a great alternative to traditional arrays.

  7. #7
    Ninja Code Monkey
    Join Date
    Nov 2001
    Location
    Washington State
    Posts
    1,027
    hrm...I highly reccomend picking up 'Data Structures and Algorithms in Java' from Waite Group Press. It's a good book and very relevant here.
    "When I get a little money I buy books; and if any is left I buy food and clothes." - Erasmus
    "There is no programming language, no matter how structured, that will prevent programmers from writing bad programs." - L. Flon
    "Mischief my ass, you are an unethical moron." - chsh
    Blog of X

  8. #8
    Senior Member
    Join Date
    Jan 2002
    Posts
    187
    if you don't know how many objects you'll need when you're writing the code, but your user will when he's executing the program, you could just prompt him for it and then make the array that size.

    or you could use more advanced storage structures liked linked lists or b-trees....this would make it more like a traditional database. i guess it's as elegant as you want it to be.
    U suk at teh intuhnet1!!1!1one

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •