实验5 继承、基础类与包
- 格式:docx
- 大小:126.19 KB
- 文档页数:9
实验5 继承、基础类与包 一、实验目的 1、理解类的继承,终结类、终结方法、抽象类与抽象方法的概念。 2、熟练掌握终结类、终结方法、抽象类与抽象方法的定义及实现方法。 3、熟练掌握本书提到的Java基础类库中的一些常见类,学会查阅JavaDoc。 4、了解Java包的概念及编译运行方法,养成良好的命名习惯。 实验五第一题内容: 1. 题目: /*编写一抽象类(Shape2D), * Circle、Tritangle、Rectangle均为其子类。 * 其中父类中有计算其周长、面积的方法。然后编写一测试类, * 分别建立若干个子对象,并分别将这些对象的面积与周长输出。*/
2.实验主要流程、基本操作或核心代码、算法片段(该部分如不够填写,请另加附页) publicabstractclass Shape2D { publicabstractdouble getCir(); publicabstractdouble gteSq(); }
class Circle extends Shape2D{ privatestaticfinaldoublePI = 3.14; doubler; public Circle(int i) { r=i; }
publicdouble getR() { returnr; }
publicvoid setR(double r) { this.r = r; } publicdouble getCir(){ return 2*PI*this.r; }
publicdouble gteSq() { returnPI*this.r*this.r; } } class Tritangle extends Shape2D { doublesideA,sideB,sideC,area,length; booleanboo; public Tritangle(double a,double b,double c) { this.sideA=a; this.sideB=b; this.sideC=c; if(((a+b)>c&&(a-b)b)&&(a-ca)&&(b-c角形的条件表达式 { boo=true; } else { boo=false; } } publicdouble getCir() { returnsideA+sideB+sideC; } publicdouble gteSq() { if(boo) { double p=(sideA+sideB+sideC)/2.0; area=Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC)) ; returnarea; } else { System.out.println("不是一个三角形,不能计算面积"); return 0; } }
} class Rectangle extends Shape2D{ doublewid; doublelen; public Rectangle(){} public Rectangle(double width,double length){ this.wid = width; this.len = length; } publicdouble getCir() { return (wid+len)*2; }
publicdouble gteSq() { returnwid*len; }
} class Square extends Rectangle { doublewidth; public Square(double width) { //super(width, width); this.width=width; // TODO Auto-generated constructor stub } doublewid; publicdouble getCir() { return 4*width; }
publicdouble gteSq() { returnwidth*width; }
} class Test{
publicstaticvoid main(String[] args) { Circle c1=new Circle(5); System.out.println("the circumsference of the circle is :"); System.out.println(+c1.getCir()); System.out.println("the squae of the circle is :"); System.out.println(+c1.gteSq());
Tritangle t1=new Tritangle(7,4,5); System.out.println("the circumsference of the circle is :"); System.out.println(+t1.getCir()); System.out.println("the squae of the circle is :"); System.out.println(+t1.gteSq());
Rectangle r1=new Rectangle(7,4); System.out.println("the circumsference of the circle is :"); System.out.println(+r1.getCir()); System.out.println("the squae of the circle is :"); System.out.println(+r1.gteSq());
Square s1=new Square(7); System.out.println("the circumsference of the circle is :"); System.out.println(+s1.getCir()); System.out.println("the squae of the circle is :"); System.out.println(+s1.gteSq());
} } 3.实验结果的分析与评价(该部分如不够填写,请另加附页)
实验五第二题内容: 1.题目:/*(1) 声明Student类。 属性包括学号、姓名、英语成绩、数学成绩、计算机成绩和总成绩。 方法包括:构造方法、get方法、 set方法、toString方法、equals方法、compare方法(比较两个学生的总成绩,结果分为大于、小于、等于),sum方法(计算总成绩) 和testScore方法(计算评测成绩)。 注:评测成绩可以取三门课成绩的平均分,另外任何一门课的成绩的改变都需要对总成绩进行重新计算,因此,在每一个set方法中应调用sum方法计算总成绩。 (2) 声明StudentXW(学习委员)类为Student类的子类。 在StudentXW类中增加责任属性,并重写testScore方法(计算评测成绩,评测成绩=三门课平均分+3) (3) 声明StudentBZ(班长)类为Student类的子类。 在StudentBZ类中增加责任属性,并重写testScore方法(计算评测成绩,评测成绩=三门课平均分+5) (4) 声明测试类,生成若干个Student类、StudentXW类及StudentBZ类对象,并分别计算它们的评测成绩。*/
2.实验主要流程、基本操作或核心代码、算法片段(该部分如不够填写,请另加附页) publicclass Student { String name; public String getName() { returnname; } publicvoid setName(String name) { this.name = name; } String num; public String getNum() { returnnum; } publicvoid setNum(String num) { this.num = num; } publicint getEsco() { returnesco; } publicvoid setEsco(int esco) { this.esco = esco; sum(); } publicint getMsco() { returnmsco; } publicvoid setMsco(int msco) { this.msco = msco; sum(); } publicint getTsco() { returntsco; } publicvoid setTsco(int tsco) { this.tsco = tsco; sum(); } publicint getSum() { returnsum; }