java练习题 请给出详细代码 谢啦
发布网友
发布时间:2022-05-06 12:40
我来回答
共1个回答
热心网友
时间:2022-06-30 14:01
package myeclipse;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class StudentInfoInput {
private String stuNo;
private String stuName;
private double one;
private double two;
private double three;
private double four;
public StudentInfoInput() {
super();
// TODO Auto-generated constructor stub
}
public StudentInfoInput(String stuNo, String stuName) {
super();
this.stuNo = stuNo;
this.stuName = stuName;
}
public StudentInfoInput(String stuNo, String stuName, double one,
double two, double three, double four) {
super();
this.stuNo = stuNo;
this.stuName = stuName;
this.one = one;
this.two = two;
this.three = three;
this.four = four;
}
public StudentInfoInput(String stuName) {
super();
this.stuName = stuName;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public double getOne() {
return one;
}
public void setOne(double one) {
this.one = one;
}
public double getTwo() {
return two;
}
public void setTwo(double two) {
this.two = two;
}
public double getThree() {
return three;
}
public void setThree(double three) {
this.three = three;
}
public double getFour() {
return four;
}
public void setFour(double four) {
this.four = four;
}
public String toString() {
return "stuNo:"+stuNo+",stuName:"+stuName+",one:"+one+",two:"+two+",three:"+three+",four:"+four;
}
}
class Sort {
public static void main(String[] args) {
StudentInfoInput si1 = new StudentInfoInput("001","小明",23.2,123,65.3,98);
StudentInfoInput si2 = new StudentInfoInput("002","小红",54,54.5,78.7,43);
StudentInfoInput si3 = new StudentInfoInput("003","小小",56,34.5,67,90);
sort(si1,si2,si3);
}
public static void sort(StudentInfoInput si1,StudentInfoInput si2,StudentInfoInput si3) {
Map<StudentInfoInput,Double> map = new HashMap<StudentInfoInput, Double>();
ValueComparator vc = new ValueComparator(map);
TreeMap<StudentInfoInput, Double> tm = new TreeMap<StudentInfoInput, Double>(vc);
map.put(si1, sum(si1));
map.put(si2, sum(si2));
map.put(si3, sum(si3));
tm.putAll(map);
System.out.println(tm);
}
public static double sum(StudentInfoInput si) {
return si.getOne()+si.getTwo()+si.getThree()+si.getFour();
}
}
class ValueComparator implements Comparator<StudentInfoInput> {
Map<StudentInfoInput, Double> base;
public ValueComparator(Map<StudentInfoInput, Double> base) {
this.base = base;
}
@Override
public int compare(StudentInfoInput o1, StudentInfoInput o2) {
if (base.get(o1) >= base.get(o2)) {
return -1;
} else {
return 1;
}
}
}
追问非常非常感谢啦😃