面向对象程序设计实验15.docx

上传人:b****5 文档编号:11979060 上传时间:2023-04-16 格式:DOCX 页数:18 大小:191.51KB
下载 相关 举报
面向对象程序设计实验15.docx_第1页
第1页 / 共18页
面向对象程序设计实验15.docx_第2页
第2页 / 共18页
面向对象程序设计实验15.docx_第3页
第3页 / 共18页
面向对象程序设计实验15.docx_第4页
第4页 / 共18页
面向对象程序设计实验15.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

面向对象程序设计实验15.docx

《面向对象程序设计实验15.docx》由会员分享,可在线阅读,更多相关《面向对象程序设计实验15.docx(18页珍藏版)》请在冰豆网上搜索。

面向对象程序设计实验15.docx

面向对象程序设计实验15

浙江大学城市学院实验报告

课程名称:

面向对象程序设计实验

实验项目名称:

综合练习

学生姓名:

专业:

学号:

实验地点:

理四226实验日期:

09年6月3日

【实验目的】

综合练习关于面向对象的知识点及编辑思想

【实验报告】

将编程题(2-1至2-8)的源程序、运行结果以及实验中遇到的问题和解决问题的方法,写在实验报告上。

2-1名词解释,请解释面向对象的四大特性的概念,抽象、封装、继承、多态

抽象:

从具体的实例中抽取共同的性质形成一般的概念。

封装:

一个对象将自己的数据和对这些数据的操作合理,有效地封装在一起。

继承:

子类可以继承父类的属性和功能,即继承了父类的数据和数据上的操作,同时又可以增加子类独有的数据和数据上的操作。

多态:

一是操作名称的多态,即有多个操作具有相同的名字,但这些操作所接收的消息类型必须不同;二是与继承有关的多态,是指同一操作被不同类型的对象调用时可能产生不同的行为。

2-2题目要求:

设计一组判断成绩等级的练习。

要求:

从键盘输入学生的语文成绩,判断该学生成绩的等级,其中≥90:

’A’,89~80:

’B’,79~70:

’C’,69~60:

’D’,<60:

’E’。

1、用if~else实现成绩判定。

2、用switch语句实现成绩判断.

3、评判多个学生成绩等级。

程序代码及运行结果如下:

1.

testGrade1.java:

importjava.util.*;

publicclasstestGrade1{

publicstaticvoidmain(Stringargs[]){

Scannerreader=newScanner(System.in);

System.out.println("输入一个学生的语文成绩:

");

intn=reader.nextInt();

if(n>100||n<0)

System.out.println("成绩输入非法!

");

elseif(n>=90)

System.out.println("A");

elseif(n>=80)

System.out.println("B");

elseif(n>=70)

System.out.println("C");

elseif(n>=60)

System.out.println("D");

else

System.out.println("E");

}

}

2.

testGrade2.java:

importjava.util.*;

publicclasstestGrade2{

publicstaticvoidmain(Stringsrgs[]){

Scannerreader=newScanner(System.in);

System.out.println("输入一个学生的语文成绩:

");

intn=reader.nextInt();

if(n==100)

System.out.println("A");

else

switch(n/10){

case1:

case2:

case3:

case4:

case5:

System.out.println("E");

break;

case6:

System.out.println("D");

break;

case7:

System.out.println("C");

break;

case8:

System.out.println("B");

break;

case9:

System.out.println("A");

break;

default:

System.out.println("成绩输入非法!

");

}

}

}

3.

testGrade3.java:

importjava.util.*;

publicclasstestGrade3{

publicstaticvoidmain(Stringsrgs[]){

inta[]=newint[10];

Scannerreader=newScanner(System.in);

System.out.println("输入10个学生的语文成绩:

");

for(inti=0;i<10;i++)

a[i]=reader.nextInt();

for(inti=0;i<10;i++)

if(a[i]==100)

System.out.printf("A");

else

switch(a[i]/10){

case1:

case2:

case3:

case4:

case5:

System.out.printf("E");

break;

case6:

System.out.printf("D");

break;

case7:

System.out.printf("C");

break;

case8:

System.out.printf("B");

break;

case9:

System.out.printf("A");

break;

default:

System.out.printf("成绩输入非法!

");

}

}

}

2-3题目要求:

编写程序实现目录和文件的创建、删除和重命名

程序代码及运行结果如下:

textEdit.java:

importjava.io.*;

importjava.util.*;

publicclasstextEdit{

publicstaticvoidmain(Stringargs[]){

Scannerreader=newScanner(System.in);

Stringedit;

System.out.println("输入需要执行的操作(目录创建,目录删除,目录重命名,文件创建,文件删除,文件重命名,退出操作):

");

edit=reader.nextLine();

while(!

edit.equals("退出操作")){

if(edit.equals("目录创建")){

Stringpath,dir;

System.out.println("输入需要创建目录的路径:

");

path=reader.nextLine();

System.out.println("输入需要创建的目录名:

");

dir=reader.nextLine();

Filef=newFile(path,dir);

if(f.mkdir())

System.out.println("创建成功!

");

else

System.out.println("创建失败!

");

}

elseif(edit.equals("目录删除")){

Stringpath,dir;

System.out.println("输入需要删除目录的路径:

");

path=reader.nextLine();

System.out.println("输入需要删除的目录名:

");

dir=reader.nextLine();

Filef=newFile(path,dir);

if(f.delete())

System.out.println("删除成功!

");

else

System.out.println("删除失败!

");

}

elseif(edit.equals("目录重命名")){

Stringpath,dir,name;

System.out.println("输入需要重命名目录的路径:

");

path=reader.nextLine();

System.out.println("输入需要重命名目录的名字:

");

dir=reader.nextLine();

System.out.println("输入目录修改后的名字:

");

name=reader.nextLine();

Filef=newFile(path,dir);

Filed=newFile(path,name);

if(f.renameTo(d))

System.out.println("重命名成功!

");

else

System.out.println("无法重命名!

");

}

elseif(edit.equals("文件创建")){

Stringpath,dir;

System.out.println("输入需要创建文件的路径:

");

path=reader.nextLine();

System.out.println("输入需要创建的文件名:

");

dir=reader.nextLine();

Filef=newFile(path,dir);

try{

if(f.createNewFile())

System.out.println("创建成功!

");

else

System.out.println("创建失败!

");

}catch(IOExceptione){

//TODO自动生成catch块

e.printStackTrace();

}

}

elseif(edit.equals("文件删除")){

Stringpath,dir;

System.out.println("输入需要删除文件的路径:

");

path=reader.nextLine();

System.out.println("输入需要删除的文件名:

");

dir=reader.nextLine();

Filef=newFile(path,dir);

if(f.delete())

System.out.println("删除成功!

");

else

System.out.println("删除失败!

");

}

elseif(edit.equals("文件重命名")){

Stringpath,dir,name;

System.out.println("输入需要重命名文件的路径:

");

path=reader.nextLine();

System.out.println("输入需要重命名文件的名字:

");

dir=reader.nextLine();

System.out.println("输入文件修改后的名字:

");

name=reader.nextLine();

Filef=newFile(path,dir);

Filed=newFile(path,name);

if(f.renameTo(d))

System.out.println("重命名成功!

");

else

System.out.println("无法重命名!

");

}

System.out.println("输入需要执行的操作(目录创建,目录删除,目录重命名,文件创建,文件删除,文件重命名,退出操作):

");

edit=reader.nextLine();

}

}

}

2-4题目要求:

定义一个Person类,可以在应用程序中使用该类。

成员属性:

Person类的属性(变量):

     姓名:

name,字符串类型:

String;

     性别:

sex,字符型:

char;

     年龄:

age,整型:

int。

3个重载的构造函数:

     public Person(String s)            //设置姓名

     public Person(String s,char c)       //调用本类的构造函数Person(String s),设置性别

     public Person(String s,char c,int i)//调用本类的构造函数PersonPerson(String s,char),设置年龄

一个成员方法:

     public String toString()//获得姓名、性别和年龄

利用定义的Person类,请实例化对象,输出下面结果:

姓名:

张三   性别:

男  年龄:

21

程序代码及运行结果如下:

Test4.java:

importjava.util.*;

classPerson{

Stringname;

charsex;

intage;

publicPerson(Strings){

this.name=s;

}

publicPerson(Strings,charc){

this(s);

this.sex=c;

}

publicPerson(Strings,charc,inti){

this(s,c);

this.age=i;

}

publicStringtoString(){

System.out.println("姓名:

"+this.name+"性别:

"+this.sex+"年龄:

"+this.age);

returnnull;

}

}

publicclassTest4{

publicstaticvoidmain(Stringargs[]){

Personperson=newPerson("张三",'男',21);

person.toString();

}

}

2-5定义一个学生类Student,它继承自person类。

(1)Student类有以下几个变量

   继承自父类的变量:

姓名(name),字符串类型(String);性别(sex),字符型(char);年龄(age),整型(int)。

   子类新增加的变量:

    学号(number),长整型;

三门功课的成绩:

哲学(phi),整型;英语(eng),整型;计算机(comp),整型。

(2)Student类有以下几个方法

    子类新增加的方法:

求三门功课的平均成绩aver():

该方法没有参数,返回值类型为double型;

求三门功课成绩的最高分max():

该方法没有参数,返回值为int型;

求三门功课成绩的最低分min();该方法没有参数,返回值为int型。

覆盖父类的同名方法:

toString() 获取学号、姓名、性别、平均分、最高分、最低分信息。

例如:

 

学号:

1234567 姓名:

张三  性别:

男  平均分:

90.0 最高分:

95分  最低分:

87

程序代码及运行结果如下:

Test5.java:

importjava.util.*;

classPerson{

Stringname;

charsex;

intage;

publicPerson(Strings){

this.name=s;

}

publicPerson(Strings,charc){

this(s);

this.sex=c;

}

publicPerson(Strings,charc,inti){

this(s,c);

this.age=i;

}

publicStringtoString(){

System.out.println("姓名:

"+this.name+"性别:

"+this.sex+"年龄:

"+this.age);

returnnull;

}

}

classStudentextendsPerson{

longnumber;

intphi;

inteng;

intcomp;

publicStudent(Strings,charc,inti,longx,inta,intb,intd){

super(s,c,i);

this.number=x;

this.phi=a;

this.eng=b;

p=d;

}

doubleaver(){

doubleresult;

result=((double)(phi+eng+comp))/3.0;

returnresult;

}

intmax(){

intresult;

if(phi>eng){

if(phi>comp){

result=phi;

}

else{

result=comp;

}

}

else{

if(eng>comp){

result=eng;

}

else{

result=comp;

}

}

returnresult;

}

intmin(){

intresult;

if(phi

if(phi

result=phi;

}

else{

result=comp;

}

}

else{

if(eng

result=eng;

}

else{

result=comp;

}

}

returnresult;

}

publicStringtoString(){

System.out.println("学号:

"+this.number+"姓名:

"+this.name+"性别:

"+this.sex+"平均分:

"+this.aver()+"最高分:

"+this.max()+"最低分:

"+this.min());

returnnull;

}

}

publicclassTest5{

publicstaticvoidmain(Stringargs[]){

Studentstudent=newStudent("张三",'男',22,1234567,88,87,95);

student.toString();

}

}

(将本实验报告命名为:

学号_姓名_report15.doc)ftp:

//10.66.27.117:

2009/yxjupload/123456/实验报告十五(对应的第几节课的目录下,注意最迟上交时间6月10日下午四点)

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 总结汇报 > 工作总结汇报

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1