列表

详情


JAVA50. 根据周长求面积

描述

已知:
1. 图形类Shape,该类中定义了图形的周长属性,以及返回周长的方法。
2. Area接口,该接口中定义了返回面积的方法getArea()。
要求:
1. 定义圆形类Circle,使其继承于Shape,并实现Area接口。
2. 定义方形类Square,使其继承于Shape,并实现Area接口。
注意:
圆周率要使用Math类中的常量。

输入描述

周长

输出描述

面积(计算时请使用Math类中的常量,面积为double类型,保留三位小数,四舍五入部分预设代码已经完成)

示例1

输入:

4

输出:

1.273
1.000

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Java 解法, 执行用时: 38ms, 内存消耗: 12384KB, 提交时间: 2022-02-10

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextDouble()) {
            double s = scanner.nextDouble();
            // Circle和Square是需要你定义的类
            System.out.println(String.format("%.3f",new Circle(s).getArea()));
            System.out.println(String.format("%.3f", new Square(s).getArea()));
        }
    }

}

class Shape {

    private double s; // 周长

    public Shape(double s) {
        this.s = s;
    }

    public double getS() {
        return s;
    }

}

interface Area {
    double getArea(); // 面积
}

// 圆形
class Circle extends Shape implements Area {

    //write your code here......
    public Circle (double s){
        super(s);
    }
    double r=getS()/(2*Math.PI);
    @Override
    public double getArea(){        
        return Math.PI*r*r;    
    }

}

// 方形
class Square extends Shape implements Area {

    //write your code here......
    public Square(double s){
        super(s);
    }
    double r=super.getS()/4;
    @Override
    public double getArea(){
        return r*r;
    }

}

Java 解法, 执行用时: 40ms, 内存消耗: 12372KB, 提交时间: 2021-11-25

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextDouble()) {
            double s = scanner.nextDouble();
            // Circle和Square是需要你定义的类
            System.out.println(String.format("%.3f",new Circle(s).getArea()));
            System.out.println(String.format("%.3f", new Square(s).getArea()));
        }
    }

}

class Shape {

    private double s; // 周长

    public Shape(double s) {
        this.s = s;
    }

    public double getS() {
        return s;
    }

}

interface Area {
    double getArea(); // 面积
}

// 圆形
class Circle extends Shape implements Area {

    //write your code here......


    public Circle(double s) {
        super(s);
    }

    @Override
    public double getArea() {
        return Math.PI * Math.pow((getS() / (2 * Math.PI)), 2);
    }
}

// 方形
class Square extends Shape implements Area {

    //write your code here......


    public Square(double s) {
        super(s);
    }

    @Override
    public double getArea() {
        return Math.pow((getS() / 4), 2);
    }
}

Java 解法, 执行用时: 40ms, 内存消耗: 12504KB, 提交时间: 2021-10-17

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextDouble()) {
            double s = scanner.nextDouble();
            // Circle和Square是需要你定义的类
            System.out.println(String.format("%.3f",new Circle(s).getArea()));
            System.out.println(String.format("%.3f", new Square(s).getArea()));
        }
    }

}

class Shape {

    private double s; // 周长

    public Shape(double s) {
        this.s = s;
    }

    public double getS() {
        return s;
    }

}

interface Area {
    double getArea(); // 面积
}

// 圆形
class Circle extends Shape implements Area {
     
    public Circle(double s){
         super(s);
     }
    public double getArea(){
        //周长=2πr  则 r=周长/2π
        double r = getS()/(2*Math.PI);
        return Math.PI * r * r;
    }
    

}

// 方形
class Square extends Shape implements Area {
     public Square(double s){
         super(s);
     }
     public double getArea(){
        return Math.pow(getS() / 4,2);
    }
    

}

Java 解法, 执行用时: 40ms, 内存消耗: 12584KB, 提交时间: 2021-11-13

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNextDouble()){
            double s=scanner.nextDouble();
            System.out.println(String.format("%.3f",new Circle(s).getArea()));
            System.out.println(String.format("%.3f",new Square(s).getArea()));
        }
    }
}

class Shape{
    private double s;
    public Shape(double s){
        this.s=s;
    }
    public double getS(){
        return s;
    }
}

interface Area{
    double getArea();
}

class Circle extends Shape implements Area{
    public Circle(double s){
        super(s);
    }
    @Override
    public double getArea(){
        return Math.PI*Math.pow(getS()/(2*Math.PI),2);
    }
}

class Square extends Shape implements Area{
    public Square(double s){
        super(s);
    }
    @Override
    public double getArea(){
        return Math.pow(getS()/4,2);
    }
}

Java 解法, 执行用时: 40ms, 内存消耗: 12688KB, 提交时间: 2021-11-21

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextDouble()) {
            double s = scanner.nextDouble();
            // Circle和Square是需要你定义的类
            System.out.println(String.format("%.3f",new Circle(s).getArea()));
            System.out.println(String.format("%.3f", new Square(s).getArea()));
        }
    }

}

class Shape {

    private double s; // 周长

    public Shape(double s) {
        this.s = s;
    }

    public double getS() {
        return s;
    }

}

interface Area {
    double getArea(); // 面积
}

// 圆形
class Circle extends Shape implements Area {

    //write your code here......
    public Circle(double s){
        super(s);
    }
    public double getArea(){
         return Math.pow(getS()/(2*Math.PI),2)*Math.PI;
    }

}

// 方形
class Square extends Shape implements Area {

    //write your code here......
    public Square(double s){
        super(s);
    }
    public double getArea(){
        return Math.pow(getS()/4,2);
    }

}

上一题