《Java程序设计》课程作业二
实验报告
1.定义一个表示学生信息Student类
(1)关键代码分析(见注释):
package myText;
import java.io.*;
import java.util.*;
public class StudentInformation {
public static void main(String[] args) {
Student s=new Student(201221,"杨婧丽","女",20);
System.out.String());
}
}java的tostring方法
class Student
{
//定义 Student类的成员变量,包括学号,姓名,性别,年龄
private long studentNumber;
private String studentName;
private String studentSex;
private int studentAge;
// Student类的构造方法,在构造方法中通过形参完成对成员变量的赋值操作
public Student(long studentNumber,String studentName,String studentSex,int studentAge)
{
this.studentNumber=studentNumber;
this.studentName=studentName;
this.studentSex=studentSex;
this.studentAge=studentAge;
}
// 使用Getter和Setter方法以及toString()方法,输出学生的关键信息
public long getStudentNumber() {
return studentNumber;
}
public void setStudentNumber(long studentNumber) {
this.studentNumber = studentNumber;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentSex() {
return studentSex;
}
public void setStudentSex(String studentSex) {
this.studentSex = studentSex;
}
public int getStudentAge() {
return studentAge;
}
public void setStudentAge(int studentAge) {
this.studentAge = studentAge;
}
public String toString() {
return "Student类中该学生的学号:" + getStudentNumber()+ ", 姓名:" + getStudentName() + ", 性别:"+ getStudentSex() + ", 年龄:" + getStudentAge() ;
}
}
(2)设计思路:
这部分代码比较基础,即先定义一个Student类,类中定义了成员变量,再编写构造方法和成员方法。写构造方法时,将形式参数studentNumber的值传递给成员变量studentNumber, 变量前面加上一个this关键字,其指的就是这个对象的成员变量或者方法,而不是指成员方法的形式参数或者局部变量。为此在上面这个代码中,this.studentNumber代表的就是对象中的成员变量,又叫做对象的属性。而后面的studentNumber则是方法的形式参数。代码this.studentNumber=studentNumber;就是将形式参数的值传递给成员变量。写成员方法时,Getter是对类对象的属性值的获得,Setter是
对类对象的属性的赋值方法。Java中的toString方法在Object类中已经存在。由于Java中所有的类都继承自Object类,所以此方法存在于所有对象中,其作用是为了方便所有类的字符串操作。查询Java的API文档后可以看到“建议所有子类都重写此方法”。每次返回类实例,则用return语句来完成。由以上方法便可输出学生的关键信息。
2. 定义GraduateStudent继承Student类
(1)关键代码分析(见注释):
class GraduateStudent extends Student //定义GraduateStudent类,并继承Student类
{
//增加属性:专业、导师、score(某门课程成绩)
private String major;
private String teacherName;
private int score;
public GraduateStudent(long studentNumber,String studentName,String studentSex,int studentAge,String major,String teacherName,int score)
{
super(studentNumber,studentName,studentSex,studentAge); //调用父类的构造方法,一 定要放在方法的首个语句
this.major=major; //在构造方法中通过形参完成对成员变量的赋值操作
acherName=teacherName;
this.score=score;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getTeacherName() {
return teacherName;
}
public void setTeacherName(String teacherName) {
acherName = teacherName;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
//重写toString()方法,输出研究生相关信息
public String toString()
{
return "学号:"+getStudentNumber()+" 姓名:"+getStudentName()+" 性别:"+getStudentSex()+" 年龄:"+getStudentAge()+" 专业:"+getMajor()+" 导师:"+getTeacherName()+" 成绩:"+getScore();
}
}
(2)设计思路:
这部分代码主要实现了类的继承,关键代码class GraduateStudent extends Student,在GraduateStudent类里写构造方法时,子类可以调用由父类声明的构造方法,但是必须在子类的构造方法中使用super关键字来调用,因为父类的构造方法中包括参数,则参数列表为必选项,用于指定父类构造方法的入口参数。成员方法toString重写,即增加了新的成员变量,最后输出了研究生的关键信息。
3. 创建多个GraduateStudent对象,存储于集合中,按学号从小到大的顺序向文件输出所有学生信息(每个学生信息占一行),并向文件输出这些学生课程成绩的平均值,以及成绩的最大值和最小值。
(1)关键代码分析(见注释):
public class StudentInformation {
public static void main(String[] args) {
GraduateStudent s1=new GraduateStudent(2012101,"陈琦","男",21,"通信工程","傅松",80);
GraduateStudent s2=new GraduateStudent(2012105,"崔雪娇","女",22,"通信工程","郑瑾",90);
GraduateStudent s3=new GraduateStudent(2012106,"董朝阳","男",22,"通信工程","罗雨峰",82);
GraduateStudent s4=new GraduateStudent(2012110,"方卉","女",21,"通信工程","叶清",85);
GraduateStudent s5=new GraduateStudent(2012103,"黄立明","男",22,"通信工程","孟凡超",90);
GraduateStudent s6=new GraduateStudent(2012116,"李善超","男",20,"通信工程","王建",82);
GraduateStudent s7=new GraduateStudent(2012109,"张磊","男",22,"通信工程","宗恒",97);
GraduateStudent s8=new GraduateStudent(2012111,"彭璐","女",22,"通信工程","周华平",92);
GraduateStudent s9=new GraduateStudent(2012108,"韩苗苗","女",21,"通信工程","王玉丽",81);
GraduateStudent s10=new GraduateStudent(2012112,"时小鑫","男",22,"通信工程","胡晶",90);
List<Student> students = new ArrayList<Student>();//用一个数组来存放所要排序的对象
students.add(s1);// //把各个对象加到数组中去
students.add(s2);
students.add(s3);
students.add(s4);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论