• Post Reply Bookmark Topic Watch Topic
  • New Topic

element of the array from another class  RSS feed

Nattakan Lukkanapinit
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a class Agent that contains:

String name - the Agent's name
Client[] my_Clients - an array of Client references. Allocate an array of some fixed size, MAX_CLIENTS, but don't instantiate individual Clients ... we allow this array to point to the caller's Client objects.
int num_Clients - the current number of Clients this Agent has.
MAX_CLIENTS, MIN_NAME_LEN, MAX_NAME_LEN - some static finals to control the values passed into mutators.
Give the Agent class the minimal constructors plus these methods:

AddClient(Client client) - puts client into the Agent's list.
RemoveClient(Client client) - removes client by squeezing him out of the list.
ShowClientsShort() - Displays a list of all this Agent's Clients' names (only names).
ShowClientsLong() - Displays a list of all the Clients, using the Client display method.
double GetIncome() - Returns the dollar amount collectable from all Clients so far this year (based on the tally of the Client member data, percent_cut, and income_this_year.
Create a main program that instantiates a number of Actors and Writers, displays a few of their data to the screen, individually (not all). Next, instantiate one Agent, and place some of the Actors and Writers into the Agent's list. Print out the Agent's Clients' names demonstrating both ShowClients---() methods, and show how much money the Agent is going to get this year.


I tried to add Cli1 in the main just to test but it doesn't work.
the output is "Client@6bdd46f7".
My question is:
How to store "Cli1" in to the array

pete stein
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hong, Your problem had me scratching my head for you are adding a valid Client object to the array, and placing this object in a System.out.println(...) method should automatically call Client's toString method, a method which on brief inspection looks valid. But then I did a debugging trick that might help you in the future: simplify the problem until it stands out. I create a small program that had just the essense of your program:

and still had the same problem. But this code is much smaller and much easier to play with, inspect and dissect. And on doing this, I found that your toString method in fact isn't kosher as it begins with a capital letter -- "ToString()" instead of "toString()". If you try to use the @Override annotation you'll get a compile error, and if you fix this problem, you'll see that your solution does in fact work -- as long as toString() is working correctly.

Sorry about the rambling, and please post back if this doesn't make sense.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!