多态JAVA习题

计算一组几何体面积

【问题描述】

输入若干个几何体参数,按面积排序输出;

【输入形式】

第一行是几何形体数目n,下有n行,每行以一个字母c开头。

若c是‘R’,则是矩形,后跟2个整数(长宽);

若c是‘C’,代表圆,后跟一个数,代表半径;

若c是‘T’,则代表一个三角形,后跟三个整数,代表三边;

【输出形式】

按面积从小到大输出每个几何体的种类和面积。每行一个几何形体,
输出格式为: “几何体名称:面积”(面积保留两位小数)

【样例输入】

3

R 3 5

C 9

T 3 4 5

【样例输出】

Triangle:6.00

Rectangle:15.00

Circle:254.47

【样例说明】
【评分标准】编程体现多态,要求:

(1)将各种几何体泛化成形状类Shape,类中有统一的计算面积的抽象接口double getArea();在Shape的子类三角形类Triangle,矩形类Rectangle,
    圆类Circle中给出计算面积的具体实现;重写各子类的String toString()方法,让其返回“几何体名称:面积”字符串信息。

(2)定义一个形状数组存放一组几何形体,最后按面积从小到输出。

(3)面积保留两位小数,提示用静态方法String.format("%.2f",表达式) 对计算结果格式化返回一个字符串,具体如何使用,请查阅相关文档

(4)已知三角形的三条边a,b,c,可以用海伦公式计算三角形的面积
//Main类
import java.util.Scanner;
public class Main{

    public static void main(String[] args) {

	Scanner input = new Scanner(System.in);
	int n = input.nextInt();
	Shape[] area = new Shape[n]; //抽象类不能创建实例,即不可以使用new()来创建抽象对象。
	//而可以创建以此抽象类为元素的数组
	for(int i = 0; i < n; i++){

		  String str = input.next(); //不接收空格
		  int a, b, c;
		  double f;
		  
		  if(str.charAt(0) == 'R'){
			   a = input.nextInt();
			   b = input.nextInt();
			   area[i] = new Rectangle(a,b);
		  }
		  else if(str.charAt(0) == 'T'){
			   a = input.nextInt();
			   b = input.nextInt();
			   c = input.nextInt();
			   area[i] = new Triangle(a,b,c);
		  }
		  else{
			   f = input.nextDouble();
			   area[i] = new Circle(f);	
		  }
	}
	
	
	for(int i = 0; i < n - 1; i++){
		for(int j = i + 1; j <= n - 1; j++){
			if(area[i].getArea() > area[j].getArea()){
				Shape t = area[i];
				area[i] = area[j];
				area[j] = t;
			}
		}
    }
    
	for(int i = 0; i < n; i++)
		System.out.println(area[i].toString());
	input.close();
  }
}
//接口Area
interface Area{
	double getArea();
}
//抽象类Shape
public abstract class Shape implements Area
{
	public abstract String toString();
}
//Circle类
import java.text.*;
public class Circle extends  Shape
{
	DecimalFormat df = new DecimalFormat("#.00");
	double r;
	Circle (double r)
	{
		this.r = r;
	}
	public double getArea()
	{
		double a = Math.PI * r * r;
		return a;
	}
	public String toString()
	{
		String str = "Circle:" + df.format(getArea());
		return str;
	}
}
//Rectangle类
import java.text.*;
public class Rectangle extends Shape
{
	DecimalFormat df = new DecimalFormat("#.00");
	int a, b;
	Rectangle(int a, int b)
	{
		this.a = a;
		this.b = b;
	}
	public double getArea()
	{
		double s = a * b;
		return s;
	}
	public String toString()
	{
		String str = "Rectangle:" + df.format(getArea());
		return str;
	}
}
//Triangle类
import java.lang.Math;
import java.text.*;
public class Triangle extends Shape
{
	DecimalFormat df = new DecimalFormat("#.00");
	int a, b, c;
	Triangle(int a, int b, int c)
	{
		this.a = a;
		this.b = b;
		this.c = c;
	}
	public double getArea()
	{
		double s = ( a + b + c ) / 2.0;
		return Math.sqrt(s * (s - a) * (s - b) * (s - c));
	}
	public String toString()
	{
		String str = "Triangle:"+df.format(getArea());
		return str;
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值