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?
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.
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 **
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 **
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 **
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 );
}
}
Re: how to auto create objects
Quote:
Originally Posted by
Sling-it
I have an Android program
Moved from New to Java.
db
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.
Re: how to auto create objects
Quote:
Originally Posted by
DarrylBurke
Moved from New to Java.
db
Although this is android, the question is about JAVA and JAVA objects:s:
Quote:
Originally Posted by
pbrockway2
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.
Re: how to auto create objects
Quote:
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.
Quote:
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.
Quote:
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.
Re: how to auto create objects
Quote:
Originally Posted by
Aadamgibson
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
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