Java课程第三次实验报告
this.profession=profession;
}
publicString toString(){
returnsuper.toString()+"\t专业:"+this.profession;
}
}
Collegestudenttest.java代码如下:
package学生类的继承;
import学生类的继承.Student;
2.掌握继承类的编写方法
二、实验环境(实验设备)
1.每位学生配备计算机一台
2.计算机需安装好JDK和JCreator
三、实验内容
1.在上一次写的学生类的基础上派生出一个子类——大学生类(实验书P115T1),并根据题目要求编写主类测试之。
Student.java类代码如下:
package学生类的继承;
this.num=num;
}
publicvoidsetclassnum(String classnum){
this.classnum=classnum;
}
publicvoidsetname(String name){
=name;
}
publicvoidsetsex(String sex){
Student s2=newStudent("002","9802","李四","女",21);
System.out.println("构造s2后,班级人数为:"+Student.getcount());
s1.setname("王平");
s2.setage(19);
System.out.println(s1.toString());
publicString toString(){
return"[矩形]"+"长度:"+this.Length+"\t宽度:"+this.Width;
}
}
MyRectangleTest.java代码如下:
package矩形类;
import矩形类.MyRectangle;
publicclassMyRectangleTest {
System.out.println(s3.toString());
}
}
实验结果:
2.定义一个矩形类,计算其面积,定义派生类计算正方形面积。具体要求见课本P104T5.8
MyRectangle.java代码如下:
package矩形类;
publicclassMyRectangle {
privateintLength;
returnWidth;
}
publicintgetArea(){
returnLength*Width;
}
publicvoidsetLength(intLength){
this.Length=Length;
}
publicvoidsetWidth(intWidth){
this.Width=Width;
}
privateinty;
publicPoint(intx,inty){
this.x=x;
this.y=y;
}
publicintgetx(){
returnx;
}
publicintgety(){
returny;
}
publicvoidsetx(intx){
this.x=x;
}
publicvoidsety(inty){
}
publicintgetVolume(){
returnsuper.getArea1()*Height;
}
publicString toString(){
return"[长方体]"+"\t横坐标:"+super.getx()+"\t纵坐标:"+super.gety()+"\t长度:"+super.getLength()+"\t宽度:"+super.getWidth()+"\t高度:"+this.Height+"\t面积:"+this.getArea2()+"\t体积:"+this.getVolume();
publicclassCollegestudentTest {
publicstaticvoidmain(String[] args) {
//TODO自动生成的方法存根
Student s1=newStudent("001","9801","张三","男",24);
System.out.println("构造s1后,班级人数为:"+Student.getcount());
this.y=y;
}
publicString toString(){
return"[点]"+"横坐标:"+this.x+"纵坐标:"+this.y;
}
}
classRectangleextendsPoint{
privateintLength;
privateintWidth;
publicRectangle(intx,inty,intLength,intWidth){
实验报告
(2015 / 2016学年第2学期)
课程名称
JAVA语言程序设计
实验名称
Java对象和类的使用
实验时间
2016
年
月
日
指导单位
计算机学院/软件学院
软件工程系
指导教师
朱艳梅
学生姓名
班级学号
学院(系)
专业
实验名称
Java对象和类的使用
指导教师
朱艳梅
实验类型
上机
实验学时
2
实验时间
一、实验目的
1.掌握类的基本概念,会编写简单的类
returnHeight;
}
publicvoidsetHeight(intHeight){
this.Height=Height;
}
publicintgetArea2(){
return2*super.getLength()*super.getWidth()+2*super.getLength()*Height+2*super.getWidth()*Height;
return"[矩形]"+"\t横坐标:"+super.getx()+"\t纵坐标:"+super.gety()+"\t长度:"+this.Length+"\t宽度:"+this.Width+"\t面积:"+this.getArea1()+"\t周长:"+this.getPerimeter();
}
}
System.out.println("修改A1后,矩形面积为:"+A1.getArea());
}
}
运行结果:
3.利用继承关系,定义点类、矩形类、长方体类,并测试之,设计思路见实验书P115。
Point.java代码如下:
package类的继承;
publicclassPoint {
privateintx;
}
publicintgetPerimeter(){
return2*(Length+Width);
}
publicvoidsetLength(intLength){
this.Length=Length;
}
publicvoidsetWidth(intWidth){
this.Width=Width;
}
publicString toString(){
this.num=num;
this.cLeabharlann assnum=classnum;=name;
this.sex=sex;
this.age=age;
count++;
}
publicstaticintgetcount(){
returncount;
}
publicString getnum(){
returnnum;
super(x,y);
this.Length=Length;
this.Width=Width;
}
publicintgetLength(){
returnLength;
}
publicintgetWidth(){
returnWidth;
}
publicintgetArea1(){
returnLength*Width;
Collegestudent s3=newCollegestudent("003","9803","林飞","男",21,"光电信息科学与工程");
System.out.println(s3.toString());
s3.setnum("629");
s3.setclassnum("9810");