java面向对象程序设计题

2024-12-05 01:46:28
推荐回答(4个)
回答1:

外面下暴雨了,有空帮你写个:
//可以算成绩的类,暂且当作班级类
import java.util.Arrays;
import java.util.Comparator;
public class Grade {
private Student[] stus;
public Grade(Student[] stus) {
super();
this.stus = stus;
// 对学生数组按分数排序
Arrays.sort(stus, new Comparator() {
@Override
public int compare(Student s1, Student s2) {
if (s1.getScore() >= s2.getScore()) {
return 1;
} else {
return -1;
}
}
});
}
public Student[] getStus() {
return stus;
}
public void setStus(Student[] stus) {
this.stus = stus;
}
// 平均成绩
public double avgScore() {
double score = 0.0;
for (Student stu : stus) {
score += stu.getScore();
}
return score / stus.length;
}
// 最高分的学生
public Student maxScore() {
return stus[stus.length - 1];
}
// 最低分学生
public Student minScore() {
return stus[0];
}
}

/*学生信息类*/
public class Student {
private int id;
private String name;
private double score;
public Student(int id, String name, double score) {
super();
this.id = id;
this.name = name;
this.score = score;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}

/*测试类*/
public class Test {
public static void main(String[] args) {
//测试类,先创建学生数组,里面有5个学生
Student[]stus = new Student[]{
new Student(1, "zhangsan", 60.0),
new Student(2, "lisi", 70.0),
new Student(3, "wangwu", 80.0),
new Student(4, "mazi", 90.0),
new Student(5, "baichi", 100.0)
};
//创建班级类(就是你要求的那个类)
Grade g = new Grade(stus);
System.out.println("平均分是:"+g.avgScore());
System.out.println("最高分是:"+g.maxScore().getName()+"("+g.maxScore().getScore()+"分)");
System.out.println("最低分是:"+g.minScore().getName()+"("+g.minScore().getScore()+"分)");
}
}

希望对你有帮助
顺便拜托加点分吧~

回答2:

import java.util.Scanner;
class Student {
private String stuNo;
private String stuName;
private double grade;
private double temp;
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
public double maxGrade(double values[]){
double max=values[0];
for (int i = 1; i < values.length; i++) {
if(values[i]>max){
temp=max;
max = values[i];
}else{
return max;
}
}
return max;
}
public double minGrade(double values[]){
double min=values[0];
for (int i = 1; i < values.length; i++) {
if(values[i]>min){
return min;
}else{
temp=min;
min = values[i];
}
}
return min;
}
public double averageGrade(double values[]){
double temp =0;
for (int i = 0; i < values.length; i++) {
temp +=values[i];
}
return temp/values.length;
}
}
public class Test {
public static void main(String[] args) {
Student stu = new Student();
Scanner input =new Scanner(System.in);
double []stuGrade=new double[5];
for (int i = 0; i < stuGrade.length; i++) {
System.out.println("请输入第"+(i+1)+"位同学的成绩");
stuGrade[i]=input.nextDouble();
}
System.out.println("最高成绩为:"+stu.maxGrade(stuGrade));
System.out.println("最低成绩为:"+stu.minGrade(stuGrade));
System.out.println("平均成绩为:"+stu.averageGrade(stuGrade));
}
}
希望你有用!!

回答3:

这种个你自己也先写吧,

回答4:

额...您真懒..这个都不自己写...