3

From my understanding, toString() method allows for a conversion of types example: int x = 5, sys....(x.toString()) and it prints 5 to the console. But what is the benefit/downfall of doing this as opposed to my code below? (not saying mine is better, I am genuinely curious)

here is my code:

public class GitFiddle {

    //Initalize class variables.
    private String guitarMake;
    private String guitarModel;
    private int numOfStrings;
    private String notes;
    private int jumboFrets;
    private String neckType;
    private String fingerBoard;
    private String humPickUps;
    private boolean tuned;


    //A constructor that has specific variables assigned to it.
    public GitFiddle (String guitarMake, String guitarModel, int    numOfStrings,
    String notes, int jumboFrets, String neckType, String fingerBoard,
    String humPickUps, boolean tuned) {
        this.guitarMake = guitarMake;
        this.guitarModel = guitarModel;
        this.numOfStrings = numOfStrings;
        this.notes = notes;
        this.jumboFrets = jumboFrets;
        this.neckType = neckType;
        this.fingerBoard = fingerBoard;
        this.humPickUps = humPickUps;
        this.tuned = tuned;
    }

    //Created the output that will be displayed to the user. 
    public String output()
    {
        return "My guitar is an " + guitarMake + "," + " " + guitarModel + "  which is a " + 
        numOfStrings + "-string, electric guitar." + "\nThe standard tuning for this guitar is as follows(from low to high): " 
        + notes + "." + "\nIt has " + jumboFrets + " jumbo frets on a " + neckType + ", a " + fingerBoard + 
        " fingerboard and pearl, dot inlays." + "\nIt also has dual " + humPickUps + 
        " humbucker pickups which is perfect for some sweet metal action!" + 
        "\nIs this 7-string beauty tuned up and ready to play?: " + tuned +   "\nAre you ready?";
    }

    public static void main(String args[])
    {
        //Create an instance of household item method.
        GitFiddle guitar = new GitFiddle ("Ibanez", "S-7 320 EX", 7, "B E A D G B E", 22, "Wizard, W-7 II neck", "rosewood", "EMG 81 85", true);

        //Output the status of the household item.
        System.out.println(guitar.output());
    }
}
  • 1
    From what I understand the toString() method shall return information about the instrument so isn't it exactly what you did in the output() method? – Pramus Feb 17 '16 at 20:49
  • Your `output()` method is essentially doing what `toString()` generally does. Change its name and you have a `toString()` method. – khelwood Feb 17 '16 at 20:49
  • Why even respond with a duplicate answer generated post, if you're not willing to be constructive without being condescending, then why bother replying to a post – 3monkeys1gorilla Feb 23 '16 at 04:54

7 Answers7

10

Change public String output() to public String toString(). You already built it, just gave it another name.

Guy
  • 46,488
  • 10
  • 44
  • 88
  • I would just remove `"My guitar is an " +` as one can expect `toString()` to be impersonal, but I fully agree. – Shlublu Feb 17 '16 at 21:05
5

If I have well understood your point, you want to add a toString() method to your object.

In fact, your output() method can be replaced by the toString() method (which will be called when you want to print your object).

public String toString():
    return "My guitar is an " + guitarMake + "," + " " + guitarModel + "  which is a " + 
    numOfStrings + "-string, electric guitar." + "\nThe standard tuning for this guitar is as follows(from low to high): " 
    + notes + "." + "\nIt has " + jumboFrets + " jumbo frets on a " + neckType + ", a " + fingerBoard + 
    " fingerboard and pearl, dot inlays." + "\nIt also has dual " + humPickUps + 
    " humbucker pickups which is perfect for some sweet metal action!" + 
    "\nIs this 7-string beauty tuned up and ready to play?: " + tuned +   "\nAre you ready?";

and in your main:

System.out.println(guitar); // here toString() method is called to print your guitar object's description.

In addition, toString() method is already implemented (try System.out.println(guitar);) and adding it to your object as above is going to override it.

gallium
  • 301
  • 6
  • 12
4

Every class already has a toString() method. You just need to override it.

@Override
public String toString(){
   return output();
}

or you can rename the output() to toString()

in generally toString() method use for get information about class content. I

Ozan Ertürk
  • 465
  • 5
  • 17
3

there are fancy ways to do it. But if you are just getting started in programming i recomend a simple approach:

Add this to your code.

@Override
public String toString() {
    return "My guitar is an " + guitarMake + "," + " " + guitarModel + "  which is a " + 
    numOfStrings + "-string, electric guitar." + "\nThe standard tuning for this guitar is as follows(from low to high): " 
    + notes + "." + "\nIt has " + jumboFrets + " jumbo frets on a " + neckType + ", a " + fingerBoard + 
    " fingerboard and pearl, dot inlays." + "\nIt also has dual " + humPickUps + 
    " humbucker pickups which is perfect for some sweet metal action!" + 
    "\nIs this 7-string beauty tuned up and ready to play?: " + tuned +   "\nAre you ready?";
}
3

You simply add a function

public String toString(){
 String text = "Your text.";
 return text;
}

to your class. That's it.

Jodn
  • 314
  • 1
  • 11
  • so i like all suggestions, however to keep it simple and get what i feel is the requirement for the assignment and not much more all I did was change: Public String ouput() to Public String toString() and made the System.out.println to reflect the change, i.e. : guitar.toString()); thank you for all the feedback, let's hope I continue my 100% scores on the assignments! – 3monkeys1gorilla Feb 17 '16 at 21:07
3

make new class:

 public class GitFiddle {
    private String guitarMake;
    private String guitarModel;
    private int numOfStrings;
    private String notes;
    private int jumboFrets;
    private String neckType;
    private String fingerBoard;
    private String humPickUps;
    private boolean tuned;

    //A constructor that has specific variables assigned to it.
    public GitFiddle (String guitarMake, String guitarModel, int    numOfStrings,
                      String notes, int jumboFrets, String neckType, String fingerBoard,
                      String humPickUps, boolean tuned) {
        this.guitarMake = guitarMake;
        this.guitarModel = guitarModel;
        this.numOfStrings = numOfStrings;
        this.notes = notes;
        this.jumboFrets = jumboFrets;
        this.neckType = neckType;
        this.fingerBoard = fingerBoard;
        this.humPickUps = humPickUps;
        this.tuned = tuned;
    }


    @Override
    public String toString() {
        return "my guitar Marke is: "+this.guitarMake + " and ,..";
    }

}
2

Answer after taking in suggestions and editing the appropriate portions:

public class GitFiddle {

    //Initalize class variables.
    private String guitarMake;
    private String guitarModel;
    private int numOfStrings;
    private String notes;
    private int jumboFrets;
    private String neckType;
    private String fingerBoard;
    private String humPickUps;
    private boolean tuned;


    //A constructor that has specific variables assigned to it.
    public GitFiddle (String guitarMake, String guitarModel, int numOfStrings, String notes, int jumboFrets, String neckType, String fingerBoard, String humPickUps, boolean tuned) {
        this.guitarMake = guitarMake;
        this.guitarModel = guitarModel;
        this.numOfStrings = numOfStrings;
        this.notes = notes;
        this.jumboFrets = jumboFrets;
        this.neckType = neckType;
        this.fingerBoard = fingerBoard;
        this.humPickUps = humPickUps;
        this.tuned = tuned;
    }

    //Created the output that will be displayed to the user. 
    public String toString()
    {
        return "My guitar is an " + guitarMake + "," + " " + guitarModel + " which is a " + 
        numOfStrings + "-string, electric guitar." + "\nThe standard tuning for this guitar is as follows(from low to high): " 
        + notes + "." + "\nIt has " + jumboFrets + " jumbo frets on a " + neckType + ", a " + fingerBoard + 
        " fingerboard and pearl, dot inlays." + "\nIt also has dual " + humPickUps + 
        " humbucker pickups which is perfect for some sweet metal action!" + 
        "\nIs this 7-string beauty tuned up and ready to play?: " + tuned +  "\nAre you ready?";
    }

    public static void main(String args[])
    {
        //Create an instance of household item method.
        GitFiddle guitar = new GitFiddle ("Ibanez", "S-7 320 EX", 7, "B E A D G B E", 22, "Wizard, W-7 II neck", "rosewood", "EMG 81 85", true);

        //Output the status of the household item.
        System.out.println(guitar.toString());
    }
}

Thanks again all!

  • 2
    That's great you update us :) As this is after the suggestion you received, you don't need to post an answer though. You just have to upvote the answers that help you and to accept the answer that helped you the most, if any. That way, you'll reward all the contributors that helped you :) - Nice and funny exercise, by the way ! – Shlublu Feb 17 '16 at 21:18
  • 1
    haha @shlublu, that's good to know, besides being new to java/programming i am obviously new to stackoverflow, but all answers were upvoted as they all helped and checked the one i used! thanks again – 3monkeys1gorilla Feb 17 '16 at 21:20