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
 */

import java.util.Scanner;


public class TestEight {


    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{
    private int a;
    private int b;
    Fraction(int a,int b){
        this.a = a;
        this.b = b;
    }
    double toDouble(){
        return (double) a/b;
    }
    Fraction plus(Fraction fraction){
        Fraction rfraction = new Fraction(fraction.a * this.b + fraction.b * this.a,fraction.b * this.b);
        return  rfraction;
    }
    Fraction multiply(Fraction fraction){
        Fraction rfraction = new Fraction(fraction.a * this.a,fraction.b * this.b);
        return  rfraction;
    }
    void print(){
        if(a==b){
            System.out.println(1);
        }else{
            int temp = gcd(this.a,this.b);
            System.out.println(a/temp+"/"+b/temp);
        }
    }
    //辗转相除法求最大公约数
    int gcd(int a,int b){
            if(a%b==0)
                return b;
            else
                return gcd(b,a%b);
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值