-->
当前位置:首页 > 题库 > 正文内容

编程题:家庭土地管理

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

}







### 输入格式:

第一行输入土地数 以及 人口数,中间以空格隔开。
每一行分别输入一块土地的信息如果时圆形土地输入例如: cir 5,如果是长方形土地输入例如:rec 20 5
### 输出格式:

第一行输出家庭土地总面积
第二行输出家庭人均土地面积

### 输入样例:

在这里给出一组输入。例如:

in
3 5
cir 100
rec 420 200
rec 1000 300



### 输出样例:

在这里给出相应的输出。例如:

out
total Area is:415415.93
average Area is:83083.19







答案:若无答案欢迎评论

发表评论

访客

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