6 zzuliPTA家庭土地管理

小明家住农村,家里有几块形状不同的地,请帮助小明计算下他家地的总面积和人均面积。系统包括家庭类,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;
    }

}

  • 8
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
小明最近迷上了积木画,有这么两种类型的积木,分别为 i 型(大小为 2 个单位面积)和 Z 型(大小为 3 个单位面积)。他喜欢用这些积木拼出各种各样的图案,让他的创造力得以充分发挥。 小明发现,i 型积木是一块长方形,长和宽各为 1 个单位面积,所以他可以将它朝向横向或纵向摆放。而 Z 型积木是由两块长方形组成,分别是 1*2 和 1*1 的长方形,他可以将它们组合成 Z 字形。 小明利用这两种积木形状的多样性,开始创作了一些简单的图案。他发现,通过反复组合和变化积木的位置和朝向,他可以拼出各种不同形状和图案。他拼出了心形、小动物等各种可爱的形状,还尝试着拼出了字母的形态。 小明也发现,他可以通过不同的颜色来增加图案的美感。他购买了一些不同颜色的积木,有红、蓝、黄等多种颜色。他发现,同样的图案在不同的颜色下,给人的感觉完全不同。红色的图案给人热情和活力的感觉,蓝色的图案则显得沉静和深邃。 小明觉得积木画是一种很好的思维训练和创造力发展的方式。通过将积木组合成各种图案,他不仅能够锻炼自己的空间想象力和手眼协调能力,还能够培养自己的创造力和想象力。每次完成一幅图案,小明都会感到满足和自豪。 总而言之,小明喜欢积木画这项活动,他通过 i 型和 Z 型积木的组合和变化,创作出了各种各样的图案。他发现,这项活动不仅可以锻炼自己的空间想象力和手眼协调能力,还可以培养创造力和想象力。他非常享受这种创作过程,每次完成一幅图案都会感到满足和自豪。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豆粉今天敲了吗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值