Java - 打印对象的内容

时间:2014-11-23 18:25:59

标签: java class object methods

我正在尝试打印我的对象Fraction()的内容,但它一直给我很多错误,并将我引回到我的print语句的起始位置。我有一个我创建的toString()方法,但这似乎也不起作用。这是我的班级和测试员班级:

 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();


    }
}

我正在尝试打印对象f1,f2,f3和f4的内容。它说我的错误是在第一个打印语句(System.out.printf("f1 = %d", f1);)上。

2 个答案:

答案 0 :(得分:2)

您的分数不是数字(%d),而是Object,应该调用toString()方法。将%d更改为%s即可,即

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

答案 1 :(得分:1)

那不是toString()的工作原理,改变:

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

为:

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

当您打印一个对象时,将调用toString(),当您尝试打印%d(一个数字)时,将调用该字符串。