分数 40
作者 段喜龙
单位 南昌航空大学
掌握类的继承、多态性使用方法以及接口的应用。详见作业指导书
2020-OO第07次作业-1指导书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
,注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格; - 排序后的各图形类型及面积,格式同排序前的输出;
- 所有图形的面积总和,格式为
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
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
我滴源码:
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Collections;
import java.util.Comparator;
//给定的主函数
public class Main{
public static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
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();
}
DealCardList dealCardList = new DealCardList(list);
if(!dealCardList.validate()) {
System.out.println("Wrong Format");
System.exit(0);
}
dealCardList.showResult();
input.close();
}
}
class DealCardList {
ArrayList<Card> cardList = new ArrayList<Card>();
public DealCardList() {
super();
}
public DealCardList(ArrayList<Integer> list) {
for(int i:list) {
if(i==1) {
Card a = new Circle(Main.input.nextDouble());
cardList.add(a);
}
else if(i==2) {
Card a =new Rectangle(Main.input.nextDouble(),Main.input.nextDouble());
cardList.add(a);
}
else if(i==3) {
Card a =new Triangle(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble());
cardList.add(a);
}else if(i==4) {
Card a = new Trapezoid(Main.input.nextDouble(),Main.input.nextDouble(),Main.input.nextDouble());
cardList.add(a);
}
}
}
public boolean validate() {
for(Card i:cardList) {
if(!i.getShape().validate()) {
return false;
}
}
return true;
}
public void showResult() {
System.out.println("The original list:");
for(Card i:cardList) {
System.out.print(i+":"+String.format("%.2f",i.getShape().getArea())+" ");
}
Collections.sort(cardList,new Comparator<Card>() {
public int compare(Card a, Card b) {
return b.compareTo(a);
}
});
double sum = 0;
System.out.println("\nThe sorted list:");
for(Card i:cardList) {
sum += i.shape.getArea();
System.out.print(i+":"+String.format("%.2f",i.getShape().getArea())+" ");
}
System.out.println("\nSum of area:"+String.format("%.2f",sum));
}
}
abstract class Card {
Shape shape;
public Card(Shape shape) {
super();
this.shape = shape;
}
public Card() {
super();
}
public Shape getShape() {
return shape;
}
public void setShape(Shape shape) {
this.shape = shape;
}
public int compareTo(Card card) {
if(this.shape.getArea()>card.shape.getArea()) {
return 1;
}else if(this.shape.getArea()==card.shape.getArea()) {
return 0;
}else {
return -1;
}
}
}
abstract class Shape extends Card{
String name;
public Shape(String name) {
super();
this.name = name;
this.shape = this;
}
public Shape() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
abstract public double getArea();
abstract public boolean validate();
@Override
public String toString() {
return name;
}
}
class Circle extends Shape {
double radius;
public Circle(double radius) {
super("Circle");
this.radius = radius;
}
@Override
public double getArea() {
return radius*radius*Math.PI;
}
@Override
public boolean validate() {
if(radius<=0) {
return false;
}
return true;
}
}
class Rectangle extends Shape {
double width,length;
public Rectangle(double width, double length) {
super("Rectangle");
this.width = width;
this.length = length;
}
public Rectangle() {
super();
// TODO Auto-generated constructor stub
}
@Override
public double getArea() {
return width*length;
}
@Override
public boolean validate() {
if(width<=0||length<=0) {
return false;
}
return true;
}
}
class Trapezoid extends Shape {
double topSide,bottomSide,height;
public Trapezoid( double topSide, double bottomSide, double height) {
super("Trapezoid");
this.topSide = topSide;
this.bottomSide = bottomSide;
this.height = height;
}
@Override
public double getArea() {
return (bottomSide+topSide)*height/2;
}
@Override
public boolean validate() {
if(bottomSide<=0||height<=0||topSide<=0) {
return false;
}
return true;
}
}
class Triangle extends Shape {
double side1,side2,side3;
public Triangle(double side1, double side2, double side3) {
super("Triangle");
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public Triangle() {
super();
// TODO Auto-generated constructor stub
}
@Override
public double getArea() {
double p = (side1 +side2+side3)/2;
return Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));
}
@Override
public boolean validate() {
if(side1 <= 0||side2<=0||side3<=0) {
return false;
}
if(side1+side2<=side3||side1+side3<=side2||side2+side3<=side1) {
return false;
}
return true;
}
}
我滴总结:
段喜龙老师的题目总是贴切java设计课的核心,类设计(不想某ke,整写数学题来难我们 ̄△ ̄
根据指导书写题目,严格遵从类设计就行。
没什么难度的。
这道题主要考了点多态的使用。