Java课程第三次实验报告
\1. 在前面实验二已定义的学生类Student的基础上,以Student类为父类,为学生类派生出一个子类为大学生类(CollegeStudent)。
CollegeStudent 类在学生类上增加一个专业(profession)数据属性;方法上增加获得专业和设置专业两个方法。并对超类中的toString( )方法进行重写,使得CollegeStudent类中的toString( )方法除了显示学生类的信息外,还要显示它的专业属性。
编写测试程序的主类。在主类中创建一个Student对象和CollegeStudent对象,并显示或修改这两个对象的属性值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| package student ; public class Student { private int UID; private String name; private Boolean gender; private int age; static int cnt ; public Student() {} public Student(int UID,String name,Boolean gender,int age) { cnt ++ ; this.UID = UID; this.name = name; this.gender = gender; this.age = age; }
public int getAge() { return age; } public String getName() { return name; } public Boolean getGender() { return gender; } public int getUID() { return UID; }
public void setAge(int age) { this.age = age; } public void setUID(int uID) { UID = uID; } public void setName(String name) { this.name = name; } public void setGender(Boolean gender) { this.gender = gender; } @Override protected void finalize() throws Throwable { System.out.println("对象已被销毁");; } @Override public String toString() { if (gender) return "该同学的学号为:" + UID +",姓名叫:" + name + ",性别:男" + ",年龄:" + age; return "该同学的学号为:" + UID +",姓名叫:" + name + ",性别:女" + ",年龄:" + age; } public static int getCnt() { return cnt; } } class CollegeStudent extends Student{ private String profession; CollegeStudent(int studentID, String name, Boolean sex, int age, String profession) { super( studentID, name, sex, age ); this.profession = profession; } public String getProfession() { return profession; } public void setProfession(String profession) { this.profession = profession; } @Override public String toString() { return "CollegeStudent{" + "profession='" + profession + '\'' + "studentID='" + super.getUID() + '\'' + ", name='" + super.getName() + '\'' + ", sex='" + super.getGender() + '\'' + ", age=" + super.getAge() + '}'; } }
|
- 设计一个人员类(Person),其中包含一个方法pay,代表人员的工资支出。再从Person类派生出助教类(Assistant)、讲师类(Instructor)、副教授类(Assistant Professor)和教授类(Professor)。其中:工资支出=基本工资+授课时数*每课时兼课金。
但助教基本工资为800,每课时兼课金25,讲师基本工资为1000,每课时兼课金35,
副教授基本工资为1200,每课时兼课金40,教授基本工资为1400,每课时兼课金50。
1.将Person定义为抽象类,pay为抽象方法,设计程序实现多态性。
2.将Person定义为接口,设计程序实现多态性。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| class Person { public int basic; public int hour; public int charge; public Person() { } public Person(int basic, int charge) { this.basic = basic; this.charge = charge; } public void pay(int hour) { System.out.println( hour + "小时后的工资为" + (basic + hour * charge) ); } } class Assistant extends Person { public Assistant() { super( 800, 25 ); } } class Instructor extends Person { public Instructor() { super( 1000, 35 ); } } class AssistantProfessor extends Person { public AssistantProfessor() { super( 1200, 40 ); } } class Professor extends Person { public Professor() { super( 1400, 50 ); } }
abstract class Person { public int hour; public abstract void pay(int hour); class Assistant extends Person { @Override public void pay(int hour) { System.out.println( hour + "小时后的工资为" + (800 + hour * 25) ); } } class Instructor extends Person { @Override public void pay(int hour) { System.out.println( hour + "小时后的工资为" + (1000 + hour * 35) ); } } class AssistantProfessor extends Person { @Override public void pay(int hour) { System.out.println( hour + "小时后的工资为" + (1200 + hour * 40) ); } } class Professor extends Person { @Override public void pay(int hour) { System.out.println( hour + "小时后的工资为" + (1400 + hour * 50) ); } }
interface Person { void pay(int hour); } class Assistant implements Person { @Override public void pay(int hour) { System.out.println( hour + "小时后的工资为" + (800 + hour * 25) ); } } class Instructor implements Person { @Override public void pay(int hour) { System.out.println( hour + "小时后的工资为" + (1000 + hour * 35) ); } } class AssistantProfessor implements Person { @Override public void pay(int hour) { System.out.println( hour + "小时后的工资为" + (1200 + hour * 40) ); } } class Professor implements Person { @Override public void pay(int hour) { System.out.println( hour + "小时后的工资为" + (1400 + hour * 50) ); } }
|
- 从键盘输入两个数,进行相除,显示商。当输入串中含有非数字时或除数为0时,通过异常处理机制,使程序能正确运行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import java.util.InputMismatchException; import java.util.Scanner; public class yichang { public static void main(String[] args) { try { Scanner scanner = new Scanner(System.in); System.out.print( "请输入除数:" ); int dividedNum = scanner.nextInt(); System.out.print( "请输入被除数:" ); int divNum = scanner.nextInt(); System.out.println( "计算结果:" + dividedNum / divNum ); } catch (InputMismatchException e1) { System.out.println("输入不为数字!"); }catch (ArithmeticException e2) { System.out.println("不能除0!"); } } }
|