2020-05-25

本文介绍了一个简单的Java程序,通过定义Rectangle和Circle类来计算并输出多个长方形和圆形的总面积和总周长。代码中使用了数组存储对象,并利用Arrays.deepToString方法打印数组内容。
  1. 定义长方形类与圆形类Circle
    长方形类-类名:Rectangle,private属性:int width,length圆形类-类名:Circle,private属性:int radius编写构造函数:带参构造函数:Rectangle(width, length),Circle(radius)编写方法:public int getPerimeter(),求周长。public int getArea(),求面积。toString方法,使用Eclipse自动生成。注意:计算圆形的面积与周长,使用Math.PI。求周长和面积时,应先计算出其值(带小数位),然后强制转换为int再返回。
  2. main方法输入2行长与宽,创建两个Rectangle对象放入相应的数组。
    输入2行半径,创建两个Circle对象放入相应的数组。输出1:上面2个数组中的所有对象的周长加总。输出2:上面2个数组中的所有对象的面积加总。最后需使用Arrays.deepToString分别输出上面建立的Rectangle数组与Circle数组思考:如果初次做该题会发现代码冗余严重。使用继承、多态思想可以大幅简化上述代码。

代码块:
import java.util.Arrays;
import java.util.Scanner;
public class Main {public static void main(String[] args) {Rectangle[] rectangle = new RectangleRectangle[2];
Circle[] circle = new CircleCircle[2];
Scanner sc = new ScannerScanner(System.in);
for(int i=0;i<rectangle.length;i++) {
rectangle[i] = newRectangleRectangle(sc.nextInt(),sc.nextInt());
}
for(int i=0;i<circle.length;i++) {
circle[i] = new CircleCircle(sc.nextInt());
}
System.out.println(rectangle[0].getPerimeter()+rectangle[1].getPerimeter()+circle[0].getPerimeter()+circle[1].getPerimeter());
System.out.println(rectangle[0].getArea()+rectangle[1].getArea()+circle[0].getArea()+circle[1].getArea());
System.out.println(Arrays.deepToString(rectangle));
System.out.println(Arrays.deepToString(circle));
sc.close();
}
}
class RectangleRectangle{private int width;
private int length;
public Rectangle(int width,int length) {
this.width = width;
this.length = length;
}
//求周长方法
public int getPerimeter() {return 2(width+length);
}
//求面积方法
public int getArea() {
return widthlength;
}
@Override
public String toString() {
return “Rectangle [width=” + width + “, length=” + length + “]”;
}
}
class CircleCircle{
private int radius;
public Circle(int radius) {this.radius = radius;
}
//求周长方法
public int getPerimeter() {
return (int)(2Math.PIradius);
}
//求面积方法
public int getArea() {
return (int)(Math.PIradiusradius);
}
@Override
public String toString() {
return “Circle [radius=” + radius + “]”;
}
}

编译时报错: In file included from /usr/include/string.h:535, from /home/tp/workfile/de95/iplatform/board/model_qca_wifi7/sdk/12.1/build_dir/hostpkg/libubox-2020-05-25-66195aee/blob.h:25, from /home/tp/workfile/de95/iplatform/board/model_qca_wifi7/sdk/12.1/build_dir/hostpkg/libubox-2020-05-25-66195aee/blobmsg.h:20, from /home/tp/workfile/de95/iplatform/board/model_qca_wifi7/sdk/12.1/build_dir/hostpkg/libubox-2020-05-25-66195aee/blobmsg.c:16: In function ‘strcpy’, inlined from ‘blobmsg_new’ at /home/tp/workfile/de95/iplatform/board/model_qca_wifi7/sdk/12.1/build_dir/hostpkg/libubox-2020-05-25-66195aee/blobmsg.c:196:2: /usr/include/x86_64-linux-gnu/bits/string_fortified.h:79:10: error: ‘__builtin_strcpy’ offset 6 from the object at ‘attr’ is out of the bounds of referenced subobject ‘name’ with type ‘uint8_t[]’ {aka ‘unsigned char[]’} at offset 6 [-Werror=array-bounds] 79 | return __builtin___strcpy_chk (__dest, __src, __glibc_objsize (__dest)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from /home/tp/workfile/de95/iplatform/board/model_qca_wifi7/sdk/12.1/build_dir/hostpkg/libubox-2020-05-25-66195aee/blobmsg.c:16: /home/tp/workfile/de95/iplatform/board/model_qca_wifi7/sdk/12.1/build_dir/hostpkg/libubox-2020-05-25-66195aee/blobmsg.c: In function ‘blobmsg_new’: /home/tp/workfile/de95/iplatform/board/model_qca_wifi7/sdk/12.1/build_dir/hostpkg/libubox-2020-05-25-66195aee/blobmsg.h:42:17: note: subobject ‘name’ declared here 42 | uint8_t name[];
最新发布
10-11
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值