小明家住农村,家里有几块形状不同的地,请帮助小明计算下他家地的总面积和人均面积。系统包括家庭类,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
输出格式:
第一行输出家庭土地总面积
第二行输出家庭人均土地面积
输入样例:
在这里给出一组输入。例如:
3 5
cir 100
rec 420 200
rec 1000 300
输出样例:
在这里给出相应的输出。例如:
total Area is:415415.93
average Area is:83083.19
代码范例:
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(); //输入圆形土地的半径
Circle area1=new Circle(r); //创建圆形土地对象,并放入shapes数组
shapes[i]=area1;
}
else
if(str.equals("rec")){
double height = input.nextDouble();
double width = input.nextDouble();
Rectangle area2=new Rectangle(height,width);//创建长方形土地对象,并放入shapes数组
shapes[i]= area2;
}
}//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 Math.PI*Math.pow(radius,2);
}
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+=shapes[i].getArea();
}
return sum;
}
//求家庭人均土地面积
public double getAvgArea(){
double AveAera=getTotalArea()/getNumPeople();
return AveAera;
}
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;
}
}