Mason Walton
Mason Walton

Reputation: 129

Java - Print Contents of an Object

I am trying to print the contents of a of my object Fraction(), but it keeps giving me a lot of errors and referring me back to where my print statements start. I have a toString() method that I created, but that does not seem to work either. Here is my class and tester class:

 package fraction;

    public class Fraction 
    {
        /**
         * The numerator and denominator.
         */
        private int top;
        private int bottom;

        /**
         *  Creates the fraction 0/1
         */
        public Fraction()
        {
            top = 0;
            bottom = 1;
        }

        /**
         * Creates the fraction n/1
         * @param n an integer
         */
        public Fraction(int n)
        {
            top = n;
            bottom = 1;
        }

        /**
         * Creates a fraction with the specified numerator and denominator
         * @param n the numerator
         * @param d the denominator
         */
        public Fraction(int n, int d)
        {
            top = n;
            bottom = d;

            if (bottom == 0) {
                throw new IllegalArgumentException();
            }
        }

        /**
         * Computes the sum of this fraction and the specified fraction 
         * @param f a fraction
         * @return the fraction obtained by adding this fraction from the specified fraction
         */
        public Fraction add(Fraction f)
        {
            return new Fraction(this.numerator() * f.denominator() + f.numerator() * this.denominator());
        }

        /**
         * Compares this fraction and the specified fraction
         * @param f a fraction
         * @return 0 when this fraction is equal to the specified fraction; 1 when this fraction is greater than the specified 
         * fraction; -1 otherwise
         */
        public int compareTo(Fraction f)
        {
            if (top > f.numerator())
                return 1;
            else if (top < f.numerator())
                return -1;
            else if (bottom > f.denominator())
                return 1;
            else if (bottom < f.denominator())
                return -1;
            else
                return 0;
        }

        /**
         * Gives the denominator of this fraction
         * @return the denominator of this fraction
         */
        public int denominator()
        {
            return bottom;
        }

        /**
         * Gives the quotient of this fraction and the specified fraction
         * @param f a fraction
         * @return the fraction obtained by dividing this fraction by the specified fraction
         * @throws IllegalArgumentException when the numerator equals 0
         */
        public Fraction divide(Fraction f)
        {
            if (f.numerator() == 0) {
                throw new IllegalArgumentException();
            }
            else
            {
                return new Fraction(this.numerator() * f.numerator(), this.denominator() * f.denominator());
            }
        }

        /**
         * Determines whether this fraction is equal to the specified fraction 
         * @param f a fraction
         * @return true when this fraction is equal to the specified fraction; otherwise, false
         */
        public boolean equals(Fraction f)
        {
            return top == f.numerator() && bottom == f.denominator();
        }

        /**
         * Computes the greatest common divisor of the specified numbers
         * @param num1 an integer
         * @param num2 an integer
         * @return the greatest common divisor of the specified parameters 
         */
        private int gcd(int num1, int num2)
        {
            if (num2 == 0) {
                return num1;
            }
            else
            {
                return gcd(num2, num1%num2);
            }
        }

        /**
         * Calculates the product of this fraction and the specified fraction
         * @param f a fraction
         * @return the product of this fraction and the specified fraction
         */
        public Fraction multiply(Fraction f)
        {
            return new Fraction(this.numerator() * f.numerator(), this.denominator() * f.denominator());
        }

        /**
         * Simplifies this fraction by expressing its numerator and denominator in standard form: 
         * the denominator is positive and the numerator and denominator are relative primes
         */
        public void normalize()
        {
            int gcd = gcd(top,bottom);
            top /= gcd;
            bottom /= gcd;
        }

        /**
         * Gives the numerator of this fraction 
         * @return the numerator of this fraction
         */
        public int numerator()
        {
            return top;
        }

        /**
         * Computes the reciprocal of this fraction 
         * @return the reciprocal of this fraction
         * @throws IllegalArgumentException when the numerator is equal to 0
         */
        public Fraction reciprocal()
        {
            if (top == 0) {
                throw new IllegalArgumentException();
            }

            int temp;
            temp = numerator();
            top = denominator();
            bottom = temp;
            return new Fraction(top, bottom);
        }

        /**
         * Modifies this fraction
         * @param n the new numerator of this fraction
         * @param d the new denominator of this fraction
         * @throws IllegalArgumentException when the denominator equals 0
         */
        public void setFraction(int n, int d)
        {
            if (d == 0) {
                throw new IllegalArgumentException();
            }

            top = n;
            bottom = d;
        }

        /**
         * Computes the difference of this fraction and the specified fraction
         * @param f a fraction
         * @return the fraction obtained by subtracting this fraction from the specified fraction
         */
        public Fraction subtract(Fraction f)
        {
            return new Fraction(this.numerator() * f.denominator() - f.numerator() * this.denominator(), this.denominator() * f.denominator());
        }

        /**
         * Gives a string representing this fraction in in-line notation, numerator/denominator 
         * @return a string representing this fraction in in-line notation, numerator/denominator 
         */
        @Override public String toString()
        {
            return String.format("%d/%d",top,bottom);
        }
    }

package fraction;

import java.util.Scanner;

public class FractionTester 
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);

        System.out.print("Enter the numerator and denominator of f1 > ");
        int n1 = in.nextInt(), d1 = in.nextInt();
        System.out.print("Enter the numerator and denominator of f2 > ");
        int n2 = in.nextInt(), d2 = in.nextInt();
        System.out.print("Enter the numerator and denominator of f3 > ");
        int n3 = in.nextInt(), d3 = in.nextInt();
        System.out.print("Enter the numerator and denominator of f4 > ");
        int n4 = in.nextInt(), d4 = in.nextInt();
        System.out.println();

        Fraction f1 = new Fraction(n1, d1);
        Fraction f2 = new Fraction(n2, d2);
        Fraction f3 = new Fraction(n3, d3);
        Fraction f4 = new Fraction(n4, d4);

        System.out.printf("f1 = %d", f1);
        System.out.printf("f2 = %d", f2);
        System.out.printf("f3 = %d", f3);
        System.out.printf("f4 = %d", f4);

        if (f1.compareTo(f2) == 0) {
            System.out.println("f1 = f2");
        }
        else if (f1.compareTo(f2) < 0) {
            System.out.println("f1 < f2");
        }
        else {
            System.out.println("f1 > f2");
        }

        if (f3.compareTo(f4) == 0) {
            System.out.println("f1 = f2");
        }
        else if (f3.compareTo(f4) < 0) {
            System.out.println("f1 < f2");
        }
        else {
            System.out.println("f1 > f2");
        }
        System.out.println();


    }
}

I am trying to print the contents of the objects f1, f2, f3, and f4. It says my error is on the first print statement (System.out.printf("f1 = %d", f1);).

Upvotes: 3

Views: 667

Answers (2)

Christian Hujer
Christian Hujer

Reputation: 17945

Your fraction is not a number (%d) but an Object of which the toString() method should be called. Change %d to %s and it will work, i.e.

System.out.printf("f1 = %s", f1);

Upvotes: 2

Nir Alfasi
Nir Alfasi

Reputation: 53525

That's not how toString() works, change:

System.out.printf("f1 = %d", f1)

to:

System.out.println("f1 = "+ f1)

When you print an object its toString() will be called, which will return a String, while you where trying to print %d (a digit).

Upvotes: 1

Related Questions