计算各种图形的周长(接口与多态)( SDUT 3338 )

说明:仅用于复习备考/。 Problem Description 定义接口Shape,定义求周长的方法length()。 定义如下类实现接口Shape的抽象方法: (1)三角形类Triangle (2)长方形类Rectangle (3)圆形类Circle等。 定义测试类ShapeTest,用Shape接口定义变量shape,用其指向不同类形的对象,输出各种图形的周长。并为其他的Shape接口实现类提供良好的扩展性。

大体的样子:用的时候就是 先定义一个 ShapeTest shapetest = new ShapeTest ( ) ,然后,来实例化 Shape ,最后通过 ShapeTest 来输出。

代码语言:javascript
复制
interface Shape{
	abstract double length();
}
class Triangle implements Shape{
	double x,y,z;
	Triangle(double x, double y, double z){
		this.x = x;
		this.y = y;
		this.z = z;
	}
	public double length(){
		return x + y + z;
	}
}
class Rectangle implements Shape{
	double x,y;
	Rectangle(double x, double y){
		this.x = x;
		this.y = y;
	}
	public double length(){
		return 2 * x + 2 * y;
	}
}
class Circle implements Shape{
	double z;
	Circle(double z){
		this.z = z;
	}
	public double length(){
		return 2 * 3.14 * z;
	}
}
class ShapeTest{
	public double length(Shape shape){
		return shape.length();
	}
}

Input

输入多组数值型数据(double);

一行中若有1个数,表示圆的半径;

一行中若有2个数(中间用空格间隔),表示长方形的长度、宽度。

一行中若有3个数(中间用空格间隔),表示三角形的三边的长度。

若输入数据中有负数,则不表示任何图形,周长为0。

Output

行数与输入相对应,数值为根据每行输入数据求得的图形的周长(保留2位小数)。

Sample Input

代码语言:javascript
复制
1
2 3
4 5 6
2
-2
-2 -3

Sample Output

代码语言:javascript
复制
6.28
10.00
15.00
12.56
0.00
0.00

Hint

构造三角形时要判断给定的三边的长度是否能组成一个三角形,即符合两边之和大于第三边的规则;

计算圆周长时PI取3.14。

全部的代码:

代码语言:javascript
复制
package aa;

import java.util.Scanner;

public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
ShapeTest shapetest = new ShapeTest(); // 定义 shapetest 用于最后的输出询问
while(sc.hasNext()){
Shape shape = null;
String str = sc.nextLine(); // 读一行
String []s = str.split(" "); //分割
if(s.length == 1){
double r = Double.parseDouble(s[0]); // 强制类型转换
shape = new Circle(r); // shape 抽象类的实例化
}else if(s.length == 2){
double l = Double.parseDouble(s[0]);
double w = Double.parseDouble(s[1]);
shape = new Rectangle(l,w);
}else if(s.length == 3){
double x = Double.parseDouble(s[0]);
double y = Double.parseDouble(s[1]);
double z = Double.parseDouble(s[2]);
shape = new Triangle(x,y,z);
}
if(shape!=null){
double ans = shapetest.length(shape);
System.out.printf("%.2f\n",ans);
}
else{
System.out.println("0.00");
}
}

}

}
interface Shape{ // 定义接口
abstract double length();
}
class Triangle implements Shape{
double x,y,z;
Triangle(double x, double y, double z){ //构造方法
if(x < 0 || y < 0 || z < 0) {
this.x = 0;
this.y = 0;
this.z = 0;
}
if(x + y > z && x + z > y && y + z > x){
this.x = x;
this.y = y;
this.z = z;
}
}
public double length(){ // 实现接口中方法
return x + y + z;
}
}
class Rectangle implements Shape{
double x,y;
Rectangle(double x, double y){
if(x < 0 || y < 0){
this.x = 0;
this.y = 0;
}
else {
this.x = x;
this.y = y;
}
}
public double length(){
return 2 * x + 2 * y;
}
}
class Circle implements Shape{
double z;
Circle(double z){
if(z < 0){
this.z = z;
}
else{
this.z = z;
}
}
public double length(){
return 2 * 3.14 * z;
}
}
class ShapeTest{ // 定义的 ShapeTest 类,用来实现普遍性
public double length(Shape shape){ //传进来的 shape 判断是否实例化
return shape.length();
}
}