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;

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 2014年的蘋果全球開發(fā)者大會(WWDC),當(dāng)Craig Federighi向全世界宣布“We have new ...
    yeshenlong520閱讀 2,405評論 0 9
  • DAY 05 1、 public classArrayDemo { public static void mai...
    周書達(dá)閱讀 834評論 0 0
  • package code; public class Animal { private String name; ...
    玄薛燁閱讀 279評論 0 0
  • 每日要點(diǎn) 雜項(xiàng) 模板方法模式模板方法模式(GoF設(shè)計模式)使用了JAVA的繼承機(jī)制,在抽象類中定義一個模板方法,該...
    迷茫o閱讀 271評論 0 0
  • 以后要怎么教育小孩呢,第一,要讀名人傳記,讓他知道生命可以達(dá)到的高度,第二,再讀創(chuàng)業(yè)攻略,讓他不要拘束于傳統(tǒng) 每年...
    linjisong閱讀 226評論 0 0

友情鏈接更多精彩內(nèi)容