Results 1 to 8 of 8
Thread: how to auto create objects
- 08-27-2012, 03:45 AM #1
Member
- Join Date
- Aug 2012
- Posts
- 3
- Rep Power
- 0
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?
- 08-27-2012, 03:51 AM #2
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.
- 08-27-2012, 04:29 AM #3
Member
- Join Date
- Apr 2012
- Location
- Spainish TOwn
- Posts
- 25
- Rep Power
- 0
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.
- 08-27-2012, 07:01 AM #4
- 08-27-2012, 07:42 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 08-27-2012, 05:56 PM #6
Member
- Join Date
- Aug 2012
- Posts
- 3
- Rep Power
- 0
Re: how to auto create objects
Although this is android, the question is about JAVA and JAVA objects

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.
- 08-28-2012, 02:58 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: how to auto create objects
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.This seems so counter intuitive though.
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.
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.)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 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.
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.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.........
- 01-08-2013, 11:42 AM #8
Similar Threads
-
Noob question - Create objects using objects as parameters
By pantaloc in forum New To JavaReplies: 12Last Post: 04-29-2012, 02:55 PM -
For loop to create objects
By SteroidalPsycho in forum New To JavaReplies: 4Last Post: 02-24-2010, 09:31 AM -
How to create an array of objects
By redmaverick in forum New To JavaReplies: 7Last Post: 10-19-2009, 02:14 AM -
read txt file,with some records, create objects and store objects in tables of a db.
By stamv in forum JDBCReplies: 1Last Post: 01-22-2009, 04:25 PM -
How to create auto-increment
By Albert in forum JDBCReplies: 2Last Post: 07-04-2007, 05:23 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks