77范文网 - 专业文章范例文档资料分享平台

Java实验报告(四)继承和多态(2)

来源:网络收集 时间:2018-12-20 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

*

* @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)在线全文阅读。

Java实验报告(四)继承和多态(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/378328.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: