pta图形题

import java.util.*;

public class Main{

    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        int i =0;

        while (i<n){

            int judge = sc.nextInt();

            if (judge==1){

                double r = sc.nextDouble();

                Circle circle = new Circle(r);

                String a1 = String.format("%.2f", circle.getArea());

                String a2 = String.format("%.2f", circle.getPerimeter());

                System.out.println(a1+" "+a2);

            }

            else if (judge==2){

                double length = sc.nextDouble();

                double width = sc.nextDouble();

                Rectangle rectangle = new Rectangle(length, width);

                String c1 = String.format("%.2f",rectangle.getArea());

                String c2 = String.format("%.2f", rectangle.getPerimeter());

                System.out.println(c1+" "+c2);

            }

            i++;

        }

    }

}

class Circle implements Shape{

    private double radius;


    public Circle(double radius) {

        this.radius = radius;

    }


    @Override

    public float getArea() {

        float area= (float) (Math.PI*Math.pow(radius,2));

        return area;

    }


    @Override

    public float getPerimeter() {

        float perimeter = (float) (2*Math.PI*radius);

        return perimeter;

    }

}

interface Shape{

    float getArea();//求面积

    float getPerimeter();//求周长

}


class Rectangle implements Shape{

    private double length;

    private double width;


    public Rectangle(double length, double width) {

        this.length = length;

        this.width = width;

    }


    @Override

    public float getArea() {

        float area= (float) (length*width);

        return area;

    }


    @Override

    public float getPerimeter() {

        float perimeter= (float) ((length+width)*2);

        return perimeter;

    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 2014年的苹果全球开发者大会(WWDC),当Craig Federighi向全世界宣布“We have new ...
    yeshenlong520阅读 2,312评论 0 9
  • DAY 05 1、 public classArrayDemo { public static void mai...
    周书达阅读 758评论 0 0
  • package code; public class Animal { private String name; ...
    玄薛烨阅读 264评论 0 0
  • 每日要点 杂项 模板方法模式模板方法模式(GoF设计模式)使用了JAVA的继承机制,在抽象类中定义一个模板方法,该...
    迷茫o阅读 225评论 0 0
  • 以后要怎么教育小孩呢,第一,要读名人传记,让他知道生命可以达到的高度,第二,再读创业攻略,让他不要拘束于传统 每年...
    linjisong阅读 198评论 0 0