java实验及答案.docx
《java实验及答案.docx》由会员分享,可在线阅读,更多相关《java实验及答案.docx(32页珍藏版)》请在冰豆网上搜索。
![java实验及答案.docx](https://file1.bdocx.com/fileroot1/2023-1/30/8f4c2b61-5985-45e6-8dd5-fb6a0fb5dbc5/8f4c2b61-5985-45e6-8dd5-fb6a0fb5dbc51.gif)
java实验及答案
实验13:
面向对象程序设计—继承
1、根据下面的要求实现人类People
a)人类People的成员变量:
私有成员name表示姓名,为String类型
私有成员sex表示性别,为boolean类型,true表示男,false表示女
私有成员age表示年龄,为int类型
b)人类People的方法:
(1)无参构造方法People(),将各成员变量初始化为默认值
(2)构造方法People(Stringn,booleans,inta)
(3)设置姓名的公有方法
(4)读取姓名的公有方法
(5)设置性别的公有方法
(6)读取性别的公有方法publicStringgetSex()
如果为true,返回male,否则返回female
(7)设置年龄的公有方法
(8)读取年龄的公有方法
(8)voiddisp()将人的姓名、性别、年龄输出到屏幕
2、通过继承第1题中的人People类,派生学生类Student。
要求如下:
a)学生类Student的成员变量:
私有成员department表示所在院系,为String类型
b)学生类Student的方法:
(1)无参构造方法Student(),将各成员变量初始化为默认值
(2)构造方法Student(Stringn,booleans,inta,Stringdep)
(3)设置院系的公有方法
(4)读取院系的公有方法
(5)voiddisp()将学生的姓名、性别、年龄、所在院系输出到屏幕
3、建立学生类Student的测试类StudentTest,任意建立一个学生对象,并调用方法输出该学生的信息。
答案:
1、publicclassPeople
{
privateStringname;
privatebooleansex;
privateintage;
publicPeople()
{
}
publicPeople(Stringn,booleans,inta)
{
setName(n);
setSex(s);
setAge(a);
}
publicvoidsetName(Stringn)
{
name=n;
}
publicStringgetName()
{
returnname;
}
publicvoidsetSex(booleans)
{
sex=s;
}
publicStringgetSex()
{
if(sex==true)
return"male";
else
return"female";
}
publicvoidsetAge(inta)
{
age=a;
}
publicintgetAge()
{
returnage;
}
publicvoiddisp()
{
System.out.println("Name:
"+getName()+"Sex:
"+getSex()+"Age:
"+getAge());
}
}
2、publicclassPeople
{
privateStringname;
privatebooleansex;
privateintage;
publicPeople()
{
}
publicPeople(Stringn,booleans,inta)
{
setName(n);
setSex(s);
setAge(a);
}
publicvoidsetName(Stringn)
{
name=n;
}
publicStringgetName()
{
returnname;
}
publicvoidsetSex(booleans)
{
sex=s;
}
publicStringgetSex()
{
if(sex==true)
return"male";
else
return"female";
}
publicvoidsetAge(inta)
{
age=a;
}
publicintgetAge()
{
returnage;
}
publicvoiddisp()
{
System.out.println("Name:
"+getName()+"Sex:
"+getSex()+"Age:
"+getAge());
}
}
3、publicclassStudentTest
{
publicstaticvoidmain(Stringargs[])
{
Studentstu=newStudent("Zhang",true,20,"SoftWare");
stu.disp();
}
}
实验14:
面向对象程序设计—抽象类
1、定义一个抽象类GeneralPeople,要求如下:
c)成员变量:
私有成员name表示姓名,为String类型
私有成员sex表示性别,为boolean类型,true表示男,false表示女
d)方法:
(1)无参构造方法GeneralPeople(),将各成员变量初始化为默认值
(2)构造方法GeneralPeople(Stringname,booleansex)
(3)设置姓名的公有方法
(4)读取姓名的公有方法
(5)设置性别的公有方法
(6)读取性别的公有方法publicStringgetSex()
如果为true,返回male,否则返回female
(7)抽象方法publicabstractvoiddisp();
2、根据抽象类GeneralPeople派生出学生类Student
e)学生类Student的成员变量:
私有成员school表示所在学校,为Strimg类型
f)学生类Student的方法:
(1)无参构造方法Student(),将各成员变量初始化为默认值
(2)构造方法Student(Stringname,booleansex,Stringschool)
(3)设置学校的公有方法
(4)读取学校的公有方法
(8)实现方法disp(),将学生的姓名、性别、所在学校输出到屏幕
3、根据学生类Student派生出大学生类CollegeStudent,要求如下:
g)CollegeStudent的成员变量:
私有成员specialty表示所学专业,为String类型
h)CollegeStudent的方法:
(1)无参构造方法CollegeStudent(),将各成员变量初始化为默认值
(2)构造方法CollegeStudent(Stringname,booleansex,Stringschool,Stringspecialty)(3)设置专业的公有方法
(4)读取专业的公有方法
(5)voiddisp()将学生的姓名、性别、年龄、所在学校、所学专业输出到屏幕
4、建立大学生类CollegeStudent的测试类大学生类CollegeStudentTest,任意建立一个大学生对象,并调用方法输出该学生的详细信息。
答案:
1、packageP14;
publicabstractclassGeneralPeople{
privateStringname;
privatebooleansex;
publicGeneralPeople(){
super();
}
publicGeneralPeople(Stringname,booleansex){
super();
this.name=name;
this.sex=sex;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicStringgetSex(){
if(sex==true)
return"male";
else
return"female";
}
publicvoidsetSex(booleansex){
this.sex=sex;
}
publicabstractvoiddisp();
}
2、packageP14;
publicclassStudentextendsGeneralPeople{
privateStringschool;
publicStudent(){
super();
}
publicStudent(Stringname,booleansex,Stringschool){
super(name,sex);
this.school=school;
}
publicStringgetSchool(){
returnschool;
}
publicvoidsetSchool(Stringschool){
this.school=school;
}
publicvoiddisp(){
System.out.print("Name:
"+this.getName()+"Sex:
"+this.getSex()+"School:
"+this.getSchool());
}
}
3、packageP14;
publicclassCollegeStudentextendsStudent{
privateStringspecialty;
publicCollegeStudent(){
super();
}
publicCollegeStudent(Stringname,booleansex,Stringschool,Stringspecialty){
super(name,sex,school);
this.specialty=specialty;
}
publicStringgetSpecialty(){
returnspecialty;
}
publicvoiddisp(){
super.disp();
System.out.print("Specialty:
"+this.getSpecialty());
}
}
4、packageP14;
publicclassCollegeStudentTest{
publicstaticvoidmain(String[]args){
CollegeStudentc=newCollegeStudent("john",true,"zzu","soft");
c.disp();
}
}
实验15:
面向对象程序设计—接口
1、定义一个接口GeneralPeople,包含一个抽象方法voiddisp();
2、学生类Student实现接口GeneralPeople
i)学生类Student的成员变量:
私有成员name表示姓名,为String类型
私有成员sex表示性别,为boolean类型,true表示男,false表示女
私有成员school表示所在学校,为Strimg类型
j)学生类Student的方法:
(1)无参构造方法Student(),将各成员变量初始化为默认值
(2)构造方法Student(Stringname,booleansex,Stringschool)
(3)设置姓名的公有方法
(4)读取姓名的公有方法
(5)设置性别的公有方法
(6)读取性别的公有方法publicStringgetSex()
如果为true,返回male,否则返回female
(7)设置学校的公有方法
(8)读取学校的公有方法
(9)实现方法disp(),将学生的姓名、性别、所在学校输出到屏幕
3、根据学生类Student派生出大学生类CollegeStudent,要求如下:
k)CollegeStudent的成员变量:
私有成员specialty表示所学专业,为String类型
l)CollegeStudent的方法:
(1)无参构造方法CollegeStudent(),将各成员变量初始化为默认值
(2)构造方法CollegeStudent(Stringname,booleansex,Stringschool,Stringspecialty)(3)设置专业的公有方法
(4)读取专业的公有方法
(5)voiddisp()将学生的姓名、性别、年龄、所在学校、所学专业输出到屏幕
4、建立接口GeneralPeople的测试程序,要求建立一个GeneralPeople类型的数组g,任意建立几个Student类型的对象和CollegeStudent类型的对象,将这些对象依次赋值给数组g中的数组元素,多态地调用方法disp()。
并练习使用操作符instanceof和方法getClass().getName()。
答案:
1、packageP15;
publicinterfaceGeneralPeople{
voiddisp();
}
2、packageP15;
publicclassStudentimplementsGeneralPeople{
privateStringname;
privatebooleansex;
privateStringschool;
publicStudent(){
super();
}
publicStudent(Stringname,booleansex,Stringschool){
super();
this.name=name;
this.sex=sex;
this.school=school;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicStringgetSchool(){
returnschool;
}
publicvoidsetSchool(Stringschool){
this.school=school;
}
publicStringgetSex(){
if(sex==true)
return"male";
else
return"female";
}
publicvoidsetSex(booleansex){
this.sex=sex;
}
publicvoiddisp(){
System.out.print("Name:
"+this.getName()+"Sex:
"+this.getSex()+"School:
"+this.getSchool());
}
}
3、packageP15;
publicclassCollegeStudentextendsStudent{
privateStringspecialty;
publicCollegeStudent(){
super();
}
publicCollegeStudent(Stringname,booleansex,Stringschool,Stringspecialty){
super(name,sex,school);
this.specialty=specialty;
}
publicStringgetSpecialty(){
returnspecialty;
}
publicvoiddisp(){
super.disp();
System.out.print("Specialty:
"+this.getSpecialty());
}
}
4、packageP15;
publicclassGeneralPeopleTest{
publicstaticvoidmain(String[]args){
Students=newStudent("mary",false,"zzu");
CollegeStudents2=newCollegeStudent("henry",true,"zzu","management");
CollegeStudents3=newCollegeStudent("zhang",true,"zzu","management");
GeneralPeopleg[]=newGeneralPeople[3];
g[0]=s;
g[1]=s2;
g[2]=s3;
for(inti=0;i{
System.out.print(g[i].getClass().getName()+":
");
g[i].disp();
System.out.println();
}
System.out.println(g[0]instanceofCollegeStudent);
}
}
实验16:
图形用户界面—颜色和字体控制
1、在一个窗口中分别以5种不同的颜色和字体显示字符串“你好!
中国!
”
答案:
packageP16;
1、importjava.awt.*;
importjavax.swing.*;
publicclassColorFontControlextendsJFrame{
publicColorFontControl()
{
super.setSize(600,500);
super.setLocation(100,150);
super.setVisible(true);
}
publicvoidpaint(Graphicsg){
super.paint(g);
Strings[]=GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
for(inti=s.length-5;i{
Colorc=newColor((float)Math.random(),(float)Math.random(),(float)Math.random());
g.setColor(c);
Fontf=newFont(s[i],Font.PLAIN,i-20);
g.setFont(f);
g.drawString("你好,中国!
",100,(s.length-i)*80);
}
}
publicstaticvoidmain(String[]args){
ColorFontControlframe=newColorFontControl();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
实验17:
图形用户界面设计——事件处理
设计如下界面,添加“事件处理”,要求程序具备以下功能:
(1)点击“确定”按钮在一消息对话框或者在一标签上显示刚刚输入的信息;
(2)点击“取消”按钮清空各文本框里的内容,“学号”后的文本框获得输入焦点.
答案:
1、packageP17;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassUseEventFrameextendsJFrame{
StringlabelName[]={"学号","姓名","性别","年龄","学院","专业"};
intfieldLen[]={10,5,3,3,6,10};
privateJLabellabel[]=newJLabel[6];
privateJTextFieldfield[]=newJTextField[6];
privateJButtonoBtn,cBtn;
privateJLabeldisLabel;
publicUseEventFrame()
{
super.setSize(750,400);
super.setLocation(50,80);
Containerc=this.getContentPane();
c.setLayout(newFlowLayout());
for(inti=0;i{
label[i]=newJLabel(labelName[i]);
}
for(inti=0;i{
field[i]=newJTextField(fieldLen[i]);
}
for(inti=0;i<6;i++)
{
c.add(label[i]);
c.add(field[i]);
}
Iconic1=newImageIcon("1.gif");
Iconic2=newImageIcon("2.gif");
oBtn=newJButton("确定",ic1);
cBtn=newJButton("取消",ic2);
c.add(oBtn);
c.add(cBtn);
disLabel=newJLabel();
c.add(disLabel);
MyListenerlis=newMyListener();
oBtn.addActionListener(lis);
cBtn.addActionListener(lis);
super.setVisible(true);
}
p