java练习题 请给出详细代码 谢啦
java练习题请给出详细代码谢啦定义一个学生类成员属性:学号,姓名,四门课成绩成员方法:三个构造函数,第一个没有参数第二个只初始化姓名,学号,第三个初始化所有数据。定义一...
java练习题
请给出详细代码
谢啦定义一个学生类
成员属性:学号,姓名,四门课成绩
成员方法:三个构造函数,第一个没有参数
第二个只初始化姓名,学号,第三个初始化所有数据。
定义一个主类
并初始化3位学生,并对总成绩排序,输出 展开
请给出详细代码
谢啦定义一个学生类
成员属性:学号,姓名,四门课成绩
成员方法:三个构造函数,第一个没有参数
第二个只初始化姓名,学号,第三个初始化所有数据。
定义一个主类
并初始化3位学生,并对总成绩排序,输出 展开
1个回答
展开全部
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;
}
}
}
追问
非常非常感谢啦😃
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询