Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Simulator display issues
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Tallguy




PostPosted: Mon Mar 07, 2011 6:34 pm   Post subject: Simulator display issues

Hello hello,

I for my assignment it is to continue on the steps to creating a fully working LoTR battle simulator, each assignment building off each other. This assignment to is randomize 'actor' variables (name/speed etc) for a user defined army (light/dark) and for a size that they speficify. Im kinda new to java so . . .

-we need a array of refences that refernecs individual 'actors' then display everything to the screen


code:
for (int i=0; i<nLightArmy; i++){
        armyOfLight[i] = new ActorFactory();
        armyOfLight[i].armyOfLight();
        //JOptionPane.showMessageDialog(null,armyOfLight[i].toString());
        outPut +=armyOfLight[i].toString();
                                       
}
        for (int i=0; i<nLightArmy; i++){
                JOptionPane.showMessageDialog(null,armyOfLight[i].toString());
}
        //JOptionPane.showMessageDialog(null,outPut + "\n");


im having a problem with displaying all the data together on one GUI screen, each class 'sends' it data forward but i cant seem to get the data to display i get 'ActorFatcory@15443e54' for each actor?? Why is this doing it?



src.zip
 Description:
All the files...

Download
 Filename:  src.zip
 Filesize:  4.88 KB
 Downloaded:  121 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Mon Mar 07, 2011 7:23 pm   Post subject: RE:Simulator display issues

1) Generics make Java suck less.
code:

List<Actor> armyOfLight = new ArrayList<Actor>();

for(Actor army_member : armyOfLight) {
  ...
}

No casting. No off-by-one errors. Awesome APIs for the data structure.

Quote:

'ActorFatcory@15443e54' for each actor

Yes, you're assigning an ActorFactory instead of an Actor.
code:

armyOfLight[i] = new ActorFactory();


A factory pattern is typically used as:
code:

ActorFactory my_factory = new ActorFactory();
...
my_actor = my_factory.get_new_actor();

But that only makes sense if creation of new instances involves processes outside of a typical constructor.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
DemonWasp




PostPosted: Mon Mar 07, 2011 8:01 pm   Post subject: RE:Simulator display issues

The default implementation of the toString() method returns ClassName@Address. To make it display something else, override it by adding a method with this exact signature:

public String toString()

With an implementation that returns a String representing your Actor.
Tallguy




PostPosted: Tue Mar 08, 2011 11:33 am   Post subject: Re: Simulator display issues

awesome ty,
Tony i would use arraylist, but for this assignment we are not allowed.

This is what i did, and something in another class to fix my previous error
code:
for (int i = 0; i < nDarkArmy; i++) {
                        armyOfDarkness[i] = new ActorFactory();
                        armyOfDarkness[i].armyOfDarkness();
                        outPutDarkness += armyOfDarkness[i].actor.toString();
          }


now the only problem i have is.....

To display all the data i have used:

code:

public void dispalyAll() {
               
                JOptionPane.showMessageDialog(null , "Army of light ;" + outPutLight + "\nArmy of darkness;"+outPutDarkness);

}


why is 'null;' still showing up? it says "Army of light;null;.....data", what can i do to get rid of this?



src.zip
 Description:
All files

Download
 Filename:  src.zip
 Filesize:  4.89 KB
 Downloaded:  114 Time(s)

Tony




PostPosted: Tue Mar 08, 2011 1:15 pm   Post subject: RE:Simulator display issues

So it seems that null is from outPutLight in
code:

"Army of light ;" + outPutLight

Search for the use of the name to see where you create this field, and how it is populated with data.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: