Results 1 to 7 of 7
Thread: toString method
 01302008, 04:33 AM #1Member
 Join Date
 Dec 2007
 Posts
 26
 Rep Power
 0
toString method
I'm completely confused on how to make a working toString method. Here's what I have. This program inputs 4 variables which make fractions and add, subtract, multiply, and divide them. It's a pretty simple thing but, making the toString method and showing it is confusing me. Here it is.
Java Code:/** * @(#)prog53.java * * * @author * @version 1.00 2008/1/29 */ import java.util.*; import static java.lang.System.*; public class prog53 { public static void main(String args[]) { Fractions Tom = new Fractions(); Tom.enterData(); Tom.add(); Tom.subtract(); Tom.multiply(); Tom.divide(); } } class Fractions{ double n1, d1, n2, d2; public void enterData() { Scanner input = new Scanner(System.in); out.println("Enter First Numerator: "); n1 = input.nextDouble(); out.println("Enter First Denominator"); d1 = input.nextDouble(); out.println("Enter Second Numerator: "); n2 = input.nextDouble(); out.println("Enter Second Denominator: "); d2 = input.nextDouble(); } public void add() { double add; add = ((n1/d1) + (n2/d2)); out.println("Adding results: " +add); } public void subtract() { double sub; sub = (((n1*d2)  (n2*d1)) / (d1*d2)); out.println("Subtracting results: " +sub); } public void multiply() { double mult; mult = (n1*n2) / (d1*d2); out.println("Multiplying results: " +mult); } public void divide() { double div; div = (n1*d2) / (n2*d1); out.println("Dividing results: " +div); } public String toString(double add, double sub, double mult, double div) { add.toString(); } //pg 165 }
Last edited by apfroggy0408; 01302008 at 04:43 AM.
 01302008, 06:18 AM #2
The toString method is called by the jvm and returns a string that represents the state of the class instance. The member variables of this class are of interest so we return them, like this:
Java Code:public String toString() { return getClass().getName() + "[n1:"+n1+", d1:"+d1+", n2:"+n2+", d2:"+d2+"]"; }
Java Code:Fractions fractions = new Fractions(); fractions.enterData(); System.out.println("fractions = " + fractions); fractions.add(); System.out.println("fractions = " + fractions);
 01302008, 08:45 AM #3Member
 Join Date
 Jan 2008
 Location
 Pune
 Posts
 1
 Rep Power
 0
there is a small mistake in your toString() method.
public String toString(double add, double sub, double mult, double div)
{
add.toString(); < (mistake)
}
just typecast it into "Double" and then do whatever you want to do,like ...
public String toString(double add, double sub, double mult, double div)
{
(Double)add.toString(); //now "add" is an object of the type Double
}
 01312008, 01:34 AM #4Member
 Join Date
 Dec 2007
 Posts
 26
 Rep Power
 0
Wonderful, thanks a lot. I switched around the code you gave me hardwired and suited it to my needs. Here it is.
Java Code:/** * @(#)prog53.java * * * @author * @version 1.00 2008/1/29 */ import java.util.*; import static java.lang.System.*; public class prog53 { public static void main(String args[]) { Answers answers = new Answers(); answers.enterData(); answers.add(); answers.subtract(); answers.multiply(); answers.divide(); System.out.println(""+ answers); } } class Answers{ double n1, d1, n2, d2, add, sub, mult, div; public void enterData() { Scanner input = new Scanner(System.in); out.println("Enter First Numerator: "); n1 = input.nextDouble(); out.println("Enter First Denominator"); d1 = input.nextDouble(); out.println("Enter Second Numerator: "); n2 = input.nextDouble(); out.println("Enter Second Denominator: "); d2 = input.nextDouble(); out.println(""); } public void add() { add = ((n1/d1) + (n2/d2)); } public void subtract() { sub = (((n1*d2)  (n2*d1)) / (d1*d2)); } public void multiply() { mult = (n1*n2) / (d1*d2); } public void divide() { div = (n1*d2) / (n2*d1); } public String toString() { return getClass().getName() + "[Addition:"+add+", Subtracting:"+sub+", Multiplying:"+mult+", Dividing:"+div+"]"; } //pg 165 }
 01312008, 02:36 AM #5Member
 Join Date
 Dec 2007
 Posts
 26
 Rep Power
 0
New question. How can i make a variable show two other variables with quotes in between. Here's what I'm trying to do.
Java Code:addn = ((n1*d2) + (n2*d1)); addd = (d1*n2); out.println(+addn); out.println(+addd); addfrac = addn +"/" addd; < is where i'm trying to pull it off but it's not working
Last edited by apfroggy0408; 01312008 at 03:03 AM.
 01312008, 03:56 AM #6Member
 Join Date
 Dec 2007
 Posts
 26
 Rep Power
 0
Figured it out how to show it, but it was through the toString method. If someone could still show me how to do it the way above, that would be very nice.
 01312008, 04:08 AM #7
Similar Threads

method not abstract, does not override actionperformed method.
By Theman in forum New To JavaReplies: 2Last Post: 03262010, 05:12 PM 
Method Help
By pringle in forum New To JavaReplies: 4Last Post: 04162008, 01:23 PM 
Inheritance, methods, and toString...
By Kreuz14 in forum New To JavaReplies: 1Last Post: 04012008, 10:12 PM 
Arrays.toString
By Java Tip in forum Java TipReplies: 0Last Post: 12012007, 09:35 PM 
Can i just use toString?
By cachi in forum New To JavaReplies: 1Last Post: 07312007, 08:32 PM
Bookmarks