(1)创建和显示学生信息,具体要求如下。
①定义Person类,在该类中定义成员变量name(姓名)、sex(性别)和age(年龄),然后定义有参构造方法,初始化成员变量,最后定义成员方法 toString()返回姓名、性别和年龄信息。
② 定义Student类继承Person类,在该类中增加成员变量no(学号)、scoreEn(英语成绩)、 scoreMath(数学成绩)和scoreCh(语文成绩),然后增加成员方法aver()、max()和min()分别返回三门功课的平均分、最高分和最低分,最后重写toString0)方法返回姓名、性别、年龄、学号、平均分、最高分和最低分信息。
③ 定义 StudentTest 类,在main()方法中实现创建和显示学生信息。
package com.d20_ToProgram;
public class Person {
private String name;
private char sex;
private int age;
public Person(String name, char sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", sex=" + sex +
", age=" + age +
'}';
}
}
package com.d20_ToProgram;
public class Student extends Person{
private String no;
private double scoreEn;
private double scoreMath;
private double scoreCh;
public Student(String name, char sex, int age, String no, double scoreEn, double scoreMath, double scoreCh) {
super(name, sex, age);
this.no = no;
this.scoreEn = scoreEn;
this.scoreMath = scoreMath;
this.scoreCh = scoreCh;
}
public static double aver(double scoreEn,double scoreMath,double scoreCh){
return (scoreEn + scoreMath + scoreCh) / 3;
}
public static double max(double scoreEn,double scoreMath,double scoreCh){
if(scoreEn > scoreMath || scoreEn > scoreCh){
return scoreEn;
}else if(scoreMath > scoreEn || scoreMath > scoreCh){
return scoreMath;
}else {
return scoreCh;
}
}
public static double min(double scoreEn,double scoreMath,double scoreCh){
if(scoreEn < scoreMath || scoreEn < scoreCh){
return scoreEn;
}else if(scoreMath < scoreEn || scoreMath < scoreCh){
return scoreMath;
}else {
return scoreCh;
}
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public double getScoreEn() {
return scoreEn;
}
public void setScoreEn(double scoreEn) {
this.scoreEn = scoreEn;
}
public double getScoreMath() {
return scoreMath;
}
public void setScoreMath(double scoreMath) {
this.scoreMath = scoreMath;
}
public double getScoreCh() {
return scoreCh;
}
public void setScoreCh(double scoreCh) {
this.scoreCh = scoreCh;
}
@Override
public String toString() {
return super.toString() + "Student{" + "no='" + no + '\'' + ", scoreEn=" + scoreEn + ", scoreMath=" + scoreMath + ", scoreCh=" + scoreCh + "最高分:"+",平均分:" + aver(scoreEn,scoreMath,scoreCh);
}
}
package com.d20_ToProgram;
public class Test {
public static void main(String[] args) {
Person p = new Person("张三",'男',19);
System.out.println(p.toString());
Student s = new Student("张三",'男',19,"20210820",98.5,98.6,98.7);
System.out.println(s.toString());
}
}
帮忙看一下这个编程这样解的思路对吗,还有好像只能输出到平均分后面的最高分还有最低分具体要怎么弄!