Reputation: 67
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());
}
}
Upvotes: 3
Views: 853
Reputation: 487
make a 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 brand is: "+this.guitarMake + " and ,..";
}
}
Upvotes: 3
Reputation: 67
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!
Upvotes: 2
Reputation: 485
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
Upvotes: 4
Reputation: 301
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.
Upvotes: 5
Reputation: 50809
Change public String output()
to public String toString()
. You already built it, just gave it another name.
Upvotes: 10
Reputation: 324
You simply add a function
public String toString(){
String text = "Your text.";
return text;
}
to your class. That's it.
Upvotes: 3
Reputation: 525
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?";
}
Upvotes: 3