JAVA 面向对象练习题

这篇博客介绍了如何设计一个JAVA分数类Fraction,包括构造函数、转换为double的方法、加法和乘法操作,并要求实现分数的最简形式。博主通过解决一道面向对象编程题,展示了如何使用最大公约数简化分数并提供了自己的实现代码。
摘要由CSDN通过智能技术生成

        最近在学习JAVA,学校里学的东西早忘了,是照着慕课网上的课程学的,碰到一条面向对象的简单编程题。以下是题目:

设计一个表示分数的类Fraction。这个类用两个int类型的变量分别表示分子和分母,构造一个a/b的分数

这个类的构造函数是:

Fraction(int a, int 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 b = new Fraction(in.nextInt(),in.nextInt());
Fraction a = new Fraction(in.nextInt(), in.nextInt());a.print();b.print();
		b.print();
a.plus(b).print();a.multiply(b).plus(new Fraction(5,6)).print();a.print();in.close();}
}

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

class Fraction {

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


输入格式:

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


输出格式:

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


输入样例:

2 4 1 3


输出样例:

1/2

1/3

5/6

1

1/2

1/3


一开始想了很久没头绪,后来看了一位同学的代码,是用最大公约数来做的很受启发,以下是这位同学的代码:


class Fraction{
    int a;
    int b;
     Fraction(int a ,int b) {
        int gcd = GCD(a,b);
        this.a = a/gcd;
        this.b = b/gcd;
    }
     //分数转小数,在这题中有用到????
     double toDouble(int a ,int b) {
         double ret = a*1.0/b ;
         return ret;
     }
      
     //分数“x”,返回一个新的Fraction
    Fraction  multiply(Fraction r){
        int a1 = a*r.a ;
        int b1 = b*r.b ;
        int gcd = GCD(a1,b1);
        a1 /= gcd ;
        b1 /= gcd ;
     Fraction x = new Fraction(a1,b1);
     return x ;
     }
    //分数“+” ,返回一个新的Fraction 
    Fraction plus(Fraction r) {
        int b1 = LCM(b,r.b);
        int a1 = a*(b1/b) + r.a*(b1/r.b);
        int gcd = GCD(a1,b1);
        b1 /= gcd ;
        a1 /= gcd ;
        Fraction x = new Fraction(a1,b1);
        return x ;
         
    }
    //print Method !!
    void print() {
        if(a==0) {
            System.out.println(0);
        }
        else if(a==b) {
            System.out.println(1);
        }
        else System.out.println(a+"/"+b);
    }
    //Method最大公因数
    int GCD(int x,int y) {
        int ret = 1;
        while(x%y != 0) {
            int t= x%y;
            x = y ;
            y = t ;
        }
        ret = y ;
        return ret ;
    }
    //Method最小公倍数
    int LCM(int x,int y) {
        int gcd = GCD(x,y);
        return x*y/gcd;
    }
     
}

不过最大公倍数我觉的没必要,而且他的代码有些冗余,以下是我的代码:


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  {
	private int a;
	private int b;
	
	Fraction(int a,int b){
		int max=simplify(a,b);
		this.a = a/max;
		this.b = b/max;	
	}
	
	double toDouble(int a,int b){
		double r =(double)a/(double)b;
		return r;
	}
	
	Fraction plus(Fraction r){	
		int a1 = a*r.b+b*r.a;
		int b1 = b*r.b;
	    Fraction p =new Fraction(a1,b1);
	    return p ;
	}
	
	Fraction multiply(Fraction r){
		int a2 = a*r.a;
		int b2 = b*r.b;
		Fraction m =new Fraction(a2,b2);
		return m;
	}
	
//	求最大公约数
	int simplify(int a,int b){
		int max = 0 ;
		if(a%b == 0){
			max=b;
		}else{
			for (int i=1;i<=a;i++){
				if(a%i==0 && b%i==0){
					max = i;	
				}	
			}
		}
		return max;
	}

    void print() {
        if(a==0) {
            System.out.println(0);
        }
        else if(a==b) {
            System.out.println(1);
        }
        else System.out.println(a+"/"+b);
    }
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值