当前位置:首页 » 《随便一记》 » 正文

6 zzuliPTA家庭土地管理

16 人参与  2023年05月03日 13:21  分类 : 《随便一记》  评论

点击全文阅读


小明家住农村,家里有几块形状不同的地,请帮助小明计算下他家地的总面积和人均面积。系统包括家庭类,Shape接口,园类以及长方形类。其中园类和长方形类实现Shape接口。
Family类包含属性numPeople描述家庭人数,数组shapes描述小明家所拥有的几块土地,方法 getTotalArea(), getAvgArea()分别求出家庭总土地面积和人均土地面积。
属性numPepole 和shapes值在运行时通过键盘给出。
请完成下列代码

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in); 
        //输入土地数 input area number
        int areanum = input.nextInt();
        //输入家庭人数 peoplenum
        int peoplenum = input.nextInt();
 //创建土地数组 根据土地数 new Shape Array based on areanum
    Shape []shapes = 【】;
        for(int i=0;i<shapes.length;i++){
            String str = input.next();
            
            if(str.equals("cir")){ //判断是否是圆形土地
                double r = input.nextDouble(); //输入圆形土地的半径
                【】  //创建圆形土地对象,并放入shapes数组
            }
            else
                if(str.equals("rec")){
                    double height = input.nextDouble();
                    double width = input.nextDouble();
                     【】//创建长方形土地对象,并放入shapes数组
                }
            
        }//for 结束
        //创建家庭对象小明,将小明家的人数和土地传入
        Family xiaoming = new Family(peoplenum,shapes);
        //求小明家总土地面积
        double totalArea =【】;
        //求小明家人均土地面积
        double avgArea =【】;
        System.out.printf("total Area is:%.2f\n",totalArea);
        System.out.printf("average Area is:%.2f\n",avgArea);
    }

}

//定义接口Shape
【】 Shape {
  //定义方法double getArea();
     【】;
}

class Circle [ ]Shape {
    private double radius;

    @Override //重写方法getArea()
    []


    
    public Circle() {
        this(1);
    }

    public Circle(double radius) {
        setRadius(radius);
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

}


class Rectangle[] Shape{
    private double height,width;
 
 //方法重写
 【】


public Rectangle(double height, double width) {
        super();
        this.height = height;
        this.width = width;
    }

    public Rectangle() {
        this.width = height =1;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }
}
class Family{
    private int numPeople; //人口数
    
    private Shape[] shapes;//家庭拥有土地

    public Family(int numPeople, Shape[] shapes) {
        this.numPeople = numPeople;
        this.shapes = shapes;
    }
    
    public double getTotalArea(){
        double sum  =0;
        //求家庭土地总面积,将shapes数组中每一个土地求面积相加求和
    【】
        return sum;
    }
    //求家庭人均土地面积
public double getAvgArea(){
        【】
    }

    public int getNumPeople() {
        return numPeople;
    }

    public void setNumPeople(int numPeople) {
        this.numPeople = numPeople;
    }

    public Shape[] getShapes() {
        return shapes;
    }

    public void setShapes(Shape[] shapes) {
        this.shapes = shapes;
    }
    
}

import java.util.Scanner;public class Main {    public static void main(String[] args) {        Scanner input = new Scanner(System.in);        //输入土地数 input area number        int areanum = input.nextInt();        //输入家庭人数 peoplenum        int peoplenum = input.nextInt();        //创建土地数组 根据土地数 new Shape Array based on areanum        Shape []shapes = new Shape[areanum];        for(int i=0;i<shapes.length;i++){            String str = input.next();            if(str.equals("cir")){ //判断是否是圆形土地                double r = input.nextDouble(); //输入圆形土地的半径                shapes[i]=new Circle(r);  //创建圆形土地对象,并放入shapes数组            }            else            if(str.equals("rec")){                double height = input.nextDouble();                double width = input.nextDouble();                shapes[i]=new Rectangle(height,width) ;//创建长方形土地对象,并放入shapes数组            }        }//for 结束        //创建家庭对象小明,将小明家的人数和土地传入        Family xiaoming = new Family(peoplenum,shapes);        //求小明家总土地面积        double totalArea = xiaoming.getTotalArea();        //求小明家人均土地面积        double avgArea = xiaoming.getAvgArea();        System.out.printf("total Area is:%.2f\n",totalArea);        System.out.printf("average Area is:%.2f\n",avgArea);    }}//定义接口Shape interface Shape {    //定义方法double getArea();    double getArea();}class Circle implements Shape {private double radius;@Override //重写方法getArea()    public double getArea(){    return radius*radius*Math.PI;}public Circle() {        this(1);        }public Circle(double radius) {        setRadius(radius);        }public double getRadius() {        return radius;        }public void setRadius(double radius) {        this.radius = radius;        }}class Rectangle implements Shape{private double height,width;        //方法重写    @Override    public double getArea() {        return (height*width);    }    public Rectangle(double height, double width) {        super();        this.height = height;        this.width = width;        }public Rectangle() {        this.width = height =1;        }public double getHeight() {        return height;        }public void setHeight(double height) {        this.height = height;        }public double getWidth() {        return width;        }public void setWidth(double width) {        this.width = width;        }        }class Family{    private int numPeople; //人口数    private Shape[] shapes;//家庭拥有土地    public Family(int numPeople, Shape[] shapes) {        this.numPeople = numPeople;        this.shapes = shapes;    }    public double getTotalArea(){        double sum  =0;        //求家庭土地总面积,将shapes数组中每一个土地求面积相加求和    for(int i=0;i< shapes.length;i++)    {        sum=sum+shapes[i].getArea();    }        return sum;    }    //求家庭人均土地面积    public double getAvgArea(){        double renjun;        renjun=getTotalArea()/getNumPeople();        return renjun;    }    public int getNumPeople() {        return numPeople;    }    public void setNumPeople(int numPeople) {        this.numPeople = numPeople;    }    public Shape[] getShapes() {        return shapes;    }    public void setShapes(Shape[] shapes) {        this.shapes = shapes;    }}


点击全文阅读


本文链接:http://m.zhangshiyu.com/post/60440.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1