设计图形接口GeoGraphic,实现GeoGraphic[]数组的读写

题目:
1、设计图形接口GeoGraphic,实现Comparable接口,由接口派生类:Circle、rectangle、Cylinder(圆柱体:getArea()方法求表面积),派生类实现getArea()求面积,以getArea()为排序依据。要求实现GeoGraphic[]数组的读写,数组中含有派生类产生的对象各2个。
对象读写定义如下:
A、对象写入文件的方法,方法头定义如下:
public static void WriteShapeObject(File file,Shape[] a) throws FileNotFoundException;
B、对象文件读出并显示的方法,方法头定义如下:
public static void ReadShapeObject(File file) throws ClassNotFoundException;
其它任何需要的内容自行设计



读题:
造一个GeoGraphic,可以是抽象类也可以是接口(两种方法,能用就行),创建一个GeoGraphic[]数组对象,控制台输入或者直接赋值。对它的读写,我的理解是将它的面积写到文件中,再从文件中读到控制台(虽然并没有实现那两个读写方法)。


java.util.List.sort()按照 Comparator中方法排序。如果实现了compareTo(),不需写第二个参数。若没写而只有1参数,默认。另可自己写个排序方法作第二参数
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.*;


public class Main {

	public static void main(String[] args) throws Exception{
		ArrayList<GeoGraphic> a = new ArrayList<>();
	File f = new File("javatest.txt");
	
try(	Scanner s = new Scanner(f);){//从文件读入数据
	while(s.hasNext()) {
		double c1 = s.nextDouble();
		double c2 = s.nextDouble();
		double w1 = s.nextDouble();
		double h1 = s.nextDouble();
		double w2 = s.nextDouble();
		double h2 = s.nextDouble();
		double hr1 = s.nextDouble();
		double r1 = s.nextDouble();
		double hr2 = s.nextDouble();
		double r2 = s.nextDouble();
		a.add(new Circle(c1));
		a.add(new Circle(c2));
		a.add(new Rectangle(h1,w1));
		a.add(new Rectangle(h2,w2));
		a.add(new Cylinder(hr1,r1));
		a.add(new Cylinder(hr2,r2));
	}
}	
for(int i=0;i< a.size();i++) {//排序前
	System.out.println(a.get(i).getArea());
}

Collections.sort(a);

System.out.println(" ");//排序后
for(int i=0;i< a.size();i++) {
	System.out.println(a.get(i).getArea());
}

File outFile = new File("javaOutput.txt");
	try(PrintWriter output = new PrintWriter(outFile);){//写数据
for(int i = 0;i<a.size();i++) {
	output.println(a.get(i).getArea() + " ");
}
	}
	
//	public static void WiteShapeObject(File file,GeoGraphic a) throws FileNotFoundException {
//		
//	}
//	
//	public static void ReadShapeObject(File file) throws ClassNotFoundException{
//		
//	}
 }
}


也可以是abstract GeoGraphic,这样可以直接在GeoGraphic里面写完compareTo()方法,子类里就不用了,更方便


public interface GeoGraphic extends Comparable<GeoGraphic>{
	public double getArea();
	//public int compareTo(GeoGraphic o);
}

public class Circle implements GeoGraphic{
	private double radius;
	
	Circle(double r){
		radius=r;
	}
	
	public double getArea() {
		return radius*radius*Math.PI;
	}
	
	public int compareTo(GeoGraphic o) {
		if(this.getArea()>o.getArea())return 1;
		else if(this.getArea()==o.getArea())return 0;
		else return -1;
	}
}

public class Rectangle implements GeoGraphic{
	private double height;
	private double width;
	Rectangle(double h,double w){
		height = h;
		width = w;
	}
	
	public double getArea() {
		return height*width;
	}
	
	public int compareTo(GeoGraphic o) {
		if(this.getArea()>o.getArea())return 1;
		else if(this.getArea()==o.getArea())return 0;
		else return -1;
	}
}


public class Cylinder implements GeoGraphic{
	private double height;
	private double radius;
	Cylinder(double h,double r){
		height = h;
		radius = r;
	}
	
	public double getArea() {
		return height*2*Math.PI*radius+2*radius*radius*Math.PI;
	}
	
	public int compareTo(GeoGraphic o) {
		if(this.getArea()>o.getArea())return 1;
		else if(this.getArea()==o.getArea())return 0;
		else return -1;
	}
}

在这里插入图片描述

勉强能用。

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值