JAVA父类及子类的声明编程题(编写的代码行后面要配有必要的注释)
1、完成下面父类及子类的声明:(1)声明Student类。属性包括学号、姓名、英语成绩、计算机成绩和总成绩。方法包括构造方法、get方法、set方法、toString方法...
1、完成下面父类及子类的声明:
(1)声明Student类。
属性包括学号、姓名、英语成绩、计算机成绩和总成绩。
方法包括构造方法、get方法、set方法、toString方法、equals方法、compare方法(比较两个学生的总成绩,结果分大于、小于、等于)、sum方法(计算总成绩)和testScore方法(计算评测成绩)。
注:评测成绩可以取三门课成绩的平均分,另外任何一门课的成绩的改变都需要对总成绩进行重新计算,因此,在每一个set方法中应调用sum方法计算总成绩。
(2)声明StudentXW(学习委员)类为Student类的子类。
在StudentXW类中增加责任属性,并重写testScore方法(计算评测成绩,评测成绩=三门课的平均分+3)。
(3)声明StudentBZ(班长)类为Student类的子类。
在StudentBZ类中增加责任属性,并重写testScore方法(计算评测成绩,评测成绩=三门课的平均分+5)。
(4)声明测试类,生成若干个Student类、StudentXW类及StudentBZ类对象,并分别计算它们的评测成绩。(测试类完成对多态性的测试)再声明Student类的数组(含五个元素),生成五个对象存入数组:其中三个Student类对象、一个StudentXW类的对象、一个StudentBZ类的对象,将方法testScore()发送给数组的每一个元素,输出结果,并分析具体执行的是哪一个类中的方法。 展开
(1)声明Student类。
属性包括学号、姓名、英语成绩、计算机成绩和总成绩。
方法包括构造方法、get方法、set方法、toString方法、equals方法、compare方法(比较两个学生的总成绩,结果分大于、小于、等于)、sum方法(计算总成绩)和testScore方法(计算评测成绩)。
注:评测成绩可以取三门课成绩的平均分,另外任何一门课的成绩的改变都需要对总成绩进行重新计算,因此,在每一个set方法中应调用sum方法计算总成绩。
(2)声明StudentXW(学习委员)类为Student类的子类。
在StudentXW类中增加责任属性,并重写testScore方法(计算评测成绩,评测成绩=三门课的平均分+3)。
(3)声明StudentBZ(班长)类为Student类的子类。
在StudentBZ类中增加责任属性,并重写testScore方法(计算评测成绩,评测成绩=三门课的平均分+5)。
(4)声明测试类,生成若干个Student类、StudentXW类及StudentBZ类对象,并分别计算它们的评测成绩。(测试类完成对多态性的测试)再声明Student类的数组(含五个元素),生成五个对象存入数组:其中三个Student类对象、一个StudentXW类的对象、一个StudentBZ类的对象,将方法testScore()发送给数组的每一个元素,输出结果,并分析具体执行的是哪一个类中的方法。 展开
4个回答
展开全部
程序如下供你参考.说明:
1.0 你题目中只提供了两门成绩,英语和计算机,但是下面说三门.
如果是的话,自己添加上去吧.很好修改的.
2.0 如果有什么不清楚的话,补充问题吧.或者可以给你留言.
3.0 希望你自己多看书把语言的基础知识学好.
下面共四个java类文件.
/***Student.java **/
public class Student {
protected int studentID;
protected String name;
protected int englishScore;
protected int computerScore;
protected int totalScore;
public Student(int studentID, String name) {
super();
this.studentID = studentID;
this.name = name;
}
public Student(int studentID, String name, int englishScore,
int computerScore) {
super();
this.studentID = studentID;
this.name = name;
this.englishScore = englishScore;
this.computerScore = computerScore;
}
public int getStudentID() {
return studentID;
}
public void setStudentID(Integer studentID) {
this.studentID = studentID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getEnglishScore() {
return englishScore;
}
public void setEnglishScore(int englishScore) {
this.englishScore = englishScore;
}
public int getComputerScore() {
return computerScore;
}
public void setComputerScore(int computerScore) {
this.computerScore = computerScore;
}
public int getTotalScore() {
return totalScore;
}
/**totalScore 没有set 方法 */
@Override
public String toString() {
return "Student [studentID=" + studentID + ", name=" + name
+ ", englishScore=" + englishScore + ", computerScore="
+ computerScore + ", totalScore=" + totalScore + "]";
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (studentID != other.studentID)
return false;
return true;
}
/** @return 返回1 表示 大于,返回 0 表示等于, 返回-1表示小于 */
public int compare(Student other){
if(totalScore < other.totalScore)
return -1; //
if(totalScore == other.totalScore)
return 0;
return 1;
}
private void sum(){
totalScore = englishScore+computerScore;
}
/**计算评测成绩 */
public double testScore(){
return (englishScore+computerScore)/2;
}
}
/***** StudentXW.java******/
public class StudentXW extends Student {
private String response;///责任
public StudentXW(int studentID, String name, String response) {
super(studentID, name);
this.response = response;
}
public StudentXW(int studentID, String name, int englishScore,
int computerScore, String response) {
super(studentID, name, englishScore, computerScore);
this.response = response;
}
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
@Override
public double testScore(){
return (englishScore+computerScore)/2+3;
}
}
/*****StudentBZ.java****/
public class StudentBZ extends Student {
private String response;///责任
public StudentBZ(int studentID, String name, String response) {
super(studentID, name);
this.response = response;
}
public StudentBZ(int studentID, String name, int englishScore,
int computerScore, String response) {
super(studentID, name, englishScore, computerScore);
this.response = response;
}
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
@Override
public double testScore(){
return (englishScore+computerScore)/2+5;
}
}
/*****测试类 TestStudent.java****/
/** Student ,StudentXW, 及StudentBZ测试类 */
public class TestStudent {
public static void main(String[] args) {
Student s1 = new Student(1,"one");
s1.setEnglishScore(80);
s1.setComputerScore(80);
StudentXW sx1 = new StudentXW(2,"xone","学习委员");
sx1.setEnglishScore(90);
sx1.setComputerScore(90);
StudentBZ sb1 = new StudentBZ(3,"bone","班长");
sb1.setEnglishScore(70);
sb1.setComputerScore(70);
System.out.println("学生 One 的评测成绩为:"+s1.testScore());
System.out.println("学生 xone,学习委员的评测成绩为:"+sx1.testScore());
System.out.println("学生 bone,班长的评测成绩为:"+sb1.testScore());
Student [] studentArray = new Student[5];
studentArray[0] = new Student(101,"studentOne",60,60);
studentArray[1] = new Student(102,"studentTwo",65,65);
studentArray[2] = new Student(103,"studentThree",70,70);
studentArray[3] = new StudentXW(104,"studentXW",75,75,"学习委员");
studentArray[4] = new StudentBZ(104,"studentXW",80,80,"学习委员");
for(Student student:studentArray){
System.out.println("名字为:"+student.getName()+"的成绩为:"+ student.testScore());
}
}
}
1.0 你题目中只提供了两门成绩,英语和计算机,但是下面说三门.
如果是的话,自己添加上去吧.很好修改的.
2.0 如果有什么不清楚的话,补充问题吧.或者可以给你留言.
3.0 希望你自己多看书把语言的基础知识学好.
下面共四个java类文件.
/***Student.java **/
public class Student {
protected int studentID;
protected String name;
protected int englishScore;
protected int computerScore;
protected int totalScore;
public Student(int studentID, String name) {
super();
this.studentID = studentID;
this.name = name;
}
public Student(int studentID, String name, int englishScore,
int computerScore) {
super();
this.studentID = studentID;
this.name = name;
this.englishScore = englishScore;
this.computerScore = computerScore;
}
public int getStudentID() {
return studentID;
}
public void setStudentID(Integer studentID) {
this.studentID = studentID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getEnglishScore() {
return englishScore;
}
public void setEnglishScore(int englishScore) {
this.englishScore = englishScore;
}
public int getComputerScore() {
return computerScore;
}
public void setComputerScore(int computerScore) {
this.computerScore = computerScore;
}
public int getTotalScore() {
return totalScore;
}
/**totalScore 没有set 方法 */
@Override
public String toString() {
return "Student [studentID=" + studentID + ", name=" + name
+ ", englishScore=" + englishScore + ", computerScore="
+ computerScore + ", totalScore=" + totalScore + "]";
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (studentID != other.studentID)
return false;
return true;
}
/** @return 返回1 表示 大于,返回 0 表示等于, 返回-1表示小于 */
public int compare(Student other){
if(totalScore < other.totalScore)
return -1; //
if(totalScore == other.totalScore)
return 0;
return 1;
}
private void sum(){
totalScore = englishScore+computerScore;
}
/**计算评测成绩 */
public double testScore(){
return (englishScore+computerScore)/2;
}
}
/***** StudentXW.java******/
public class StudentXW extends Student {
private String response;///责任
public StudentXW(int studentID, String name, String response) {
super(studentID, name);
this.response = response;
}
public StudentXW(int studentID, String name, int englishScore,
int computerScore, String response) {
super(studentID, name, englishScore, computerScore);
this.response = response;
}
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
@Override
public double testScore(){
return (englishScore+computerScore)/2+3;
}
}
/*****StudentBZ.java****/
public class StudentBZ extends Student {
private String response;///责任
public StudentBZ(int studentID, String name, String response) {
super(studentID, name);
this.response = response;
}
public StudentBZ(int studentID, String name, int englishScore,
int computerScore, String response) {
super(studentID, name, englishScore, computerScore);
this.response = response;
}
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
@Override
public double testScore(){
return (englishScore+computerScore)/2+5;
}
}
/*****测试类 TestStudent.java****/
/** Student ,StudentXW, 及StudentBZ测试类 */
public class TestStudent {
public static void main(String[] args) {
Student s1 = new Student(1,"one");
s1.setEnglishScore(80);
s1.setComputerScore(80);
StudentXW sx1 = new StudentXW(2,"xone","学习委员");
sx1.setEnglishScore(90);
sx1.setComputerScore(90);
StudentBZ sb1 = new StudentBZ(3,"bone","班长");
sb1.setEnglishScore(70);
sb1.setComputerScore(70);
System.out.println("学生 One 的评测成绩为:"+s1.testScore());
System.out.println("学生 xone,学习委员的评测成绩为:"+sx1.testScore());
System.out.println("学生 bone,班长的评测成绩为:"+sb1.testScore());
Student [] studentArray = new Student[5];
studentArray[0] = new Student(101,"studentOne",60,60);
studentArray[1] = new Student(102,"studentTwo",65,65);
studentArray[2] = new Student(103,"studentThree",70,70);
studentArray[3] = new StudentXW(104,"studentXW",75,75,"学习委员");
studentArray[4] = new StudentBZ(104,"studentXW",80,80,"学习委员");
for(Student student:studentArray){
System.out.println("名字为:"+student.getName()+"的成绩为:"+ student.testScore());
}
}
}
展开全部
坐等勤快的人
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在加点分就给你
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
快赶上小项目了,哥们这些还要自己弄啊。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询