Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Bushman

Thread: how to auto create objects

  1. #1
    Sling-it is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default how to auto create objects

    I have an Android program I'm working on that needs to auto create a person object.

    ie;

    15A would be tom jones, 45, male
    15B would be tammy tiny, 23, female
    etc.

    The 15A is auto assigned, and the name and other info is input.

    String [] personletter = new String [4];
    //then inserted strings A, B, C, D into personletter

    int personcounter = 0;




    //button listener for enter new information
    //Android reads input from user and assigns to variables

    if(personcounter < personletter.length){

    //**here's where the trouble comes in**

    Person 15+personletter[personcounter] = new Person(lname, fname, age, gender);
    personcounter = personcounter++
    }



    I am trying to get a maximum of 4 people, designated as 15A, 15B, 15C and 15D. Then I can pull and display information for any of them simply by refering to their designation. Problem is that JAVA is throwing an error "can't convert String to Person" How do I get this to work other than a bunch of "IF" statements?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: how to auto create objects

    You cannot do that in Java. A more appropriate way to handle this is to use a Collection to store all your Person objects.

  3. #3
    Bushman is offline Member
    Join Date
    Apr 2012
    Location
    Spainish TOwn
    Posts
    25
    Rep Power
    0

    Default Re: how to auto create objects

    yes, junky is right.
    When dealing with objects, it is always great practice to first create a class that represents the object, then create another class which can collect those objects.

    Here is quick example

    **THE PERSON OBJECT **
    PHP Code:
    public class Person 
    {
        //attributes
        public String designation, firstName, lastName, age, gender;
        
        //constructor
        public Person ( String _designation, String _firstName, String _lastName, String _age, String _gender )
        {
            //we setup attributes here, with respect to programmer specified params passed above
            designation = _designation;
            firstName = _firstName;
            lastName = _lastName;
            age = _age;
            gender = _gender;
        }
        
        //accessors
        public String getDesignation ( )
        {
            return designation;
        }
        public String getFirstName ( )
        {
            return firstName;
        }
        public String getLastName ( )
        {
            return lastName;
        }
        public String getAge ( )
        {
            return age;
        }
        public String getGender ( )
        {
            return gender;
        }
    }

    ** THE COLLECTION **
    PHP Code:
    import java.util.ArrayList;
    
    public class PersonCollection extends ArrayList <Person>
    {
        //attributes
        public ArrayList designationList = new ArrayList ( );
        public int designationCount = -1;
    
        //constructor
        public PersonCollection ( )
        {
            //initialize designationList
            for( char letter = 'A'; letter <= 'D'; letter++ )
                designationList.add ( "15" + letter );
        }
    
        //mutator
        public void addPerson ( String firstName, String lastName, String age, String gender )
        {
            designationCount ++;
            add ( new Person ( ( String ) designationList.get ( designationCount ), firstName, lastName, age, gender ) );
        }
        
        
        //accessor
        public Person getPerson ( String designationSearchTag )
        {
            Person value = null;
            for ( int i = 0; i < size ( ); i ++ ) //for all persons in the list
                if ( designationSearchTag.equals ( designationList.get ( i ) ) ) //if search tag matches any in list
                     value = get ( i );
            return value;
        }
    }



    ** A TEST **
    PHP Code:
    public class PersonTest
    {
        public static void main ( String [ ] args )
        {
            //person collection
            PersonCollection personPack = new PersonCollection ( );
            
            personPack.addPerson ( "Peter", "Parker", "24", "male" );
            personPack.addPerson ( "Mary", "Jane", "22", "female" );
            personPack.addPerson ( "Harry", "Osbourne", "26", "male" );
            personPack.addPerson ( "Sand", "Man", "45", "male" );
            
            //get person with id 15A
            String person = personPack.getPerson ( "15D" ).getFirstName ( ) + " " + personPack.getPerson ( "15D" ).getLastName ( );
            System.out.println ( person );
        }
    }
    Last edited by Bushman; 08-27-2012 at 04:34 AM.
    Deeg likes this.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: how to auto create objects

    Quote Originally Posted by Sling-it View Post
    I have an Android program
    Moved from New to Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: how to auto create objects

    If you want to access things by names you assign to them, a good collection to consider is Map.

    Java doesn't allow variables to be constructed on the fly like PHP. If you are coming to the language from this or some other interpreted language it would be a good idea to check out general Java syntax before tackling Android programs. Oracle's Tutorial is a good place to start.

  6. #6
    Sling-it is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default Re: how to auto create objects

    Quote Originally Posted by DarrylBurke View Post
    Moved from New to Java.

    db
    Although this is android, the question is about JAVA and JAVA objects

    Quote Originally Posted by pbrockway2 View Post
    If you want to access things by names you assign to them, a good collection to consider is Map.

    Java doesn't allow variables to be constructed on the fly like PHP. If you are coming to the language from this or some other interpreted language it would be a good idea to check out general Java syntax before tackling Android programs. Oracle's Tutorial is a good place to start.
    This seems so counter intuitive though. If JAVA is such an object orented program language, how do you create these objects then? It makes no sense to program in a language that can't create the objects it uses. The objects are available throughout the package, which means I can access the info easily from different activities rather than handing them through the program.

    It sounds like IF statements are the way to go then;

    if(counter = 0) Person A = new Person..........
    if (counter = 1) Person B = new Person.........

    and so on. I can actually do without the number in front for now, and use that number as an atribute to the person.

    It would be nice to be able to do this when using a large number of textviews and sticking them into an array also.

    ie;
    for (int r = 0; r <= 10; r++){
    for (int c = 0; c <= 5; c++){
    TextView txtvw[r][c] = new TextView;
    }}

    basically making an array of textviews that are accessable through a loop.

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: how to auto create objects

    This seems so counter intuitive though.
    Language designers aim at being intuitive (being "expressive") but at the end of the day programming languages do have "ways of doing things". Perhaps because they are designed they tend to be even more arbitrary in approach than natural languages. Intuition doesn't determine the approach of computer languages: rather use of the languages moulds intuition.

    Obviously I don't know your background. But many who strike problems with "15+personletter[personcounter]" and similar (it's common) do so with intuitions formed using languages like PHP. Part of the intent of my post was to clear the ground by stating quite clearly that this approach will not fly with Java.

    how do you create these objects then? It makes no sense to program in a language that can't create the objects it uses.
    Objects, in Java, are created using the "new" keyword. To be more precise: "new" will give you a reference to a newly created object. This reference value can be assigned to any variable or variables you like. (Java is also strongly typed, so the variables must be declared to accept being assigned references to specific types of object.)

    The problem, as you expressed it in your original post, is not with creating objects. Or about objects at all. It is about variables. Something like "15+personletter[personcounter]" is not a good variable to be using. One reason I suggested having a read of a comprehensive tutorial is to get the distinction between object and variable (and associated concepts like reference value) straightened out.

    It sounds like IF statements are the way to go then;

    if(counter = 0) Person A = new Person..........
    if (counter = 1) Person B = new Person.........
    No, as stated above, collections are they way to go. Bushman gives an example of a collection specifically designed to allow access to Person instances corresponding to a given tag. I suggested using Map, since representing this sort of correspondence is what Map does. The other reason I suggested reading about Java is so that you can see and judge in an informed way what the collections framework has to offer.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: how to auto create objects

    Quote Originally Posted by Aadamgibson View Post
    The Class object is automatically created by the JVM when an object is created.
    More correctly, it's loaded by a ClassLoader.

    Quote Originally Posted by Aadamgibson View Post
    The method that is automatically called when an object is created is called a constructor. In Java, the constructor is a method that has the same name as the class.
    A constructor isn't a method.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 12
    Last Post: 04-29-2012, 02:55 PM
  2. For loop to create objects
    By SteroidalPsycho in forum New To Java
    Replies: 4
    Last Post: 02-24-2010, 09:31 AM
  3. How to create an array of objects
    By redmaverick in forum New To Java
    Replies: 7
    Last Post: 10-19-2009, 02:14 AM
  4. Replies: 1
    Last Post: 01-22-2009, 04:25 PM
  5. How to create auto-increment
    By Albert in forum JDBC
    Replies: 2
    Last Post: 07-04-2007, 05:23 AM

Posting Permissions

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