7-1 图形继承与多态 (40分)
掌握类的继承、多态性及其使用方法。具体需求参见作业指导书。https://images.ptausercontent.com/8ffeae36-61db-4b07-891c-166a2356222a.pdf
输入格式:
从键盘首先输入三个整型值(例如a b c),分别代表想要创建的Circle、Rectangle及Triangle对象的数量,然后根据图形数量继续输入各对象的属性值(均为实型数),数与数之间可以用一个或多个空格或回车分隔。
输出格式:
- 如果图形数量非法(小于0)或图形属性值非法(数值小于0以及三角形三边关系),则输出
Wrong Format
。 - 如果输入合法,则正常输出,输出内容如下(输出格式见输入输出示例):
- 各个图形的面积;
- 所有图形的面积总和;
- 排序后的各个图形面积;
- 再次所有图形的面积总和。
输入样例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);
}
}
}