掌握类的继承、多态性使用方法以及接口的应用。 具体需求参考作业指导书。 2020-OO第07次作业-2指导书V1.0.pdf
输入格式:
在一行上输入一串数字(1~4,整数),其中,1代表圆形卡片,2代表矩形卡片,3代表三角形卡片,4代表梯形卡片。各数字之间以一个或多个空格分隔,以“0”结束。例如:1 3 4 2 1 3 4 2 1 3 0
根据第一行数字所代表的卡片图形类型,依次输入各图形的相关参数,例如:圆形卡片需要输入圆的半径,矩形卡片需要输入矩形的宽和长,三角形卡片需要输入三角形的三条边长,梯形需要输入梯形的上底、下底以及高。各数据之间用一个或多个空格分隔。
输出格式:
如果图形数量非法(<=0)或图形属性值非法(数值<0以及三角形三边不能组成三角形),则输出Wrong Format。
如果输入合法,则正常输出,所有数值计算后均保留小数点后两位即可。输出内容如下:
排序前的各图形类型及面积,格式为[图形名称1:面积值1图形名称2:面积值2 …图形名称n:面积值n ],注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格,在结束符“]”之前;
输出分组后的图形类型及面积,格式为[圆形分组各图形类型及面积][矩形分组各图形类型及面积][三角形分组各图形类型及面积][梯形分组各图形类型及面积],各组内格式为图形名称:面积值。按照“Circle、Rectangle、Triangle、Trapezoid”的顺序依次输出;
各组内图形排序后的各图形类型及面积,格式同排序前各组图形的输出;
各组中面积之和的最大值输出,格式为The max area:面积值。
输入样例1:
在这里给出一组输入。例如:
1 5 3 2 0
输出样例1:
在这里给出相应的输出。例如:
Wrong Format
输入样例2:
在这里给出一组输入。例如:
4 2 1 3 0
3.2 2.5 0.4 2.3 1.4 5.6 2.3 4.2 3.5
输出样例2:
在这里给出相应的输出。例如:
The original list:
[Trapezoid:1.14 Rectangle:3.22 Circle:98.52 Triangle:4.02 ]
The Separated List:
[Circle:98.52 ][Rectangle:3.22 ][Triangle:4.02 ][Trapezoid:1.14 ]
The Separated sorted List:
[Circle:98.52 ][Rectangle:3.22 ][Triangle:4.02 ][Trapezoid:1.14 ]
The max area:98.52
输入样例3:
在这里给出一组输入。例如:
2 1 2 1 1 3 3 4 4 1 1 1 2 1 0
2.3 3.5 2.5 4.5 2.1 2.6 8.5 3.2 3.1 3.6 8.5 7.5 9.1245 6.5 3.4 10.2 11.2 11.6 15.4 5.8 2.13 6.2011 2.5 6.4 18.65
输出样例3:
在这里给出相应的输出。例如:
The original list:
[Rectangle:8.05 Circle:19.63 Rectangle:9.45 Circle:21.24 Circle:226.98 Triangle:4.65 Triangle:29.80 Trapezoid:50.49 Trapezoid:175.56 Circle:105.68 Circle:14.25 Circle:120.81 Rectangle:16.00 Circle:1092.72 ]
The Separated List:
[Circle:19.63 Circle:21.24 Circle:226.98 Circle:105.68 Circle:14.25 Circle:120.81 Circle:1092.72 ][Rectangle:8.05 Rectangle:9.45 Rectangle:16.00 ][Triangle:4.65 Triangle:29.80 ][Trapezoid:50.49 Trapezoid:175.56 ]
The Separated sorted List:
[Circle:1092.72 Circle:226.98 Circle:120.81 Circle:105.68 Circle:21.24 Circle:19.63 Circle:14.25 ][Rectangle:16.00 Rectangle:9.45 Rectangle:8.05 ][Triangle:29.80 Triangle:4.65 ][Trapezoid:175.56 Trapezoid:50.49 ]
The max area:1601.31
输入样例4:
在这里给出一组输入。例如:
1 1 3 0
6.5 12.54 3.6 5.3 6.4
输出样例4:
在这里给出相应的输出。例如:
The original list:
[Circle:132.73 Circle:494.02 Triangle:9.54 ]
The Separated List:
[Circle:132.73 Circle:494.02 ][][Triangle:9.54 ][]
The Separated sorted List:
[Circle:494.02 Circle:132.73 ][][Triangle:9.54 ][]
The max area:626.75
import java.util.Scanner;
class Circle {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public boolean validate() {
if(radius <= 0)
return false;
else return true;
}
public double getArea() {
return Math.PI*radius*radius;
}
}
class Rectangle{
private double width;
private double length;
public Rectangle(double width,double length) {
this.length = length;
this.width = width;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public boolean validate() {
if(width <0||length<0)
return false;
else
return true;
}
public double getArea() {
return width*length;
}
}
class Triangle {
private double side1;
private double side2;
private double side3;
public Triangle(double side1, double side2, double side3) {
super();
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double getSide1() {
return side1;
}
public void setSide1(double side1) {
this.side1 = side1;
}
public double getSide2() {
return side2;
}
public void setSide2(double side2) {
this.side2 = side2;
}
public double getSide3() {
return side3;
}
public void setSide3(double side3) {
this.side3 = side3;
}
public boolean validate() {
if(side1 <0||side2<0||side3<0||(side1+side2-side3)<=0||(side1+side3-side2)<=0||(side3+side2-side1)<=0)
return false;
else return true;
}
public double getArea() {
return Math.sqrt((side1+side2+side3)*(side1+side2-side3)*(side1+side3-side2)*(side2+side3-side1))/4;
}
}
class Trapezoid{
private double topSide;
private double bottomSide;
private double height;
public Trapezoid(double topSide,double bottomSide,double height) {
this.bottomSide = bottomSide;
this.height = height;
this.topSide = topSide;
}
public double getTopSide() {
return topSide;
}
public void setTopSide(double topSide) {
this.topSide = topSide;
}
public double getBottomSide() {
return bottomSide;
}
public void setBottomSide(double bottomSide) {
this.bottomSide = bottomSide;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public boolean validate() {
if(topSide<0||bottomSide<0||height<0) {
return false;
}
else return true;
}
public double getArea() {
return (topSide+bottomSide)*height/2;
}
}
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int []num = new int[1000];
int flag = 1;
int i = 0;
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int A = 0;
int B = 0;
int C = 0;
int D = 0;
int shuzi = input.nextInt();
if(shuzi == 0) {
System.out.println("Wrong Format");
}else {
while(shuzi!=0) {
if(shuzi<0||shuzi>4) {
flag = 2;
}
if(shuzi == 1) {
a++;
}
if(shuzi == 2) {
b++;
}
if(shuzi == 3) {
c++;
}
if(shuzi == 4) {
d++;
}
num[i]=shuzi;
shuzi = input.nextInt();
i++;
}
//System.out.print("\n");
if(flag == 2) {
System.out.print("Wrong Format");
}
else {
int []zhonglei = new int[i];
Circle[] circle = new Circle[i];
Rectangle[] rectangle = new Rectangle[i];
Triangle [] triangle = new Triangle[i];
Trapezoid[] trapezoid = new Trapezoid[i];
String zifu[] = new String[i];
Double []sum = new Double[i];
Double []sum1 = new Double[a];
Double []sum2 = new Double[b];
Double []sum3 = new Double[c];
Double []sum4 = new Double[d];
double s1 = 0;
double s2 = 0;
double s3 = 0;
double s4 = 0;
double max = 0;
Double []maxsum = new Double[4];
for(int j = 0;j<i;j++) {
if(num[j]==1) {
zhonglei[j]=1;
circle[j] = new Circle(input.nextDouble());
if(!circle[j].validate()) {
flag = 0;
}
}
if(num[j]==2) {
zhonglei[j]=2;
rectangle[j] = new Rectangle(input.nextDouble(),input.nextDouble());
if(!rectangle[j].validate()) {
flag =0 ;
}
}
if(num[j]==3) {
zhonglei[j]=3;
triangle[j] = new Triangle(input.nextDouble(),input.nextDouble(),input.nextDouble());
if(!triangle[j].validate()) {
flag = 0;
}
}
if(num[j]==4) {
zhonglei[j]=4;
trapezoid[j] = new Trapezoid(input.nextDouble(),input.nextDouble(),input.nextDouble());
if(!trapezoid[j].validate()) {
flag = 0;
}
}
}
if(flag==0) {
System.out.print("Wrong Format");
}
else {
for(int j = 0;j < i;j ++) {
if(zhonglei[j]==1) {
sum[j]=circle[j].getArea();
zifu[j]="Circle:";
sum1[A] = sum[j];
A++;
}
if(zhonglei[j]==2) {
sum[j]=rectangle[j].getArea();
zifu[j]="Rectangle:";
sum2[B] = sum[j];
B++;
}
if(zhonglei[j]==3) {
sum[j]=triangle[j].getArea();
zifu[j]="Triangle:";
sum3[C] = sum[j];
C++;
}
if(zhonglei[j]==4) {
sum[j]=trapezoid[j].getArea();
zifu[j]="Trapezoid:";
sum4[D] = sum[j];
D++;
}
if(sum[j]>max) {
max =sum[j];
}
}
System.out.println("The original list:");
System.out.print("[");
for(int j = 0;j < i;j++) {
System.out.print(zifu[j]+String.format("%.2f",sum[j])+" ");
}
System.out.print("]");
System.out.println("\nThe Separated List:");
System.out.print("[");
for(int j = 0;j<A;j++) {
System.out.print("Circle:"+String.format("%.2f",sum1[j])+" ");
}
System.out.print("]");
System.out.print("[");
for(int j = 0;j<B;j++) {
System.out.print("Rectangle:"+String.format("%.2f",sum2[j])+" ");
}
System.out.print("]");
System.out.print("[");
for(int j = 0;j<C;j++) {
System.out.print("Triangle:"+String.format("%.2f",sum3[j])+" ");
}
System.out.print("]");
System.out.print("[");
for(int j = 0;j<D;j++) {
System.out.print("Trapezoid:"+String.format("%.2f",sum4[j])+" ");
}
System.out.print("]");
for(int j=0;j<A-1;j++)
{
for(int k=j+1;k<A;k++)
if(sum1[k]>sum1[j])
{
double t =sum1[k];
sum1[k]=sum1[j];
sum1[j]=t;
}
}
for(int j=0;j<B-1;j++)
{
for(int k=j+1;k<B;k++)
if(sum2[k]>sum2[j])
{
double t =sum2[k];
sum2[k]=sum2[j];
sum2[j]=t;
}
}
for(int j=0;j<C-1;j++)
{
for(int k=j+1;k<C;k++)
if(sum3[k]>sum3[j])
{
double t =sum3[k];
sum3[k]=sum3[j];
sum3[j]=t;
}
}
for(int j=0;j<D-1;j++)
{
for(int k=j+1;k<D;k++)
if(sum4[k]>sum4[j])
{
double t =sum4[k];
sum4[k]=sum4[j];
sum4[j]=t;
}
}
System.out.println("\nThe Separated sorted List:");
System.out.print("[");
for(int j = 0;j<A;j++) {
System.out.print("Circle:"+String.format("%.2f",sum1[j])+" ");
s1 += sum1[j];
}
maxsum[0] = s1;
System.out.print("]");
System.out.print("[");
for(int j = 0;j<B;j++) {
System.out.print("Rectangle:"+String.format("%.2f",sum2[j])+" ");
s2 += sum2[j];
}
maxsum[1] = s2;
System.out.print("]");
System.out.print("[");
for(int j = 0;j<C;j++) {
System.out.print("Triangle:"+String.format("%.2f",sum3[j])+" ");
s3 += sum3[j];
}
maxsum[2] = s3;
System.out.print("]");
System.out.print("[");
for(int j = 0;j<D;j++) {
System.out.print("Trapezoid:"+String.format("%.2f",sum4[j])+" ");
s4 += sum4[j];
}
maxsum[3] = s4;
System.out.print("]");
for(int j =0 ;j<4;j++) {
if(max<maxsum[j]) {
max = maxsum[j];
}
}
System.out.println("\nThe max area:"+String.format("%.2f",max));
}
}
}
}
}