掌握类的继承、多态性及其使用方法。具体需求参见作业指导书。
2021-OO第06次作业-5指导书V1.0.pdf
输入格式:
从键盘首先输入三个整型值(例如a b c),分别代表想要创建的Circle、Rectangle及Triangle对象的数量,然后根据图形数量继续输入各对象的属性值(均为实型数),数与数之间可以用一个或多个空格或回车分隔。
输出格式:
1.如果图形数量非法(小于0)或图形属性值非法(数值小于0以及三角形三边关系),则输出Wrong Format。
2.如果输入合法,则正常输出,输出内容如下(输出格式见输入输出示例):
- 各个图形的面积;
- 所有图形的面积总和;
- 排序后的各个图形面积;
- 再次所有图形的面积总和。
输入样例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.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<Shape> w=new ArrayList<>();
int a,b,c,i=0,j=0,judge=1;
double sum=0;
a=input.nextInt();
b=input.nextInt();
c=input.nextInt();
for (i = 0; i < a; i++) {
double radius = input.nextDouble();
w.add(new Circle(radius));
if(w.get(i).validate())
{
}
else
judge=0;
}
for (i = 0; i < b; i++) {
double width = input.nextDouble();
double length = input.nextDouble();
w.add(new Rectangle(width, length));
}
for (i = 0; i < c; i++) {
double side1 = input.nextDouble();
double side2 = input.nextDouble();
double side3 = input.nextDouble();
w.add(new Triangle(side1, side2, side3));
}
for (i = 0; i < a + b + c; i++) {
if (w.get(i).validate())
{
}
else
{
judge = 0;
}
}
if(a==0&&b==0&&c==0)
{
judge=1;
}
if (judge == 1 && (a >= 0 && b >= 0 && c >= 0)) {
System.out.println("Original area:");
for (i = 0; i < a; i++) {
System.out.print(String.format("%.2f", w.get(i).getArea()) + " ");
}
for (i = a; i < a + b; i++) {
System.out.print(String.format("%.2f", w.get(i).getArea()) + " ");
}
for (i = a + b; i < a + b + c; i++) {
System.out.print(String.format("%.2f", w.get(i).getArea()) + " ");
}
System.out.println();
for (i = 0; i < a + b + c; i++) {
sum += w.get(i).getArea();
}
System.out.println("Sum of area:" + String.format("%.2f", sum));
sort(w);
System.out.println("Sorted area:");
for (i = 0; i < a + b + c; i++) {
System.out.print(String.format("%.2f", w.get(i).getArea()) + " ");
}
System.out.println();
sum=0;
for (i = 0; i < a + b + c; i++) {
sum += w.get(i).getArea();
}
System.out.println("Sum of area:" + String.format("%.2f", sum));
} else {
System.out.println("Wrong Format");
}
}
public static ArrayList<Shape> sort(ArrayList<Shape> w){
int i=0,j=0;
double a=0,b=0,t=0;
Shape k;
for (i = 0; i < w.size(); i++) {
for (j = 0; j < w.size() - i - 1; j++) {
if (w.get(j).getArea() >= w.get(j + 1).getArea()) {
k = w.get(j);
w.set(j, w.get(j + 1));
w.set(j + 1, k);
}
}
}
return w;
}
}
class Circle extends Shape{
@Override
public double getArea(){
double s;
s=Math.PI*radius*radius;
return s;
}
Shape shape;
Circle(){
}
double radius;
Circle(double radius)
{
this.radius=radius;
}
@Override
public boolean validate(){
if(radius>0)
return true;
else
return false;
}
}
abstract class Shape{
Shape(){
}
abstract public double getArea();
abstract public boolean validate();
}
class Triangle extends Shape{
Shape shape;
Triangle(){
}
double side1;
double side2;
double side3;
@Override
public boolean validate()
{
if(side1+side2<=side3||side1+side3<=side2||side2+side3<=side1)
{
return false;
}
if(side1-side2>=side3||side1-side3>=side2||side2-side3>=side1)
{
return false;
}
return true;
}
Triangle(double side1,double side2,double side3)
{
this.side1=side1;
this.side2=side2;
this.side3=side3;
}
@Override
public double getArea(){
double p=(side1+side2+side3)/2;
double s=Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));
return s;
}
}
class Rectangle extends Shape{
Shape shape;
Rectangle(){
}
double width;
double length;
Rectangle(double width,double length)
{
this.width=width;
this.length=length;
}
@Override
public boolean validate(){
if(width>0&&length>0)
return true;
else
return false;
}
@Override
public double getArea(){
double s;
s=width*length;
return s;
}
}