7-104 图形继承与多态分数 10
输入格式:
从键盘首先输入三个整型值(例如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.Scanner;
import java.util.Arrays;
//异常处理
class MyException extends Exception{
MyException(){
}
}
class Area{
double getArea(){
return 0;
}
}
class Circle7_104 extends Area{
private double radius = 0;
Circle7_104(double radius){
this.radius = radius;
}
double getArea() {
return Math.PI*radius*radius;
}
}
class Rectangle7_104 extends Area{
private double width = 0,height = 0;
Rectangle7_104(double width,double height){
this.width = width;
this.height = height;
}
double getArea() {
return width*height;
}
}
class Triangle7_104 extends Area{
private double a = 0,b = 0,c = 0;
Triangle7_104(double a,double b,double c){
this.a = a;
this.b = b;
this.c = c;
}
double getArea() {
double p = (a+b+c)/2;
return Math.sqrt(p*(p-a)*(p-b)*(p-c));
}
}
public class Pta_7_104 {
public static void main(String args[]) {
try {
Scanner sc = new Scanner(System.in);
int num1 = sc.nextInt();
int num2 = sc.nextInt();
int num3 = sc.nextInt();
int all = num1+num2+num3;
if((num1<0)||(num2<0)||(num3<0)) {
throw new MyException();
}
double[] cir_arr = new double[num1];
double[] rec_arr = new double[num2];
double[] tri_arr = new double[num3];
double[] all_arr = new double[all]; //创建一个新数组用来把这些面积放一起
//circle
for(int i = 0;i<num1;i++) {
double r = sc.nextDouble();;
Circle7_104 cir = new Circle7_104(r);
cir_arr[i] = cir.getArea();
}
//rectangle
for(int i = 0;i<num2;i++) {
double w = sc.nextDouble();
double h = sc.nextDouble();
Rectangle7_104 rec = new Rectangle7_104(w,h);
rec_arr[i] = rec.getArea();
}
//triangle
for(int i = 0;i<num3;i++) {
double A = sc.nextDouble();
double B = sc.nextDouble();
double C = sc.nextDouble();
if((A+B<=C)||(A+C<=B)||(B+C<=A)) {
throw new MyException();
}else {
Triangle7_104 tri = new Triangle7_104(A,B,C);
tri_arr[i] = tri.getArea();
}
}
//把创建好的数组放到一起
int j = 0;
for(double value : cir_arr) {
all_arr[j++] = value;
}
for(double value : rec_arr) {
all_arr[j++] = value;
}
for(double value : tri_arr) {
all_arr[j++] = value;
}
Arrays.sort(all_arr);
System.out.println("Original area:");
if(cir_arr.length > 0) {
for(int i = 0;i<num1;i++) {
System.out.printf("%.2f ",cir_arr[i]);
}
}
if(rec_arr.length > 0) {
for(int i = 0;i<num2;i++) {
System.out.printf("%.2f ",rec_arr[i]);
}
}
if(tri_arr.length > 0) {
for(int i = 0;i<num3;i++) {
System.out.printf("%.2f ",tri_arr[i]);
}
}
System.out.println();
System.out.printf("Sum of area:%.2f\n", Arrays.stream(all_arr).sum());
System.out.println("Sorted area:");
for (double value : all_arr) {
System.out.printf("%.2f ", value);
}
System.out.println();
System.out.printf("Sum of sorted area:%.2f\n", Arrays.stream(all_arr).sum());
}catch (MyException e) {
System.out.println("Wrong Format");
}
}
}