Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-30-2008, 06:33 AM
Member
 
Join Date: Dec 2007
Posts: 26
apfroggy0408 is on a distinguished road
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.

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 }
Edit: The regular objects work fine when calling the methods. But again, I don't know how to make a toString method. Thanks for the help.

Last edited by apfroggy0408 : 01-30-2008 at 06:43 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-30-2008, 08:18 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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:
Code:
public String toString() { return getClass().getName() + "[n1:"+n1+", d1:"+d1+", n2:"+n2+", d2:"+d2+"]"; }
You use/test this inside your main method like this:
Code:
Fractions fractions = new Fractions(); fractions.enterData(); System.out.println("fractions = " + fractions); fractions.add(); System.out.println("fractions = " + fractions);
In the println statement the jvm calls the toString method for the "fractions" instance. If you did not provide the toString override in your Fractions class the jvm would call the toString method of its superclass (Object) which would return the class name and a hex address of the instance in memory. You can observe this by commenting–out the toString method and running your test again.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-30-2008, 10:45 AM
Member
 
Join Date: Jan 2008
Location: Pune
Posts: 1
babadamale is on a distinguished road
Send a message via Yahoo to babadamale
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
}
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-31-2008, 03:34 AM
Member
 
Join Date: Dec 2007
Posts: 26
apfroggy0408 is on a distinguished road
Wonderful, thanks a lot. I switched around the code you gave me hardwired and suited it to my needs. Here it is.

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 }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-31-2008, 04:36 AM
Member
 
Join Date: Dec 2007
Posts: 26
apfroggy0408 is on a distinguished road
New question. How can i make a variable show two other variables with quotes in between. Here's what I'm trying to do.

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
If it doesn't make sense, tell me and I'll try to explain better.

Last edited by apfroggy0408 : 01-31-2008 at 05:03 AM.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-31-2008, 05:56 AM
Member
 
Join Date: Dec 2007
Posts: 26
apfroggy0408 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-31-2008, 06:08 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Code:
double addn = 25.0; double addd = 12.25; String addfrac = String.valueOf(addn) + "/" + addd; String s = String.format("%f/%f", addn, addd); System.out.println(s);
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Method Help pringle New To Java 4 04-16-2008 03:23 PM
Inheritance, methods, and toString... Kreuz14 New To Java 1 04-02-2008 12:12 AM
Arrays.toString Java Tip Java Tips 0 12-01-2007 11:35 PM
Can i just use toString? cachi New To Java 1 07-31-2007 10:32 PM
method not abstract, does not override actionperformed method. Theman New To Java 1 05-08-2007 08:13 AM


All times are GMT +3. The time now is 04:42 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org