最近在学习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);
}
}