前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >计算各种图形的周长(接口与多态)( SDUT 3338 )

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

作者头像
Lokinli
发布2023-03-09 19:06:43
3020
发布2023-03-09 19:06:43
举报
文章被收录于专栏:以终为始以终为始

说明:仅用于复习备考/。 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();
	}
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-12-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档