上机题
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