Help with code please-Fisherman

need help making a to string for output method

Page 1 of 1

7 Replies - 645 Views - Last Post: 04 November 2009 - 07:29 PM Rate Topic: -----

#1 bedrockhorror  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 56
  • Joined: 22-October 09

Help with code please-Fisherman

Posted 04 November 2009 - 11:27 AM

Hey guys my instructor wants me to have my System.out.Println as a to string method. How do I go about switching it into a to string? I believe he wants the output to display like System.out.println(man1.toString()); but I dont know how to go about changing it to that.



public class FishermanDriver
{
		public static void main(String [] args )
		{
				// Create objects
				Fisherman man1 = new Fisherman();
				Fisherman man2 = new Fisherman();
				Fisherman man3 = new Fisherman();
				// Catch fishes
				man1.catchFish();
				man2.catchFish();
				man3.catchFish();
				// Display output
				System.out.println("Fisherman 1 caught: " + man1.getFishCaught() );
				System.out.println("Fisherman 2 caught: " + man2.getFishCaught() );
			   System.out.println("Fisherman 3 caught: " + man3.getFishCaught() );

		   System.out.println("Total Number of Fish caught by group: "
							  + Fisherman.getTotalFishCaught() );


		}
}



This post has been edited by bedrockhorror: 04 November 2009 - 12:17 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Help with code please-Fisherman

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Help with code please-Fisherman

Posted 04 November 2009 - 11:42 AM

I have no idea what you are asking, they look like strings to me.
Was This Post Helpful? 0
  • +
  • -

#3 bedrockhorror  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 56
  • Joined: 22-October 09

Re: Help with code please-Fisherman

Posted 04 November 2009 - 12:16 PM

View Postbedrockhorror, on 4 Nov, 2009 - 10:27 AM, said:

Hey guys my instructor wants me to have my System.out.Println as a to string method. How do I go about switching it into a to string? I believe he wants the output to display like System.out.println(man1.toString()); but I dont know how to go about changing it to that.



public class FishermanDriver
{
		public static void main(String [] args )
		{
				// Create objects
				Fisherman man1 = new Fisherman();
				Fisherman man2 = new Fisherman();
				Fisherman man3 = new Fisherman();
				// Catch fishes
				man1.catchFish();
				man2.catchFish();
				man3.catchFish();
				// Display output
				System.out.println("Fisherman 1 caught: " + man1.getFishCaught() );
				System.out.println("Fisherman 2 caught: " + man2.getFishCaught() );
			   System.out.println("Fisherman 3 caught: " + man3.getFishCaught() );

		   System.out.println("Total Number of Fish caught by group: "
							  + Fisherman.getTotalFishCaught() );


		}
}



Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Help with code please-Fisherman

Posted 04 November 2009 - 12:19 PM

And what did quoting your original post add to the discusion?
Was This Post Helpful? 0
  • +
  • -

#5 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2728
  • View blog
  • Posts: 15,806
  • Joined: 24-September 08

Re: Help with code please-Fisherman

Posted 04 November 2009 - 12:21 PM

:rolleyes:

Okay, a toString() is just a way of formatting text the way you want. And you would call it man1.toString(); and in the toString() method would be the System.out.println().
Was This Post Helpful? 0
  • +
  • -

#6 sethkrepps  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 24-October 09

Re: Help with code please-Fisherman

Posted 04 November 2009 - 04:40 PM

So does your instructor want you to use a toString method to print out what

				System.out.println("Fisherman 1 caught:  " + man1.getFishCaught() );
				System.out.println("Fisherman 2 caught: " + man2.getFishCaught() );
			   System.out.println("Fisherman 3 caught: " + man3.getFishCaught() );

		   System.out.println("Total Number of Fish caught by group: "
							  + Fisherman.getTotalFishCaught() );



prints?

if so, You'll have to add to your class, and ill gladly help with that...but I'm not quite sure what you are asking?

like, within the class, does s/he want

	 public String toString(){
		  System.out.println("Fisherman 1 caught: " + this.getFishCaught() );
	 }



??

please elaborate. =]

This post has been edited by sethkrepps: 04 November 2009 - 04:41 PM

Was This Post Helpful? 0
  • +
  • -

#7 bedrockhorror  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 56
  • Joined: 22-October 09

Re: Help with code please-Fisherman

Posted 04 November 2009 - 05:06 PM

He wanted me to do this

public String toString() {
return fishCaught + ""; // convert fishCaught to a String and return it
}



Then for my Driver use this instead of having .getfishcaught just use +man1 instead

System.out.println("Fisherman 1 caught: " + man1 );



Sorry for the confusion guys, that was just the best way that I knew to ask it.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Help with code please-Fisherman

Posted 04 November 2009 - 07:29 PM

View Postbedrockhorror, on 4 Nov, 2009 - 04:06 PM, said:

He wanted me to do this

public String toString() {
return fishCaught + ""; // convert fishCaught to a String and return it
}



Then for my Driver use this instead of having .getfishcaught just use +man1 instead

System.out.println("Fisherman 1 caught: " + man1 );



Sorry for the confusion guys, that was just the best way that I knew to ask it.


public String toString() {
return "" + fishCaught; // convert fishCaught to a String and return it
}


will do the job
now you can

System.out.println("Fisherman 1 caught: " + man1 );

without problem
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1