此题包含的内容很充足,很适合小白练习,唯一的缺点就是,已知给的太多了。出题人太贴心了,我直接哭死。不过完完整整写出此题整体的代码,也算是达到了封装继承的入门级别了。
题目很长,可以跳过(doge);
先看题目:
小明家住农村,家里有几块形状不同的地,请帮助小明计算下他家地的总面积和人均面积。系统包括家庭类,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
先多少说两句:
这题是真的长,不过这题由于已经给出很多了,所以整体不算太难。出题人还给出各步解释,我真的哭死啊,出题人都像他这样多好啊(doge)。
话不多说直接上代码(各种细节请看代码注释):
//由于需要键盘录入数据,需要导入Scanner包;
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数组
//直接new出一个对象,并利用带参构造进行初始化
}
else if(str.equals("rec")){
double height = input.nextDouble();
double width = input.nextDouble();
shapes[i]=new Rectangle(height,width);
//直接new出一个对象,并利用带参构造进行初始化,和上面的一样
}
}//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为接口的名称,就像class是类的名称
//事实上“接口”与多态类似,细节与用法上但又不完全相同
interface Shape {
//定义方法double getArea();
//接口和多态一样,只写方法名,用到时重写即可
double getArea();
}
//implements 为接口实现的专用名称,具体为 implements A,B...
class Circle implements Shape {
private double radius;
//注意:在重写方法getArea。@Override是必须要写的
@Override
public double getArea(){
return radius*radius*Math.PI;//PI不需要定义值,直接调用即可
}
public Circle() {
this(1);//this调用本类中的其它构造方法,调用时要放在构造方法的首行。
}
public Circle(double radius) {
this.radius = 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;//这里是定义了一个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(){
return getTotalArea()/numPeople;//调用方法得到总面积,在除以总人数即可
}
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;
}
}
完成收工,good!
虽然“天不生无用之人,地不长无名之草”。但仍谨记“冰冻三尺,非一日之寒”。
执长剑纵马,执妙笔生花,我王某人在此邀请诸位与我共身!