*
* @author Administrator */
import java.text.*; public class Main {
public static void main(String[] args){
/*Shape[] shape1={new Circle(2.0),new Circle(3.0),new Circle(4.0), new Circle(5.0),new Circle(6.0)};
Shape[] shape2={new Rectangle(1.0,2.0),new Rectangle(2.0,3.0),new Rectangle(3.0,4.0),
new Rectangle(4.0,5.0),new Rectangle(5.0,6.0)}; Shape shape3=new Circle(6.0);
Shape shape4=new Rectangle(10.0,10.0);*/
Shape[] shape1={new Circle(1.1),new Circle(1.2),new Circle(1.3), new Circle(1.4),new Circle(1.5)};
Shape[] shape2={new Rectangle(1.0,4.0),new Rectangle(7.0,3.0),new Rectangle(3.0,5.0),
new Rectangle(9.0,5.0),new Rectangle(5.0,4.0)}; Shape shape3=new Circle(2.0);
Shape shape4=new Rectangle(9.5,5.0); System.out.print(\【排序面积从小到大】:\\n\ sort(shape1); sort(shape2);
System.out.print(\【圆形数组】【长度】\ for(int i=0;i System.out.println(\【位置】\ } System.out.println(\【矩形数组】【长度】\ for(int i=0;i System.out.println(\【位置】\ } int index=BinarySearch(shape1,shape3); System.out.println(\【二分法查找】:\ if(index==0){ System.out.println(\【对象】\不存在【圆形】数组中。\ } else{ System.out.println(\【对象】\ \位于【圆形】数组的第【\】位。\ } index=BinarySearch(shape2,shape4); if(index==0){ System.out.println(\【对象】\ \不存在【矩形】数组中。\ } else{ System.out.println(\【对象】\ \位于【矩形】数组的第【\】位。\ } } public static void sort(Comparable[] list) { Comparable com; int n = list.length; for (int i = 0; i < n- 1; i++) { int m = i; for (int j = i + 1; j < list.length; j++) { if (list[j].compareTo(list[m]) == -1) { m = j; } } if (m != i) { com = list[i]; list[i] = list[m]; list[m] = com; } } } public static int BinarySearch(Comparable[] list,Comparable com){ int low =0; int high = list.length-1; int mid =0; while(low<=high){ mid=(low+high)/2; if(list[mid].compareTo(com)==0){ return mid; } else if(list[mid].compareTo(com)==1){ high=mid-1; } else{ low=mid+1; } } return 0; } } Shape类: package javaapplication1; /** * * @author Administrator */ import java.text.*; public abstract class Shape implements Comparable{ public Shape(){ } public abstract double getPerimeter(); public abstract double getArea(); public abstract String toString(); } Cricle类: package javaapplication1; /** * * @author Administrator */ import java.text.*; public class Circle extends Shape{ private double radius; public Circle(){ radius = 0.0; } public Circle(double radius){ this.radius=radius; } public double getRadius(){ return radius; } public void setRadius(double val){ this.radius=val; } public double getArea(){ return Math.PI*radius*radius; } public double getPerimeter(){ return 2.0*Math.PI*radius; } public String toString(){ String str; DecimalFormat d1=new DecimalFormat(\ str=\【圆形】【半径】:\【面积】\ d1.format(getArea())+\【周\ return str; } public int comparaTo(Object o){ if(this.getArea()>((Circle)o).getArea()){ return 1; } else if(this.getArea()==((Circle)o).getArea()){ return 0; } else{ return -1; } } public int compareTo(Object o) { if(this.getArea()>((Circle)o).getArea()) return 1; else if(this.getArea()==((Circle)o).getArea()) return 0; else return -1; } } Rectangle类: package javaapplication1; /** * * @author Administrator */ import java.text.*; public class Rectangle extends Shape{ 长】: private double length; private double width; public Rectangle(){ width=0.0; length=0.0; } Rectangle(double length, double width) { this.length=length; this.width=width; //throw new UnsupportedOperationException(\ } public double getLength(){ return length; } public void setLength(double val){ this.length=val; } public double getWidth(){ return width; } public void setWidth(double val){ this.width=val; } public double getArea(){ return length*width; } public double getPerimeter(){ return 2.0*length*width; } public String toString(){ String str; DecimalFormat d1=new DecimalFormat(\ str=\【矩形】【长度】:\【宽】:\【面积】:\ d1.format(getArea())+\【周长】:\ return str; } public int comparaTo(Object obj){ if(this.getArea()>((Circle)obj).getArea()){ return 1; } else if(this.getArea()==((Circle)obj).getArea()){ return 0; } else{ return -1; } } public int compareTo(Object o) { if(this.getArea()>((Rectangle)o).getArea()) return 1; else if(this.getArea()==((Rectangle)o).getArea()) return 0; else return -1; } } 百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库Java实验报告(四)继承和多态(2)在线全文阅读。
相关推荐: