2014-11-23 72 views
3

我试图打印我的对象Fraction()的内容,但它一直给我很多错误,并将我引回到我的打印语句开始处。我有一个我创建的toString()方法,但这似乎也不起作用。这是我的阶级和Tester类:Java - 打印一个对象的内容

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);)上。

回答

1

这不是如何toString()作品,变化:

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

到:

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

当您打印对象的toString()将被调用,它会返回一个字符串,而你在哪里试图打印%d(一位数字)。

+0

您还可以使用System.out.format( “F1 =%S%N”,F1); – user1879313 2014-11-23 18:35:46

+0

谢谢,修复它。 – 2014-11-23 18:38:59

2

你的分数不是一个数字(%d),而是一个Object,其中toString()方法应该被调用。更改%d%s,它会工作,即

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