创建一个Fraction类执行分数运算。要求如下:
(1)用整数表示类的private成员变量:f1和f2。
(2)提供构造方法,将分子存入f1,分母存入f2。
(3)提供两个分数相加的运算方法,结果分别存入f1和f2。
(4)提供两个分数相减的运算方法,结果分别存入f1和f2。
(5)提供两个分数相乘的运算方法,结果分别存入f1和f2。
(6)提供两个分数相除的运算方法,结果分别存入f1和f2。
(7)以a/b的形式打印Fraction数。
(8)以浮点数形式打印Fraction数。
(9)编写主控程序运行分数运算。

解决方案 »

  1.   

    public class MyTest {
    public static void main(String[] args) {
    Fraction fraction = new Fraction(1,3);
    Fraction fraction2 = new Fraction(3,5);
    fraction.print();
    fraction2.printAsFloat();
    fraction.add(fraction2).print();
    fraction.sub(fraction2).print();
    fraction.mul(fraction2).print();
    fraction.div(fraction2).print();
    }}class Fraction{
    private int f1;
    private int f2;
    public int getF1() {
    return f1;
    }

    public void setF1(int f1) {
    this.f1 = f1;
    }
    public int getF2() {
    return f2;
    }
    public void setF2(int f2) {
    this.f2 = f2;
    }

    public Fraction(int f1, int f2) {
    super();
    this.f1 = f1;
    this.f2 = f2;
    }

    public void print() {
    System.out.println(f1 + "/" + f2);
    }

    public Fraction add(Fraction f) {
    return new Fraction(f1*f.f2+f.f1*f2, f2*f.f2);
    }
    public Fraction sub(Fraction f) {
    return new Fraction(f1*f.f2-f.f1*f2, f2*f.f2);
    }
    public Fraction mul(Fraction f) {
    return new Fraction(f1*f.f1, f2*f.f2);
    }
    public Fraction div(Fraction f) {
    return new Fraction(f1*f.f2, f2*f.f1);
    }
    public void printAsFloat() {
    System.out.println((float)f1 / f2);
    }

    }
      

  2.   

    不能再整天泡在CSDN里了!那怎么又跑出来了?
      

  3.   

    package com.yuan.study.Util;/**
     * 创建一个Fraction类执行分数运算。要求如下: 
     * (1)用整数表示类的private成员变量:f1和f2。
     * (2)提供构造方法,将分子存入f1,分母存入f2。 
     * (3)提供两个分数相加的运算方法,结果分别存入f1和f2。
     * (4)提供两个分数相减的运算方法,结果分别存入f1和f2。 
     * (5)提供两个分数相乘的运算方法,结果分别存入f1和f2。
     * (6)提供两个分数相除的运算方法,结果分别存入f1和f2。 
     * (7)以a/b的形式打印Fraction数。 
     * (8)以浮点数形式打印Fraction数。
     * (9)编写主控程序运行分数运算。
     */
    public class Fraction {
    // 分子
    private int f1; // 分母
    private int f2; /**
     * constructor
     * 
     * @param _f1
     *            分子
     * @param _f2
     *            分母
     */
    public Fraction(int _f1, int _f2) throws Exception {
    if (_f2 == 0) {
    throw new Exception("分母不能为零!");
    }
    this.f1 = _f1;
    this.f2 = _f2;
    } /**
     * 显示当前分数的字符串值
     */
    public String toString() {
    return f1 + "/" + f2;
    }
    /**
     * 显示当前分数的浮点数值
     */
    public double getFloatValue() {
    return (f1*1.00D)/f2;
    } /**
     * 加法
     * 
     * @param _ft
     * @return
     */
    public Fraction plus(Fraction ft2) throws Exception {
    int ft1_f1 = this.f1;
    int ft1_f2 = this.f2;
    int ft2_f1 = ft2.f1;
    int ft2_f2 = ft2.f2;
    Fraction ft3 = new Fraction(1, 1);
    int ft3_f1 = ft1_f1 * ft2_f2 + ft2_f1 * ft1_f2;
    int ft3_f2 = ft1_f2 * ft2_f2;
    return reduction(new Fraction(ft3_f1, ft3_f2));
    } /**
     * 减法
     * 
     * @param _ft
     * @return
     */
    public Fraction minus(Fraction ft2) throws Exception {
    int ft1_f1 = this.f1;
    int ft1_f2 = this.f2;
    int ft2_f1 = ft2.f1;
    int ft2_f2 = ft2.f2;
    Fraction ft3 = new Fraction(1, 1);
    int ft3_f1 = ft1_f1 * ft2_f2 - ft2_f1 * ft1_f2;
    int ft3_f2 = ft1_f2 * ft2_f2;
    return reduction(new Fraction(ft3_f1, ft3_f2));
    } /**
     * 乘法
     * 
     * @param _ft
     * @return
     */
    public Fraction multiply(Fraction ft2) throws Exception {
    int ft1_f1 = this.f1;
    int ft1_f2 = this.f2;
    int ft2_f1 = ft2.f1;
    int ft2_f2 = ft2.f2;
    Fraction ft3 = new Fraction(1, 1);
    int ft3_f1 = ft1_f1 * ft2_f1;
    int ft3_f2 = ft1_f2 * ft2_f2;
    return reduction(new Fraction(ft3_f1, ft3_f2)); } /**
     * 除法
     * 
     * @param _ft
     * @return
     */
    public Fraction divide(Fraction ft2) throws Exception {
    int ft1_f1 = this.f1;
    int ft1_f2 = this.f2;
    int ft2_f1 = ft2.f1;
    int ft2_f2 = ft2.f2;
    Fraction ft3 = new Fraction(1, 1);
    int ft3_f1 = ft1_f1 * ft2_f2;
    int ft3_f2 = ft1_f2 * ft2_f1;
    return reduction(new Fraction(ft3_f1, ft3_f2)); } /**
     * 约分
     * 
     * @param _ft
     * @return
     */
    public static Fraction reduction(Fraction _ft) throws Exception {
    int temp_f1 = _ft.f1;
    int temp_f2 = _ft.f2;
    int type = 1;
    if (temp_f1 < 0) {
    temp_f1 = (-1) * temp_f1;
    type = (-1) * type;
    }
    if (temp_f2 < 0) {
    temp_f2 = (-1) * temp_f2;
    type = (-1) * type;
    }
    int minimum = temp_f1 < temp_f2 ? temp_f1 : temp_f2;
    int i = 2;
    while (i <= minimum) {
    // i 是 共因子
    if (temp_f1 % i == 0 && temp_f2 % i == 0) {
    temp_f1 = temp_f1 / i;
    temp_f2 = temp_f2 / i;
    // reset minimum and i
    minimum = temp_f1 < temp_f2 ? temp_f1 : temp_f2;
    i = 2;
    } else {
    i++;
    }
    }
    return new Fraction(type * temp_f1, temp_f2);
    } /**
     * Main function
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {
    Fraction ft1 = new Fraction(3, 6);
    Fraction ft2 = new Fraction(1, 3);
    System.out.println(ft1.plus(ft2));
    System.out.println(ft1.minus(ft2));
    System.out.println(ft1.multiply(ft2));
    System.out.println(ft1.divide(ft2));
    //Float value
    System.out.println(ft1.getFloatValue());
    System.out.println(ft2.getFloatValue());
    } catch (Exception e) {
    e.printStackTrace();
    } }}
      

  4.   

    public class YunSuan{
         private int f1;
         private int f2;     public YunSuan1(int f1, int f2){         this.f1 = f1;
             this.f2 = f2;     }     public void set(int f1 ,int f2){         this.f1 = f1;
             this.f2 = f2;
         
         }
     
         public int getF1(){
         
             return f1;     }     public int getF1(){
         
             return f2;     }
       
         public void jiaFaYunSuan(){         System.out.println(f1+f2);     }     public void jianFaYunSuan(){         System.out.println(f1-f2);     }     public void chengFaYunSuan(){         System.out.println(f1*f2);     }     public void chuFaYunSuan(){         System.out.println((double)f1/f2);     }
    }public class KongZhiYunSuan{    public static void main(String[] args){        YunSuan a = new YunSuan(20 , 5);        a.set(40,13);  
            
            a.jiaFaYunSuan();
            a.jianFaYunSuan();
            a.chengFaYunSuan();
            a.chuFaYunSuan();
      
            System.out.println(a.getF1());
            System.out.println(a.getF2());    }}