• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[Solved] toString() issues

 
Jason Job
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.
I read your post twice and still can't understand what your question is. Could you please rephrase?
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you don't know how to override/write toString method?
 
Jason Job
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have edited your last two posts.
Please UseCodeTags (code instead of quote)
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic