java 打印对象_Java-打印对象的内容

我正在尝试打印我的对象Fraction()的内容,但是它一直给我带来很多错误,并将我引回到我的打印语句开始的地方.我有一个我创建的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);).

解决方法:

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

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

至:

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

当您尝试打印%d(数字)时,当您打印对象时,其toString()将被调用,该方法将返回String.

标签:java,class,object,methods

来源: https://codeday.me/bug/20191121/2048552.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值