资源描述:
《实验三 深入的面向对象编程》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、淮海工学院计算机工程学院实验报告书课程名:《面向对象程序设计》题目:实验三深入的面向对象编程班级:软件112学号:2011122615姓名:黄平评语:成绩:指导教师:批阅时间:年月日《面向对象程序设计》实验报告-5-(1)目的与要求1.定义一个学生类,定义一个大学生类和一个小学生类,这两个类是学生类的子类,定义一个选课接口,由大学生实现这个接口。在此基础上,定义一个年龄异常处理,当小学生年龄小于七岁时,弹出异常。(2)实验内容或题目1、练习实验3的实验例题3-1 3-2。2、在实验指导93页基础题中任选一题完成。如果能顺利完成基础题,可以再继续
2、完成提高题中的要求,提高题根据自己能力选做。(3)实验步骤与源程序importjava.util.*;classAgeExceptionextendsException{Strings;AgeException(){s="注意:小学生年龄不能小于7岁!";}publicStringtoString(){returns;}}classStudent{StringStudentID;StringStudentName;StringStudentPhone;StringStudentGrade;intStudentAge;StringCourse[]=
3、newString[10];Student(StringID,StringName,StringPhone,StringGrade,intAge){StudentID=ID;StudentName=Name;StudentPhone=Phone;StudentGrade=Grade;StudentAge=Age;}publicvoidOutinfor(){System.out.println("学生信息如下:");System.out.println("学号:"+StudentID);System.out.println("姓名:"+Studen
4、tName);《面向对象程序设计》实验报告-5-System.out.println("电话:"+StudentPhone);System.out.println("年级:"+StudentGrade);System.out.println("年龄:"+StudentAge);}}interfaceChoose{voidchoose();}classPrimaryextendsStudent{Primary(StringID,StringName,StringPhone,StringGrade,intAge)throwsAgeException{
5、super(ID,Name,Phone,Grade,Age);if(Age<7){throw(newAgeException());}}}classUniversityextendsStudentimplementsChoose{University(StringID,StringName,StringPhone,StringGrade,intAge){super(ID,Name,Phone,Grade,Age);}publicvoidchoose(){intxuanze=1;inti=1;System.out.println("是否添加课程?(
6、是输入1,否输入0):");Scannerinput1=newScanner(System.in);xuanze=input1.nextInt();while(xuanze==1&&i<=5){System.out.println("请输入你要添加的课程:");Scannerinput=newScanner(System.in);Course[i]=input.nextLine();i++;System.out.println("是否继续添加?(是输入1,否输入0):");Scannerinput2=newScanner(System.in);x
7、uanze=input2.nextInt();}System.out.println("大学生对象所选课程如下:");for(intj=1;j
8、=newPrimary("100012","李四","020_1002","小学一年级",2);p1.Outinfor();}catch