实验1 java基本结构编程 下载本文

内容发布更新时间 : 2024/12/23 9:57:33星期一 下面是文章的全部内容请认真阅读。

int year, month, day;

public Date(int year, int month, int day) { this.year = year; this.month = month; this.day = day; }

public Date() { }

public int getDay() { return day; }

public void setDay(int day) { this.day = day; }

public int getMonth() { return month; }

public void setMonth(int month) { this.month = month; }

public int getYear() { return year; }

public void setYear(int year) { this.year = year; }

@Override

public String toString() {

return year + \年\月\日\ } }

Stu类:package Student; import exp4_1.*; public class Stu {

String no; String name; Date birthday; Score score;

public Stu(String no, String name) { this.no = no;

this.name = name; birthday=new Date(); score=new Score(); }

public String getName() {

return name; }

public void setBirthday(int y ,int m,int d) { this.birthday.setYear(y); this.birthday.setMonth(m); this.birthday.setDay(d); }

public void setScore(int c,int m,int e) { this.score.chinese=c; this.score .math=m; this.score.english=e; this.score.setChinese(e); this.score.setMath(m); this.score.setEnglish(e); }

public void setName(String name) { this.name = name; }

public String getNo() { return no; }

public void setNo(String no) {

this.no = no; }

public void printstu(){

System.out.println(\学号:\ System.out.println(\姓名:\

System.out.println(\出生日期:\ System.out.println(\成绩:\ } }

Score类: package Student; public class Score {

int chinese,math,english;

this.chinese = chinese; this.math = math;

this.english = english; }

public Score() { }

public int getChinese() { return chinese; }

public int getMath() { return math; }

public int getEnglish() { return english; }

public void setChinese(int chinese) { this.chinese = chinese; }

public void setMath(int math) { this.math = math; }

public Score(int chinese, int math, int english) {

public void setEnglish(int english) { this.english = english; }

@Override

public String toString() {

return \语文:\数学:\英语:\

} }

2.将课程的编码、课程名称、考试日期、教师姓名 定义为类变量,并添加一个类方法用于设置这些类变量的值,添加一个类方法用于输出这些类变量的值,在主方法中通过类名.类方法名设置课程信息和输出,而学生信息通过构造方法设置。

Exp5_2类:package exp5_2; public class Exp5_2 {

public static void main(String[] args) { ScoreReport stu[]=new ScoreReport[2];

ScoreReport.setCourse(\数分\李教授\

stu[0]=new ScoreReport(\小李\ stu[1]=new ScoreReport(\小陈\ ScoreReport.printCourse(); System.out.println(stu[0]); System.out.println(stu[1]); } }

ScoreReport类:package exp5_2; public class ScoreReport { static String courseID; static String courseName; static String testDate; static String teacherName;

String stuID; String strName; int score;

public static void setCourse(String ID,String Name,String Date,String teacher)

{

courseID=ID;

courseName=Name; testDate=Date;

teacherName=teacher; }

public ScoreReport(String stuID, String strName, int score) {

this.stuID = stuID;

this.strName = strName; this.score = score; }

public static String getCourseID() {

return courseID; }

public static String getCourseName() {

return courseName; }

public int getScore() {

return score; }

public void setScore(int score) { this.score = score; }

public String getStrName(String strName) {