aspose file tools*
The moose likes Beginning Java and the fly likes [Solved] toString() issues Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "[Solved] toString() issues" Watch "[Solved] toString() issues" New topic
Author

[Solved] toString() issues

Jason Job
Greenhorn

Joined: Sep 26, 2011
Posts: 17
I'm in my second semester of college and we never really messed with creating our own toString methods in code before. It's all a bit new to me, and google has been less than helpful as I'm just not following where it's coming from.

I have three classes

Workforce
This class's constructor calls a method which uses a while loop. This calls a first name and a last name switch method to choose a random first and last name (out of about 30 names) and adds 20 workers to ArrayList : LaborForce. It will also call a method to generate a random employee number, a random salary (hourly and yearly), and year they started working at the company.
It then runs a second while loop while the index is less than 20 to return all 20 employees and their employee numbers along with the rest of the information.
So far this has just generated the memory address for the "ProductionWorker", my current code to output this information is just
System.out.println(LaborForce.get(index));
and such is my problem


Employee
Is a superclass of class Productionworker, this class requests an input for Name, employee#, year, hourlywage
it has a toString() method that returns all of these fields.

ProductionWorker
is a subclass of class Employee and also has a toString method which calls the super's tostring method. This also uses a super call in the constructor for the name, enumber, etc.

Please help! Thanks
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10403
    
    8

Welcome to the Ranch.
I read your post twice and still can't understand what your question is. Could you please rephrase?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2

So you don't know how to override/write toString method?
Jason Job
Greenhorn

Joined: Sep 26, 2011
Posts: 17
Sigh, I had a lot typed out here when my browser freaked out.
I realized that netbeans wasn't saving the changes I made to the project (for whatever reason) it seemed to be saving the information to a seperate save of the class with the same name, outside of my project. I've been spoiled with BlueJ as of thus far, and am trying to branch out into other editors. However I have having trouble creating test classes to use.

My main question about the toString() method, is that when creating it in a class does it need to be then called elsewhere? Or does it override another method and then take care of business itself when you system.out.println?

Also, could anyone help me design a test class for this so that I would know what to do in the future? I basically just want to make sure that when the Workforce constructor is called, it outputs what I want.


Employee


Production Worker


WorkForce
Jason Job
Greenhorn

Joined: Sep 26, 2011
Posts: 17
Alright, scratch that last response. I made a test class



and my output is mostly fine. Just need to fix a few things. Looks like the toString override worked just great. Yay for netbeans messing with me
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10403
    
    8

I have edited your last two posts.
Please UseCodeTags (code instead of quote)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
You can create an array of names and use the "random" number to select from the array. That is more object-oriented than a switch. You can use nextInt(myArray.length) and it will match the array perfectly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [Solved] toString() issues