Java 实验二
实验二类与对象
实验目的
1、掌握面向对象程序设计的方法与Java 作为面向对象程序设计语言的特点;
2、掌握修饰符与构造方法的使用规则;
3、掌握接口的特点、结构、调用与继承;
4、掌握如何创建包, 通过包如何管理类;
5、掌握Java 的继承机制与实现多态的方法实验内容
(1)定义一个类Student,属性为学号、姓名与成绩;方法为增加记录SetRecord与得到记录GetRecord。
SetRecord赋值学号、姓名与成绩,GetRecord通过学号得到考生的成绩。
通过实例验
证编程无误。
⑵定义一个接口Area,其中包含一个计算面积的抽象方法calculateArea()然后设
计Circle与Rectan两个类实现这个接口的方法calculateArea()分别计算圆与矩形的面积。
通过实例验证编程无误。
(3) 假定根据学生的3门学位课程的分数决定其就是否可以拿到学位,对于本科生, 如果3门课程的平均分数超过60分即表示通过,而对于研究生,则需要平均分超过80分才能够通过。
根据上述要求,请完成以下Java类的设计:
(i)设计一个基类Student描述学生的共同特征。
(ii)设计一个描述本科生的类Undergraduate该类继承并扩展Student类。
(iii)设计一个描述研究生的类Graduate该类继承并扩展Student类。
(iv)设计一个测试类StudentDemo分别创建本科生与研究生这两个类的对象,并输
出相关信息
附实验一名称就是Java 语言基础实验报告分为以下几个部分
一实验名称二实验目的三实验仪器四实验步骤(把您的操作一步一步写清楚,java 程序代码要写) 五实验结果(程序运行后的结果就就是DOS 环境下运行出来的结果写在实验报告上) 六实验讨论(实验过程中的错误及如何改正,您的心得体会等)
答案:
(1)定义一个类Student,属性为学号、姓名与成绩;方法为增加记录SetRecord与得到记录GetRecord。
SetRecord 赋值学号、姓名与成绩,GetRecord 通过学号得到考生的成绩。
通过实例
验证编程无误
public class Student {
private int ID;
private String name;
private float score;
public void SetRecord(int ID,String name,float score){
this、ID=ID;
this、name=name;
this、score=score;
}
public float getRecord(int ID){ if(ID==this 、ID) return this 、score;
else
return -1;
}
public static void main(String[] args) { Student s=new Student(); s、SetRecord(0,"alex",100); float Sco=s、getRecord(0); System、out 、
print(Sco);
}
}
⑵定义一个接口Area,其中包含一个计算面积的抽象方法calculateArea()然后设
计Circle与Rectan两个类实现这个接口的方法calculateArea()分别计算圆与矩形的面积。
通过实例验证编程无误
in terface Area{ float calculateArea();
}
class Circle implements Area{
float radius;
Circle (float r){ this、radius=r;
}
public float calculateArea(){ return (float) Math、PI*radius*radius;
}
}
class Rectan implements Area{
float width;
float height;
Rectan(float w,float h){
this、width=w; this、height=h;
}
public float calculateArea(){ return (float)width*height;
}
}
public class Interface{
public static void main (String args[]){
Circle circle =new Circle(5);
System、out、println ("Circle Area="+circle 、calculateArea());
Rectan rect=new Rectan(12,6);
System、out、println("Rectangle Area="+rect 、calculateArea());
}
}
(3) 假定根据学生的3 门学位课程的分数决定其就是否可以拿到学位,对于本科生, 如果3门课程的平均分数超过60分即表示通过,而对于研究生,则需要平均分超过80分才能够通过。
根据上述要求,请完成以下Java类的设计:
(i)设计一个基类Student描述学生的共同特征。
(ii)设计一个描述本科生的类Undergraduate该类继承并扩展Student类。
(iii)设计一个描述研究生的类Graduate该类继承并扩展Student类。
(iv)设计一个测试类StudentDemo分别创建本科生与研究生这两个类的对象,并输
出相关信息。
class Student
{
public String name;
public double a,b,c;
public double average;
int status;
}
class Undergraduate extends Student
{
Undergraduate(String s,double a,double b,double c) {
this、name = s;
this、a = a;
this、b = b;
this、c = c;
this、average = (a+b+c)/3;
}
public void Display()
{
if(this 、average>=60)
status = 1;
else
status=0;
if(status==1)
System out、println(name+":通过!");
if(status==0)
System out、println(name+":不通过!");
}
}
class Graduate extends Student
{
Graduate(String s,double a,double b,double c)
{ this、name = s; this、a = a; this、b = b; this、c = c; this、average = (a+b+c)/3;
}
public void Display()
{ if(this 、average>=80) status = 1; else status=0;
if(status==1) System、out、println(name+": 通过!");
if(status==0)
System、out、println(name+": 不通过!");
}
}
public class StudentDemo
{
public static void main(String args[])
{
Un dergraduate st1 = new Un dergraduate张三",50,70,60); st1、Display();
Graduate st2=new Graduate(李四",70,80,90);
st2、Display();
}
}
附:这三个程序中相关的实例都就是随意举的,您也可以改成自己的。