计算各种图形的周长（接口与多态）

Problem Description

（1）三角形类Triangle （2）长方形类Rectangle （3）圆形类Circle等。

Input

Output

Sample Input
1
2 3
4 5 6
2
-2
-2 -3
Sample Output
6.28
10.00
15.00
12.56
0.00
0.00
Hint

import java.util.*;
interface Shape{
public double length();
}
class Triangle implements Shape{
double a, b, c;

public Triangle(double a, double b, double c) {
super();
if(a < 0 || b < 0 || c < 0) {
a = 0;
b = 0;
c = 0;
}
if(a + b > c && a + c > b && b + c > a) {//三角形必须符合成立条件
this.a = a;
this.b = b;
this.c = c;
}
}

public double length() {
return a+b+c;
}
}

class Rectangle implements Shape{
double l, w;
public Rectangle(double l, double w) {
if(l < 0 || w < 0) {
l = 0;
w = 0;
}
else {
this.l = l;
this.w = w;
}
}
public double length() {
return 2*(this.l + this.w);
}
}

class Circle implements Shape{
double r;
public Circle(double r) {
if(r < 0) {
r = 0;
}
this.r = r;
}
public double length() {
return 2*3.14*r;
}
}

class ShapeTest{//定义测试类ShapeTest，用Shape接口定义变量shape，用其指向不同类形的对象，输出各种图形的周长。
public double length(Shape shape) {
return shape.length();
}
}

public class Main {

public static void main(String[] args){
Scanner sc = new Scanner(System.in);

ShapeTest shapetest = new ShapeTest();

while(sc.hasNext()) {
Shape shape = null;
String str = sc.nextLine();
String[] array = str.split(" ");
if(array.length == 1) {//圆形
double r = Double.parseDouble(array[0]);
shape = new Circle(r);
}
else if(array.length == 2) {//长方形
double l = Double.parseDouble(array[0]);
double w = Double.parseDouble(array[1]);
shape = new Rectangle(l, w);
}
else if(array.length == 3) {//三角形
double a = Double.parseDouble(array[0]);
double b = Double.parseDouble(array[1]);
double c = Double.parseDouble(array[2]);
shape = new Triangle(a, b, c);
}

if(shape != null) {
double area = shapetest.length(shape);
System.out.printf("%.2f\n", area);
}
else {
System.out.println("0.00");
}
}
sc.close();
}
}

import java.util.*;
import java.math.BigInteger;
import java.text.*;

interface Shape{
public double length();
}
class Triangle implements Shape{
double a, b, c;

public Triangle(double a, double b, double c) {
if(a < 0 || b < 0 || c < 0) {
this.a = 0;
this.b = 0;
this.c = 0;
}
if(a + b > c && a + c > b && b + c > a) {
this.a = a;
this.b = b;
this.c = c;
}
}

@Override
public double length() {
return a+b+c;
}

}
class Rectangle implements Shape{

double length, width;

public Rectangle(double length, double width) {
if(length < 0 || width < 0) {
this.length = 0;
this.width = 0;
}
else {
this.length = length;
this.width = width;
}
}

@Override
public double length() {

return 2*(length + width);
}

}
class Circle implements Shape{
double r;

public Circle(double r) {
if(r < 0) {
this.r = 0;
}
else
this.r = r;
}

@Override
public double length() {
return 2*r*3.14;
}
}

class ShapeTest{
public double length(Shape shape) {
return shape.length();
}
}

public class Main {

public static void main(String[] args) throws ParseException {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
Shape shape = null;
String s = sc.nextLine();
String[] arr = s.split(" ");
if(arr.length == 1) {
int a = Integer.parseInt(arr[0]);
shape = new Circle(a);
}
else if(arr.length == 2) {
int a = Integer.parseInt(arr[0]);
int b = Integer.parseInt(arr[1]);
shape = new Rectangle(a, b);
}
else if(arr.length == 3){
int a = Integer.parseInt(arr[0]);
int b = Integer.parseInt(arr[1]);
int c = Integer.parseInt(arr[2]);
shape = new Triangle(a, b, c);
}
System.out.println(String.format("%.2f", shape.length()));
}
sc.close();
}

}