2020-11-24

该博客讨论了如何在Java中实现类的继承和多态性,通过创建Circle、Rectangle和Triangle类来计算不同图形的面积。文章提供了输入输出示例,并检查图形的属性值是否合法。输入包括三种图形的数量及其各自的属性,输出包括原始和排序后的面积列表以及总面积。当输入格式错误或图形属性值不合法时,程序将输出'WrongFormat'。
摘要由CSDN通过智能技术生成

7-1 图形继承与多态 (40分)

掌握类的继承、多态性及其使用方法。具体需求参见作业指导书。https://images.ptausercontent.com/8ffeae36-61db-4b07-891c-166a2356222a.pdf

输入格式:

从键盘首先输入三个整型值(例如a b c),分别代表想要创建的Circle、Rectangle及Triangle对象的数量,然后根据图形数量继续输入各对象的属性值(均为实型数),数与数之间可以用一个或多个空格或回车分隔。

输出格式:

  1. 如果图形数量非法(小于0)或图形属性值非法(数值小于0以及三角形三边关系),则输出Wrong Format
  2. 如果输入合法,则正常输出,输出内容如下(输出格式见输入输出示例):
  • 各个图形的面积;
  • 所有图形的面积总和;
  • 排序后的各个图形面积;
  • 再次所有图形的面积总和。

输入样例1:

在这里给出一组输入。例如:

1 1 1 2.3 3.2 3.2 6.5 3.2 4.2

输出样例1:

在这里给出相应的输出。例如:

Original area:
16.62 10.24 5.68 
Sum of area:32.54
Sorted area:
5.68 10.24 16.62 
Sum of area:32.54

输入样例2:

在这里给出一组输入。例如:

0 2 2 2.3 2.5 56.4 86.5 64.3 85.6 74.6544 3.2 6.1 4.5

输出样例2:

在这里给出相应的输出。例如:

Original area:
5.75 4878.60 2325.19 7.00 
Sum of area:7216.54
Sorted area:
5.75 7.00 2325.19 4878.60 
Sum of area:7216.54

输入样例3:

在这里给出一组输入。例如:

0 0 1 3 3 6

输出样例3:

在这里给出相应的输出。例如:

Wrong Format

代码如下:


import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
class Shape 
{
	public double getArea()//计算面积
	{
		return 0.0;
	}
}
class Circle extends  Shape
{
	private double r;
	public Circle(double r) 
	{
	       this.r = r;
	}
	public double getR() 
	{
	       return r;
	}
	public void setR(double r )
	{
	       this.r = r;
	}
	public double getArea()//圆面积
	{
		return Math.PI*r*r;
	}
	public  boolean checkR(double r)
	{
		if(r<=0)
		{
			return false;
		}
		else
			return true;
	}
}
class Rectangle extends  Shape
{
	private double w;
	private double l;
	public Rectangle()
	{
	}
	public Rectangle(double l,double w)
    {
        this.w=w;
        this.l=l;
    }
    public double getWidth() 
    {
        return w;
    }
    public void setWidth(double w)
    {
        this.w = w;
    }
    public double getLength() 
    {
        return l;
    }
    public void setLength(double l)
    {
        this.l = l;
    }
    public double getArea()
    {
        return l*w;
    }
    public  boolean checkWL(double w,double l)
	{
		if(w<0||l<0)
		{
			return false;
		}
		else
			return true;
	}
}
class Triangle extends  Shape
{
	private double x;
	private double y;
	private double z;
	public Triangle()
	{
	}
	public Triangle(double x,double y,double z)
    {
        this.x=x;
        this.y=y;
        this.z=z;
    }
    public double getArea()//三角形面积
	{
    	double p=(x+y+z)/2;
		return Math.sqrt(p*(p-x)*(p-y)*(p-z));
	}
    public  Boolean checkXYZ(double x,double y,double z)
	{
		if(x+y<=z||x+z<=y||y+z<=x||x<0&&y<0&&z<0)
		{
			return false;
		}
		else
			return true;
	}
}
public class Main
{
	public static boolean checkABC(double a,double b,double c)
	{
		if(a<0||b<0||c<0)
		{
			return false;
		}
		else
			return true;
	}
	public static void main(String[] args) 
	{// TODO Auto-generated method stub
		double []s=new double [100];
		Scanner in = new Scanner(System.in);
		ArrayList tm=new ArrayList();
		double sum = 0;
		int a = in.nextInt();
		int b = in.nextInt();
		int c = in.nextInt();
		double t = 0;
		if(checkABC( a, b,c)==false)
		{
			System.out.println("Wrong Format");
			System.exit(0);
		}
		else
		{
			for(int i=0;i<a;i++)//圆
			{
				double r = in.nextDouble();
				Circle circle = new Circle(r);
				if(circle.checkR(r)==false)
				{
					System.out.println("Wrong Format");
					System.exit(0);
				}
				else
				{
					tm.add(circle.getArea());
					t++;
				}
			} 
			
				
				for(int i=0;i<b;i++)//长方形
				{
					double w = in.nextDouble();
					double l = in.nextDouble();
					Rectangle rectangle = new Rectangle(w,l);
					if(rectangle.checkWL(w,l)==false)
					{
						System.out.println("Wrong Format");
						System.exit(0);
					}
					else 
					{
						tm.add(rectangle.getArea());
						t++;
					}
				}			
				
				for(int i=0;i<c;i++)//三角形
				{
					double x = in.nextDouble();
					double y = in.nextDouble();
					double z = in.nextDouble();
					Triangle triangle = new Triangle(x,y,z);
					if(triangle.checkXYZ(x,y,z)==false)
					{
						System.out.println("Wrong Format");
						System.exit(0);
					}
					else
					{
						tm.add(triangle.getArea());
						t++;
					}
				}
                System.out.printf("Original area:\n");
				for(int i=0;i<t;i++)
				{
					sum+=(double)tm.get(i);
					System.out.printf("%.2f ",tm.get(i));
				}
				System.out.printf("\n");
				System.out.printf("Sum of area:%.2f\n",sum);
				Collections.sort(tm);
				System.out.println("Sorted area:");
				for(int i=0;i<t;i++)
				{
					System.out.printf("%.2f ",tm.get(i));
				}
				System.out.printf("\n");
				
				System.out.printf("Sum of area:%.2f\n",sum);
				
			}
		}
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值