This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes element of the array from another class Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "element of the array from another class" Watch "element of the array from another class" New topic
Author

element of the array from another class

Nattakan Lukkanapinit
Greenhorn

Joined: Apr 24, 2010
Posts: 16
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
Bartender

Joined: Feb 23, 2007
Posts: 1561
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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: element of the array from another class
 
Similar Threads
Overriding equals() method within a subclass
cannot read a Collection of a custom class as a parameter
cannot find symbol?......wats happening???
Object arrays?
static insights?