辽宁工程技术大学上机实验报告
实验内容:
(1)设计一个Dog类,有名字、颜色和年龄属性,定义够着方法初始化这些属性,定义输出方法show()显示信息。编写应用程序使用Dog类。
具体程序如下:
publicclass Dog{
public String name;
public String color;
publicint age;
public Dog(String name,String color,int age){
this.name=name;
this.color=color;
this.age=age;
}
publicstaticvoid show(Dog D){
System.out.println("这只狗的名字为:"+D.name+" 颜色为:"+D.color+" 年龄为:"+D.age);
System.out.println();
}
publicstaticvoid main(String[] args) {
Dog dog1=new Dog("Tom","black",3);
Dog dog2=new Dog("Jerry","red",5);
show(dog1);
show(dog2);
}}
运行结果如下:
(2)编写一个学校类,其中包含成员变量line(录取分数线)和对该变量进行设置和获取的方法。编写一个学生类,它的成员变量有考生的name(姓名)、id(考号)、total(综合成绩)、sports(体育成绩)。它还有获取学生的综合成绩和体育成绩的方法。编写一个录取类,它的一个方法用于判断学生是否符合录取条件。其中录取条件为:综合成绩在录取分数线之上,或体育成绩在96以上并且综合成绩大与300。在该类的main()方法中,建立若干个学生对象,对符合录取条件的学生,输出器信息及“被录取”。
具体程序如下:
publicclass School {
publicint line;
publicint Getline(){
return line;
}
publicvoid Setline(int Line){
line=Line;
}}
publicclass Student {
public String name;
public String id;
publicint total;
publicint sport;
publicint Gettotal(){
return total;
}
publicint Getsport(){
return sport;
}
public Student(String name,String id,int total,int sport){
this.name=name;
this.id=id;
this.total=total;
this.sport=sport;
}}
publicclass Admit {
publicstaticvoid AdmitCondition(Student S,School T){
if(S.total>T.line||(S.sport>=96&&S.total>300))
{System.out.println(S.name+" 同学的考号为:"+S.id+" 综合成绩为:"+S.total+" 体育成绩为:"+S.sport);
System.out.println("恭喜"+S.name+"同学被录取.");
System.out.println();}}
publicstaticvoid main(String[] args) {
Student stu1=new Student("Tim","130101",351,85);
Student stu2=new Student("Jarry","130102",630,73);
Student stu3=new Student("Chris","130105",490,98);
School T=new School();
T.Setline(500);
System.out.println("学校的录取分数线为:"+T.Getline());
AdmitCondition(stu1,T);
AdmitCondition(stu2,T);
AdmitCondition(stu3,T);
}
}
运行结果如下:
(3)设计一个表示二维平面上点的类Point,包括表示位置的protected类型的成员变量x和y,获取和设置x和y值的public方法。设计一个表示二维平面上的类Circle,它继承自类Point,还包括表示半径的protected类型的成员变量r、获取和设置r值得public方法、计算圆面积的public 方法。
具体程序如下:
publicclass Point {
protecteddouble x;
protecteddouble y;
publicdouble getX(){
return x;
}
publicdouble getY(){
return y;
}
Point(double X,double Y){
x=X;
y=Y;
}
}
publicclass Circle extends Point {
protecteddouble r;
publicdouble getR(){
return r;
}
Circle(double X,double Y,double radiu){
super(X,Y);
r=radiu;
}
publicdouble getArea(){
return Math.PI*r*r;
}
}
(4)设计一个表示圆柱体的类Cylinder,它继承自类Circle,还包括表示圆柱体高的protected类型的成员变量h、获取和设置h值得public 方法、计算圆柱体体积的public 方法。
具体程序如下:
publicclass Cylinder extends Circle{
protecteddouble h;
publicdouble getH(){
return h;
}
Cylinder(double X,double Y,double r,double height) {
super(X,Y,r);
h=height;
}
publicdouble getCylinderVolume(){
return h*super.getArea();
}
}
(5)建立若干个Cylinder对象,输出其轴心位置坐标、半径和高及其体积的值。
具体程序如下:
publicclass NewCylinder {
publicstaticvoid Print(Cylinder C){
System.out.println("圆柱体轴心的位置坐标为:( "+C.getX()+" , "+C.getY()+" ) 半径为:"+C.getR()+" 高为:"+C.getH());
System.out.println("且其体积为::"+C.getCylinderVolume());
System.out.println();
}
publicstaticvoid main(String[] args) {
Cylinder C1=new Cylinder(0,0,3,5);
Cylinder C2=new Cylinder(5,6,7,3);
Cylinder C3=new Cylinder(-2,-5,5,8);
Cylinder C4=new Cylinder(-3,4,6,5);
System.out.print("C1");Print(C1);
System.out.print("C2");Print(C2);
System.out.print("C3");Print(C3);
System.out.print("C4");Print(C4);
}
}
运行结果如下:
第二篇:java上机实验报告
信息科学与工程学院 《Java程序设计》 上机实验报告
专业班级 电子信息工程技术0903班
姓 名 钟瑜 学 号 201104235005
实验时间 2011年 10 月18 日 指导教师 方红萍 成 绩