当前位置:文档之家› 实验报告(四)继承和多态 (完整代码)

实验报告(四)继承和多态 (完整代码)

str1 += String.valueOf(i + 1) + circle[i] + "\n";
}
System.out.println("您所创建的圆按半径由小到大排序如下:\n" + str1);
Shape.sort(rectangle);
String str2 = "";
for (int i = 0; i < rectangle.length; i++) {
输入:
预期输出:
实际输出:
分析
六、实验结果
用与测试用例不同的输入数据运行算法,写出得到的结果,并分析结果是否正确。
输入:
输出结果:
结果分析:
七、总结
对上机实验结果进行分析、上机的心得体会及改进意见。
附录:
如果原来的算法中发现了错误,在附录中附上改正后的算法实现。
setLength(length);
setWidth(width);
}
public double getLength() {
return length;
}
public void setLength(double val) {
this.length = (val <= 0 ? 1 : val);
}
public double getWidth() {
} else {
System.out.println("未找到符合的圆!");
}
System.out.println("要查找的矩形的长和宽?:");
Scanner f6 = new Scanner(System.in);
Rectangle r1 = new Rectangle(f6.nextDouble(), f6.nextDouble());
sort(list);
while (low <= high) {
mid = (low + high) / 2;
if (((Comparable) target).compareTo(list[mid]) < 0) {
high = mid - 1;
} else if (((Comparable) target).compareTo(list[mid]) > 0) {
list[currentMaxIndex] = list[i];
list[i] = currentMax;
}
}
}
public static int search(Shape[] list, Shape target) {
int mid, low = 0, high = list.length - 1;
Scanner b2 = new Scanner(System.in);
int b = b2.nextInt();
Shape[] rectangle = new Shape[b];
System.out.println("总共创建了" + b + "个矩形\n请依次输入各个矩形的长和宽:");
Scanner c3 = new Scanner(System.in);
double e = e5.nextDouble();
Circle c1 = new Circle(e);
if (Shape.search(circle, c1) != 0) {
System.out.println("所查找的圆的位置及其具体信息为:\n" + Shape.search(circle, c1) + c1);
创建一个Java应用项目
把UML项目中的类自动生成代码到Java应用项目中
实现类中的方法及通用的排序和检索程序
进行编译
进行测试,使用的测试用例:
输入:预期输出:

五、调试过程
1.编译过程
记录算法实现中发现的语法错误及改正
2.调试过程
记录算法实现中发现的逻辑错误及改正,对每个测试用例,记录实际输出,并与预期输出进行比较,如果不同,分析产生错误的原因并改正。
return Math.PI * radius * radius;
}
public int compareTo(Object o) {
if (radius > ((Circle) o).radius) {
return 1;
} else if (radius < ((Circle) o).radius) {
福建农林大学计算机与信息学院实验报告

计算机
专业
年级
12级
成绩
姓名
学号
实验室
机号
实验时间
教师签字
实验(四)
一、实验目的和要求
1.掌握面向对象的继承关系
2.理解面向对象的多态
3.掌握方法的重写
4.掌握接口的实现
二、实验内容和原理
设计一个抽象类Shape,包括2个抽象方法,分别是计算形状的周长和面积。设计具体类Rectangle和Circle,分别表示矩形和圆,它们都是Shapes的子类。使Rectangle和Circle都实现Comparable接口(根据面积比较大小),编写一个通用的排序程序和一个通用的二分查找程序(自己写代码实现),能对这二种类型的数组进行排序并查找指定的对象。
三、实验环境
1.硬件环境:
2.软件环境:JDK1.5
四、算法描述及实验步骤
1.算法描述(可以用类图、流程图、伪代码或源程序描述)
package test;
import javax.swing.JOptionPane;
import java.util.Scanner;
public class Test {
for (int j = i - 1; j >= 0; j--) {
if (((Comparable) currentMax).compareTo(list[j]) < 0) {
currentMax = list[j];
currentMaxIndex = j;
}
}
if (currentMaxIndex != i) {
low = mid + 1;
} else {
return mid + 1;
}
}
return 0;
}
}
class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
}
}
}
class Circle extends Shape {
private double radius;
public Circle() {
this(0);
}
public Circle(double radius) {
setRadius(radius);
}
public double getRadius() {
return width;
}
public void setWidth(double val) {
this.width = (val <= 0 ? 1 : val);
}
@Override
public String toString() {
String str = "";
str += "矩形长:" + length + "宽:" + width;
return radius;
}
public void setRadius(double val) {
this.radius = (val < 0 ? 0 : val);
}
@Override
public String toString() {
String str = "";
str += "圆半径:" + radius + "周长:" + String.format("%-6.2f", getPerimeter());
return 2 * (length + wi Nhomakorabeath);
}
protected double getArea() {
return length * width;
}
public int compareTo(Object o) {
if (getArea() > ((Rectangle) o).getArea()) {
for (int i = 0; i < b; i++) {
rectangle[i] = new Rectangle(c3.nextDouble(), c3.nextDouble());
}
Shape.sort(circle);
String str1 = "";
for (int i = 0; i < circle.length; i++) {
public static void main(String[] args) {
System.out.println("要创建几个圆?:");
Scanner a1 = new Scanner(System.in);
相关主题