图形卡片排序游戏 (40分)
掌握类的继承、多态性使用方法以及接口的应用。详见作业指导书 https://images.ptausercontent.com/f52db597-70a8-4cf0-8e8c-5caf8f081cfd.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
,注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格; - 排序后的各图形类型及面积,格式同排序前的输出;
- 所有图形的面积总和,格式为
Sum of 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 sorted list:
Circle:98.52 Triangle:4.02 Rectangle:3.22 Trapezoid:1.14
Sum of area:106.91
输入样例3:
在这里给出一组输入。例如:
4 2 1 3 0
3.2 2.5 0.4 2.3 1.4 5.6 2.3 4.2 8.4
输出样例3:
在这里给出相应的输出。例如:
Wrong Format
代码如下:(造福人类,有用就关注我哦!嘻嘻嘻!)
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
class Xingzhuang
{
private String kapianName;
Xingzhuang()
{
}
Xingzhuang(String kapianName)
{
this.kapianName=kapianName;
}
public String getKapianName()
{
return kapianName;
}
public void setKapianName(String KapianName)
{
this.kapianName=KapianName;
}
public double getArea()
{
return 0.0;
}
public boolean check()
{
return true;
}
}
class Circle extends Xingzhuang
{
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 check(double r)
{
if(r<=0)
{
return false;
}
else
return true;
}
}
class Rectangle extends Xingzhuang
{
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 check(double w,double l)
{
if(w<0||l<0)
{
return false;
}
else
return true;
}
}
class Triangle extends Xingzhuang
{
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 check(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;
}
}
class Trapezoid extends Xingzhuang
{
private double x;
private double y;
private double z;
public Trapezoid()
{
}
public Trapezoid(double x,double y,double z)
{
this.x=x;
this.y=y;
this.z=z;
}
public double getArea()
{
return (x+y)*z/2;
}
public Boolean check(double x,double y,double z)
{
if(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 Scanner input = new Scanner(System.in);
public static void main(String[] args)
{// TODO Auto-generated method stub
//Scanner in = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<Integer>();
int num = input.nextInt();
while(num != 0)
{
if(num < 0 || num > 4)
{
System.out.println("Wrong Format");
System.exit(0);
}
list.add(num);
num = input.nextInt();
}
Kapianlist kapianlist = new Kapianlist(list);
if(!kapianlist.check())
{
System.out.println("Wrong Format");
System.exit(0);
}
kapianlist.showResult();
input.close();
}
}
class Kapian
{
Xingzhuang xingzhuang;
Kapian(Xingzhuang xingzhuang)
{
this.xingzhuang=xingzhuang;
}
public Xingzhuang getxingzhuang()
{
return xingzhuang;
}
public void setxingzhuang(Xingzhuang xingzhuang)
{
this.xingzhuang=xingzhuang;
}
}
class Kapianlist//卡片属性
{
ArrayList<Kapian> kapianList=new ArrayList<Kapian>();
Kapianlist (ArrayList<Integer> list)
{
for(int i=0;i<list.size();i++)
{
if(list.get(i)==1)
{
double r=Main.input.nextDouble();
Circle circle=new Circle(r);
Kapian kapian=new Kapian(circle);
kapian.getxingzhuang().setKapianName("Circle");
kapianList.add(kapian);
}
if(list.get(i)==2)
{
double w=Main.input.nextDouble();
double l=Main.input.nextDouble();
Rectangle rectangle=new Rectangle(w,l);
Kapian kapian=new Kapian(rectangle);
kapian.getxingzhuang().setKapianName("rectangle");
kapianList.add(kapian);
}
if(list.get(i)==3)
{
double x=Main.input.nextDouble();
double y=Main.input.nextDouble();
double z=Main.input.nextDouble();
Triangle triangle=new Triangle(x,y,z);
Kapian kapian=new Kapian(triangle);
kapian.getxingzhuang().setKapianName("triangle");
kapianList.add(kapian);
}
if(list.get(i)==4)
{
double x=Main.input.nextDouble();
double y=Main.input.nextDouble();
double z=Main.input.nextDouble();
Trapezoid traperoid=new Trapezoid(x,y,z);
Kapian kapian=new Kapian(traperoid);
kapian.getxingzhuang().setKapianName("Trapezoid");
kapianList.add(kapian);
}
}
}
public boolean check()
{
for(int i=0;i<kapianList.size();i++)
{
if(!kapianList.get(i).getxingzhuang().check())
{
return false;
}
}
return true;
}
public void KapianSort()
{
for(int k=0;k<kapianList.size();k++)
{
for(int i=k+1;i<kapianList.size();i++)
{
if(kapianList.get(k).getxingzhuang().getArea()<kapianList.get(i).getxingzhuang().getArea())
Collections.swap(kapianList, k, i);
}
}
}
public double getAllArea()
{
double s=0;
for(int i=0;i<kapianList.size();i++)
s=s+kapianList.get(i).getxingzhuang().getArea();
return s;
}
public void showResult()
{
System.out.printf("The original list:\n");
for(int i=0;i<kapianList.size();i++)
{
System.out.print(kapianList.get(i).getxingzhuang().getKapianName()+":");
System.out.printf("%.2f"+" ",kapianList.get(i).getxingzhuang().getArea());
}
System.out.printf("\n");
System.out.printf("The sorted list:\n");
KapianSort();
for(int i=0;i<kapianList.size();i++)
{
System.out.print(kapianList.get(i).getxingzhuang().getKapianName()+":");
System.out.printf("%.2f"+" ",kapianList.get(i).getxingzhuang().getArea());
}
System.out.printf("\n");
System.out.printf("Sum of area:%.2f\n",getAllArea());
}
}