Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Unexpected Output Format

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Unexpected Output Format

    Hi. I have been using the book "An Introduction to Programming Using Java," by Anthony Dos J. Reis, 2012 Edition, ISBN-13: 978-0-7637-9060-8, ISBN-10: 0-7637-9060-5. This is a textbook but I am using this on my own to learn how to program in Java. I have some prior experience with programming in different languages such as PBasic, Fortran, Pascal, Processing, and Arduino.

    The last Homework question, question #4, in Chapter 6 requires the programmer to construct a program that manipulates two integers to represent rational numbers. It has a number of methods for which I have written two in the first class called <class RationalNumber> and the main method. These methods have been written to be part of the whole program and at this point include testing statements so values in the program can be monitored.

    In the output there is an output statement for which I do not know the format.

    Here is the whole program between the asterisks and the next part is the output, also between asterisks (the asterisks are not part of the output statements):
    *****
    /*
    This program manipulates rational numbers by making use of ratios of integers.
    */
    class RationalNumber
    {
    private int numerator, denominator;
    //------------------------
    public RationalNumber(int n, int d)
    {
    numerator = n;
    denominator = d;
    System.out.println(numerator); // test statement
    System.out.println(denominator);// test statement
    }
    //---------------------------
    public RationalNumber add(RationalNumber r)
    {
    return new RationalNumber(((r.numerator*denominator)+(numerat or*denominator)) , (r.denominator*denominator));
    }
    //---------------------------
    /* public RationalNumber multiply(RationalNumber r)
    {

    }
    //----------------------------
    public void reduce()
    {

    }
    //----------------------------
    public String toString()
    {

    }
    */
    }
    //--------------------------------------
    class C6h4MyEdit
    {
    public static void main(String[] args)
    {
    RationalNumber a = new RationalNumber(3, 7); // first call setting numerator = 3, denominator = 7
    RationalNumber b = new RationalNumber(7, 3); // second call setting nuerator = 7, denominator = 3
    RationalNumber c = b.add(a);
    System.out.println(c);
    }
    }
    *****
    3
    7
    7
    3
    30
    21
    RationalNumber@70d1c9b5
    *****

    It is the last statement, RationalNumber@70d1c9b5, for which I do not know the format. Note that much of the program is commented out but included, ready to be used for programming. The methods are as stated in the question in the book. Is this some kind of location in memory or a particular format? Changing the output from RationalNumber c to RationalNumber b produces the same output statement.

    I am running Java from the command line in Linux Mint 12.


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Unexpected Output Format

    Quote Originally Posted by playinmyblues View Post
    Hi. I have been using the book ...
    I don't have that book, but here is what I found browsing through a description of the String class at the Java Docs Mother ship: Description of String.toString() method:
    "toString---Returns a string representation of the object. In general, the toString method returns a string that 'textually represents' this object...It is recommended that all subclasses override this method..."

    So, here's the thing:

    System.out.print() and System.out.println() expect their arguments to be Strings. If the arguments are basic data types like ints or doubles (or whatever...) the values are "automatically" converted to Strings suitable for printing, and all is Good.

    However...

    If an argument is a class object, the object is converted to a string by the class toString() method. If you don't supply a toString() method for your class, you get a string that is something like what you saw: The class name and an @ sign and some kind of hash code that the compiler knows about for an object (but is totally irrelevant to "normal" programs and "normal" programmers).


    So...

    If you want to use System.out.print() or System.out.println() to print your rational number that looks like "3 / 7" or some such thing, then you can implement a toString() method in your RationalNumber class definition file.

    For example for your class, you can define your toString() method to return a string whose value is numerator + "/" + denominator

    The main() function, then, can be something like
        public static void main(String[] args)
        {
            RationalNumber a = new RationalNumber(3, 7);
            System.out.println("a = " + a);
            RationalNumber b = new RationalNumber(7, 3);
            System.out.println("b = " + b);
            RationalNumber c = b.add(a);
            System.out.println("c = " + c);
        }

    And you should see something like
    3
    7
    a = 3/7
    7
    3
    b = 7/3
    30
    21
    c = 30/21


    Where the unlabeled output lines are from the debug print statements in your constructor. (As a matter of personal style, I always make output statements verbose enough to actually tell me something about what they mean. But maybe that's just me. I'm funny that way.)


    Cheers!

    Z
    Last edited by Zaphod_b; June 13th, 2012 at 02:01 PM.

  3. #3
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Unexpected Output Format

    You need to write a toString() method in your RationalNumber class

  4. #4
    Junior Member
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Unexpected Output Format

    Thank you for the answers. By the way, I think I have an error in my code that is not syntax but that would not effect the output that I described.

  5. #5
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Unexpected Output Format

    Quote Originally Posted by playinmyblues View Post
    ...I think I have an error ...
    Well, the result that came out of the main() that I showed was certainly incorrect, but I was trying to answer your question about how to display the rational number, not how to get the correct answer from the mathematical operation. (That's why I also printed out the original values from the constructor as well as the result of the addition. It verified that it can show the actual numbers.)

    Anyhow...

    After adding the toString() method to your class and running the test program, you have something to look at, and maybe you can figure out why it says 30/21 instead of the correct answer, 58/21. (Maybe you have figured it out already.)

    Now, the next step will be to reduce the fraction to lowest terms, right? I mean, 58/21 is as good as it gets (I think), but what if you add 2/3 and 1/6. You don't really want to see 15/18 do you? Wouldn't you rather have 5/6? How will you approach that little problem? (Or is that not part of the assignment?)


    Cheers!

    Z
    Last edited by Zaphod_b; June 13th, 2012 at 07:00 PM.

Similar Threads

  1. Having Java output in Tabular Format
    By jo15765 in forum Java Theory & Questions
    Replies: 17
    Last Post: May 3rd, 2012, 03:42 PM
  2. Issue with output format
    By mael331 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 25th, 2011, 02:12 PM
  3. Format difference in System.out Vs. file output
    By mwr76 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 10th, 2011, 09:13 PM
  4. Format Java output
    By wildman0501 in forum Java Theory & Questions
    Replies: 2
    Last Post: June 11th, 2011, 06:09 PM
  5. Unexpected ArrayOutOfBoundsError
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 28th, 2010, 04:00 AM