定义接口或类 Shape,定义求周长的方法length()。
定义如下类,实现接口Shape或父类Shape的方法。
(1)三角形类Triangle (2)长方形类Rectangle (3)圆形类Circle等。
定义测试类ShapeTest,用Shape接口(或类)定义变量shape,用其指向不同类形的对象,输出各种图形的周长。并为其他的Shape接口实现类提供良好的扩展性。
提示: 计算圆周长时PI取3.14。
输入格式:
输入多组数值型数据(double);
一行中若有1个数,表示圆的半径;
一行中若有2个数(中间用空格间隔),表示长方形的长度、宽度。
一行中若有3个数(中间用空格间隔),表示三角形的三边的长度。(需要判断三个边长是否能构成三角形)
若输入数据中有0或负数,则不表示任何图形,周长为0。
package com.sxt;
import java.util.Scanner;
public class ShapeTest {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Scanner sc=new Scanner(System.in);
Shape shape;
while(sc.hasNextDouble()) {
double a = sc.nextDouble();
if (a <= 0) {
System.out.println("0.00");
continue;
}
if (sc.hasNextDouble()) {//长方形
double b = sc.nextDouble();
if (b <= 0) {
System.out.println("0.00");
}
else {
shape = new Rectangle(a,b);
System.out.printf("%.2f",shape.length());
System.out.println();
}
} else if (sc.hasNextDouble()) {//三角形
double b = sc.nextDouble();
double c = sc.nextDouble();
shape = new Triangle(a,b,c);
System.out.printf("%.2f",shape.length());
System.out.println();
}
else {//圆形
shape = new Circle(a);
System.out.printf("%.2f",shape.length());
System.out.println();
}
}
sc.close();
}
}
interface Shape {
double length();
}
class Triangle implements Shape{//三角形
private double a,b,c;
public Triangle(double a, double b, double c) {
this.a=a;
this.b=b;
this.c=c;
}
public double length() {
if(a<=0||b<=0||c<=0) {
return 0;
}
else if(a+b<=c||a+c<=b||b+c<=a) {
return 0;
}
else {
return a+b+c;
}
}
}
class Rectangle implements Shape{//长方形
private double a,b;
public Rectangle(double a,double b){
this.a=a;
this.b=b;
}
public double length(){
if(a<=0||b<=0) {
return 0;
}
else {
return 2*(a+b);
}
}
}
class Circle implements Shape{//圆形
private double radius;
public Circle (double radius){
this.radius=radius;
}
public double length() {
if(radius<=0){
return 0;
}
else {
return 2*3.14*radius;
}
}
}
我的运行结果怎么是这样?
import java.util.Scanner;
class Circle implements Shape{
double dmyr;
Circle(){
dmyr = 0;
}
Circle(double dmyr){
this.dmyr = dmyr;
if(dmyr <= 0) this.dmyr = 0;
}
public double length() {
return 2 * 3.14 * dmyr;
}
}
class Rectangle implements Shape{
double da, db;
Rectangle(){
this.da = this.db = 0;
}
Rectangle(double da){
this.da = this.db = 0;
}
Rectangle(double da, double db){
this.da = da;
this.db = db;
if(da <= 0 || db <= 0){
this.da = this.db = 0;
}
}
public double length() {
return 2 * (da + db);
}
}
class Triangle implements Shape{
double da;
double db;
double dc;
Triangle(){
this.da = this.db = this.dc = 0;
}
Triangle(double da){
this.da = this.db = this.dc = 0;
}
Triangle(double da, double db){
this.da = this.db = this.dc = 0;
}
Triangle(double da, double db, double dc){
this.da = da;
this.db = db;
this.dc = dc;
if(da <= 0 || db <= 0 || dc <= 0 || !(da + db > dc && da + dc > db && db + dc > da)){
this.da = this.db = this.dc = 0;
}
}
public double length() {
return da + db + dc;
}
}
interface Shape{
double length();
}
public class Main {
public static void main(String[] args) {
Scanner dmy = new Scanner(System.in);
while(dmy.hasNextInt()) {
double[] l = new double[5];
String s;
s = dmy.nextLine();
String[] str = s.split(" ");
int cnt = 0;
for (String value : str) {
l[cnt++] = Double.parseDouble(value);
}
if(cnt == 1){
Circle C = new Circle(l[0]);
System.out.printf("%.2f\n", C.length());
}
else if (cnt == 2){
Rectangle r = new Rectangle(l[0], l[1]);
System.out.printf("%.2f\n", r.length());
}
else{
Triangle t = new Triangle(l[0], l[1], l[2]);
System.out.printf("%.2f\n", t.length());
}
}
}
}