Java:分数运算(类与对象)

题目内容:

设计一个表示分数的类Fraction。这个类用两个int类型的变量分别表示分子和分母。

这个类的构造函数是:

Fraction(int a, int b)

    构造一个a/b的分数。

这个类要提供以下的功能:

double toDouble();

    将分数转换为double

Fraction plus(Fraction r);

    将自己的分数和r的分数相加,产生一个新的Fraction的对象。注意小学四年级学过两个分数如何相加的哈。

Fraction multiply(Fraction r);

    将自己的分数和r的分数相乘,产生一个新的Fraction的对象。

void print();

    将自己以“分子/分母”的形式输出到标准输出,并带有回车换行。如果分数是1/1,应该输出1。当分子大于分母时,不需要提出整数部分,即31/30是一个正确的输出。

注意,在创建和做完运算后应该化简分数为最简形式。如2/4应该被化简为1/2。

你写的类要和以下的代码放在一起,并请勿修改这个代码:

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

Fraction a = new Fraction(in.nextInt(), in.nextInt());

Fraction b = new Fraction(in.nextInt(),in.nextInt());

a.print();

b.print();

a.plus(b).print();

a.multiply(b).plus(new Fraction(5,6)).print();

a.print();

b.print();

in.close();

}

}

注意,你的类的定义应该这样开始:

class Fraction {

也就是说,在你的类的class前面不要有public。

输入格式:

程序运行时会得到四个数字,分别构成两个分数,依次是分子和分母。

输出格式:

输出一些算式。这些输入和输出都是由Main类的代码完成的,你的代码不要做输入和输出。

输入样例:

2 4 1 3

输出样例:

1/2

1/3

5/6

1

1/2

1/3

时间限制:500ms内存限制:32000kb

误点

一定要注意不要在函数里面改变传进来的值。

实现代码


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		Fraction a = new Fraction(in.nextInt(), in.nextInt());
		Fraction b = new Fraction(in.nextInt(),in.nextInt());
		a.print();
		b.print();
		a.plus(b).print();
		a.multiply(b).plus(new Fraction(5,6)).print();
		a.print();
		b.print();
		in.close();
	}

}

class Fraction{
	int a,b;
	Fraction(int a,int b)
	{
		this.a = a;
		this.b = b;
	}
	
	double toDouble()
	{
		return (double)a/b;
	}
	
	Fraction plus(Fraction r)
	{
		Fraction s = new Fraction(r.a, r.b);
		int h = s.b * a;
		s.a *= b;
		s.b *= b;
		s.a += h;
		return s;
	}
	
	Fraction multiply(Fraction r)
	{
		r.a *= a;
		r.b *= b;
		return r;
	}
	
	void print()
	{
		int h;
		if(a >= b) h = gcd(a,b);
		else h = gcd(b,a);
		a /= h;
		b /= h;
		if(a%b == 0)
			System.out.println(a/b);
		else
			System.out.println(a+"/"+b);	
	}
	
	int gcd(int m,int n)
	{
		if(m%n==0) return n;
		return gcd(n,m%n);
	}
}

  • 11
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
下面是一个简单的Fraction类的实现: ```java public class Fraction { private int f1; // 分子 private int f2; // 分母 // 构造方法 public Fraction(int f1, int f2) { this.f1 = f1; this.f2 = f2; } // 分数相加 public Fraction add(Fraction other) { int newF1 = this.f1 * other.f2 + other.f1 * this.f2; int newF2 = this.f2 * other.f2; return new Fraction(newF1, newF2); } // 分数相减 public Fraction subtract(Fraction other) { int newF1 = this.f1 * other.f2 - other.f1 * this.f2; int newF2 = this.f2 * other.f2; return new Fraction(newF1, newF2); } // 分数相乘 public Fraction multiply(Fraction other) { int newF1 = this.f1 * other.f1; int newF2 = this.f2 * other.f2; return new Fraction(newF1, newF2); } // 分数相除 public Fraction divide(Fraction other) { int newF1 = this.f1 * other.f2; int newF2 = this.f2 * other.f1; return new Fraction(newF1, newF2); } // 以a/b的形式打印分数 public String toString() { return f1 + "/" + f2; } // 以浮点数的形式打印分数 public double toDouble() { return (double) f1 / f2; } // 测试Fraction类 public static void main(String[] args) { Fraction f1 = new Fraction(1, 2); Fraction f2 = new Fraction(3, 4); System.out.println(f1.add(f2)); // 5/4 System.out.println(f1.subtract(f2)); // -1/4 System.out.println(f1.multiply(f2)); // 3/8 System.out.println(f1.divide(f2)); // 2/3 System.out.println(f1.toString()); // 1/2 System.out.println(f1.toDouble()); // 0.5 } } ``` 在主方法中,我们创建了两个分数对象f1和f2,并对它们进行了加、减、乘、除等运算,并使用toString()方法以a/b的形式打印分数,使用toDouble()方法以浮点数的形式打印分数

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wonder-King

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值