从零开始的java生活(刷题篇10)

上机题

        1.带分数【集合遍历,排序,查找等】

【问题描述】利用已有分数类(Fraction)设计带分数类(MixedFration)(表示形式为a[b/c],其中a为整数部分,b为分子,c为分母),添加或者重写必要的属性和方法(主要有参数为字符串的构造方法,hashCode方法,equals方法和compare方法),设计Test类中的main方法,以输出正确结果。其中Fraction类已经定义,须提交包含MixedFration类和Test类的Test.java文件,主类名填写为Test。

【输入形式】以字符串形式读入6个带分数,如:3[1/2] 2[1/3] 5[2/5] -1[1/3] -2[1/6] 2[1/3]

【输出形式】带分数的运算结果仍为带分数,表示为“3[1/2]”,对应分数为“7/2”,若整数部分为0,则表示为“1/2”,若“-3[1/2]”则对应分数为“-7/2”。

以ArrayList存储后遍历后结果

对ArrayList反序后遍历后结果
重复带分数数目(以HashSet存储,计算两种存储方式对长度差值)

以TreeSet存储遍历结果

上述集合中不大于4[1/3]的最大带分数

具体参照样例输出
【样例输入】3[1/2] 2[1/3] 5[2/5] -1[1/3] -2[1/6] 2[1/3]

【样例输出】

3[1/2] 2[1/3] 5[2/5] -1[1/3] -2[1/6] 2[1/3]

2[1/3] -2[1/6] -1[1/3] 5[2/5] 2[1/3] 3[1/2] 

2

-2[1/6] -1[1/3] 2[1/3] 3[1/2] 5[2/5] 

3[1/2]

【样例说明】无
【评分标准】结果正确,满分。

提供的Fraction类如下:

public class Fraction {
	private int fenZi;
	private int fenMu;
	public Fraction() {
	}
	public Fraction(int fenZi, int fenMu) {
		super();
		this.fenZi = fenZi;
		this.fenMu = fenMu;
	}
	public int getFenZi() {
		return fenZi;
	}
	public void setFenZi(int fz) {
		this.fenZi = fz;
	}
	public int getFenMu() {
		return fenMu;
	}
	public void setFenMu(int fm) {
		this.fenMu = fm;
	}
	public void huaJian() {
		if (fenZi == 0) {
			fenMu = 1;
		}
		int yueshu = getYueshu();
			if (fenMu > 0) {
				fenZi /= yueshu;
				fenMu /= yueshu;
			} else {
				fenZi /= -yueshu;
				fenMu /= -yueshu;
			}
	}
	private int getYueshu() {
		int a = Math.abs(fenZi);
		int b = Math.abs(fenMu);
		int smaller = Math.min(a, b);
		for (int i = smaller; i > 1; i--) {
			if (a % i == 0 && b % i == 0) {
				return i;
			}
		}
		return 1;
	}
	public String toString() {
		if (fenMu == 1) {
			return fenZi + "";
		}
		return fenZi + "/" + fenMu;
	}	
	public void jia(Fraction a) {
		this.fenZi = this.fenZi * a.getFenMu() + a.getFenZi() * this.fenMu;
		this.fenMu = this.fenMu * a.getFenMu();
	}
       public void jian(Fraction a) {
		this.fenZi = this.fenZi * a.getFenMu() - a.getFenZi() * this.fenMu;
		this.fenMu = this.fenMu * a.getFenMu();
	}
}

编写的Test类和MixedFraction类如下:

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<MixedFraction> list = new ArrayList<>();
        Collections.addAll(list, new MixedFraction(sc.next()), new MixedFraction(sc.next()), new MixedFraction(sc.next()), new MixedFraction(sc.next()), new MixedFraction(sc.next()), new MixedFraction(sc.next()));

        for (MixedFraction num : list) {
            System.out.print(num + " ");
        }
        System.out.println();
        Collections.reverse(list);

        for (MixedFraction num : list) {
            System.out.print(num + " ");
        }
        System.out.println();

        HashSet<MixedFraction> hashSet = new HashSet<>(list);
        System.out.println(list.size() - hashSet.size() + 1);

        TreeSet<MixedFraction> treeSet = new TreeSet<>(list);

        for (MixedFraction num : treeSet) {
            System.out.print(num + " ");
        }
        System.out.println();
        MixedFraction maxMixedFraction = new MixedFraction("0");
        MixedFraction target = new MixedFraction("4[1/3]");
        for (MixedFraction num : treeSet) {
            if (num.compareTo(target) <= 0) {
                if (num.compareTo(maxMixedFraction) >= 0) maxMixedFraction = num;
            }
        }
        System.out.print(maxMixedFraction + " ");


    }
}

class MixedFraction extends Fraction implements Comparable<MixedFraction> {
    private int zengShu;

    public int getZengShu() {
        return zengShu;
    }

    public void setZengShu(int zengShu) {
        this.zengShu = zengShu;
    }

    public String toString() {

        return getZengShu() + "[" + super.toString() + "]";
    }

    public MixedFraction(String text) {
        Pattern pattern = Pattern.compile("\\d+|-\\d+");
        Matcher matcher = pattern.matcher(text);
        while (matcher.find()) {

            if (this.zengShu == 0) {
                setZengShu(Integer.parseInt(matcher.group()));
            } else if (this.getFenZi() == 0) {
                setFenZi(Integer.parseInt(matcher.group()));
            } else if (this.getFenMu() == 0) {
                setFenMu(Integer.parseInt(matcher.group()));
            }
        }
    }

    public int compareTo(MixedFraction o) {
        return (int) ((double) ((this.getZengShu() * this.getFenMu()) + this.getFenZi()) / this.getFenMu() - (double) ((o.getZengShu() * o.getFenMu()) + o.getFenZi()) / o.getFenMu());
    }


    public int hashCode() {
        int result = Integer.toString(getZengShu()).hashCode();
        result = 17 * result + Integer.toString(getFenZi()).hashCode();
        result = 17 * result + Integer.toString(getFenMu()).hashCode();
        return result;
    }

    public boolean equals(Object obj) {
        if (!(obj instanceof MixedFraction)) {
            return false;
        }
        MixedFraction MixedObj = (MixedFraction) obj;
        if (this == MixedObj) {
            return true;
        }

        return MixedObj.zengShu == this.zengShu && MixedObj.getFenZi() == this.getFenZi() && MixedObj.getFenMu() == this.getFenMu();
    }

}


博主java萌新,有问题可评论区共同交流学习,欢迎大家交流awa

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值