[Q] Java/android not understanding oop

Search This thread

madmcphil

Member
Jun 30, 2010
22
0
Hi guys,

Im really trying to learn java but think Im getting very confused. I have a class which holds names and information, In my android program I have the user enter a load of names which then are passed as an array to my next activity. Whenever I try to create objects from each name in the array it wont do it? Ialso understand that this may be a bad way of doing this as names will change so how would I reference them. Maybe using the array[pos] to create objects... so instead of name.printweight(); I could use 1.printweight();

I really hope above is readable or understandable as to what I am trying to learn

SUDO:

Take in names of competitors
Pass array of competitors to next activity
for loop to get each competitor name entered to a string
create an object using the competitors name (e.g name.birthday)


class competitor:
dob
weight
highest score
time

I think you cant use a string name or an int to create objects but how else would I create several competitors in my class?

Im reading some books I bought and I really am struggling to make sense of all this. Please be patient with me...

Kind regards

Phil
 
Last edited:

madmcphil

Member
Jun 30, 2010
22
0
Think I got it.....

for (int i = 0; i < numcompetitors; i++)
{
String competitorName = (String) competitorsArray;
TextView txtOutput = (TextView) findViewById(R.id.edttxtOutput);
txtOutput.append(competitorName + " ");
//create an object of this competitor
competitorsArray = new Competitor();
((Competitor) competitorsArray).average();
}
 

The_R

Senior Member
Jan 18, 2011
162
46
I couldn't really understand what you are having a problem with. The code you posted might be working but it really doesn't seem like a good way of doing what you are trying to do.

I'd like to help you out but first can you please explain a bit more clearly? Maybe even post your code, so that its easier for me to understand.
 

RoberGalarga

Senior Member
Dec 21, 2011
745
274
In front of my laptop :D
So... if I understood you, you need something like this:
class competitor:
dob
weight
highest score
time

Names entered:
A,B,C

Result:
A.dob, A.weight, A.highestscore, A.time
B.dob, B.weight, B.highestscore, B.time
C.dob, C.weight, C.highestscore, C.time

Right? Well, it can't be achieved using Java, it doesn't allow you to assign programatically names to variables. However, you can use a little trick based on an array:
Code:
//Remember: you have a class named "competitor" containing several variables
String[] Names= {"Uno","Dos","Tres"};  //the array passed by the other activity
competitor[] playerData=new competitor[Names.length];  //an array, to store copies of the competitor class (a copy for each name entered)

for(byte i=0;i<Names.length;i++){
  playerData[i]=new competitor();  //Initializing the copy...
    playerData[i].dob=     //Storing data for each name entered...
      playerData[i].weight=
        playerData[i].highestscore=
          playerData[i].time=
}
And that's all... 1st player's data will be stored in the position 0 of the array, 2nd player's data in the position 1... and so on.

BTW, instead use 2 activities, is more easy to use only an activity, changing its layout when you need a GUI's change (yep, it's possible :D).