Results 1 to 4 of 4

Thread: Urgent Help with return method toString() Java!!

  1. #1
    Join Date
    Apr 2011
    Posts
    2

    Urgent Help with return method toString() Java!!

    Hi everyone, can anyone help me with this question? Im a newbie. I know this not a hard question but i just dont know how to combine the name and position together as required. This is the question:

    Define a method called toString() which is public, takes no parameters and returns a string of the form:

    apple(3) orange(5) cherry(9) player(1) – 0 points

    The first 3 components display the names and positions of the 3 item objects. The last part, “player(1) – 0 points” displays information about the position of the player (in this case, 1) and the number of points (in this case, 0). This method should not print the string; only return it as the method's result.

    Thanks in advance.

  2. #2
    Join Date
    Jun 2006
    Posts
    623

    Re: Urgent Help with return method toString() Java!!

    Implementing toString method in java is done by overriding the Object’s toString method. The java toString() method is used when we need a string representation of an object. It is defined in Object class. This method can be overridden to customize the String representation of the Object. Below is a program showing the use of the Object’s Default toString java method.

    Code:
    class PointCoordinates {
    
    	private int x, y;
    	public PointCoordinates(int x, int y) {
    		this.x = x;
    		this.y = y;
    	}
    	public int getX() {
    		return x;
    	}
    	public int getY() {
    		return y;
    	}
    }
    
    public class ToStringDemo {
    
    	public static void main(String args[]) {
    		PointCoordinates point = new PointCoordinates(10, 10);
    		// using the Default Object.toString() Method
    		System.out.println("Object toString() method : " + point);
    		// implicitly call toString() on object as part of string concatenation
    		String s = point + " testing";
    		System.out.println(s);
    	}
    }

  3. #3
    Join Date
    Apr 2011
    Posts
    2

    Re: Urgent Help with return method toString() Java!!

    Thanks for the information. Actually i got the general idea about return method, toString. However i just dont know how to write the the return method in the required form. Can you just give me an exactly answer on the question above as an example?
    Thanks alot

  4. #4
    Join Date
    Dec 2007
    Posts
    2,291

    Re: Urgent Help with return method toString() Java!!

    One of the standard methods defined in java.lang.Object is toString. This method is used to obtain a string representation of an object. You can (and normally should) override this method for classes that you write. For more information you can refer to this site. Hope that helps you out.

Similar Threads

  1. what are the possible return types of a main method?
    By \"Dritan\" in forum Software Development
    Replies: 3
    Last Post: 08-01-2011, 03:50 AM
  2. How to override ToString() method in C#?
    By Linoo in forum Software Development
    Replies: 5
    Last Post: 23-02-2010, 07:54 PM
  3. What is toString() method in java?
    By Juany in forum Software Development
    Replies: 4
    Last Post: 10-02-2010, 05:12 PM
  4. How to return value for void method?
    By Klaty in forum Software Development
    Replies: 5
    Last Post: 21-01-2010, 11:42 AM
  5. Permission denied to call method Location.toString
    By HiSpeed in forum Software Development
    Replies: 3
    Last Post: 15-05-2009, 02:00 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,630,504.89867 seconds with 17 queries