我是靠谱客的博主 迷人宝贝,这篇文章主要介绍JAVA作业记录7—矩形类,现在分享给大家,希望可以做个参考。

题目:
(Rectangle类)遵照Circle类的例子,设计一个名为Rectangle的类表示矩形。这个类包括:
●两个名为width和height的double类型数据域,它们分别表示矩形的宽和高。width和height的默认值都为1。
●一个用于创建默认矩形的无参构造方法。
●一个创建指定width和height值的矩形的构造方法。
●一个名为getArea()的方法,返回该矩形的面积。
●一个名为getPerimeter()的方法,返回周长。
画出该类的UML图并实现这个类。
编写个测试程序 , 创建两个Rectangle 对象,一个 矩形对象的宽为4而高为40,另一个矩形 对象的宽为3.5而高为35.9。按照顺序显示每个矩形的宽、高、面积和周长。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.Scanner; public class TRectangle{ public static void main(String args[]){ Rectangle a = new Rectangle(4,40); System.out.println("The first Rectangle's width is "+a.width+" ,height is "+a.height); System.out.println("The first Rectangle's Area is "+a.getArea()+" ,Perimeter is "+a.getPerimeter()); System.out.println(); Rectangle b = new Rectangle(3.5,35.9); System.out.println("The second Rectangle's width is "+b.width+" ,height is "+b.height); System.out.println("The second Rectangle's Area is "+b.getArea()+" ,Perimeter is "+b.getPerimeter()); } } class Rectangle{ double width=1.0; double height=1.0; //无参构造方法 Rectangle(){ } //传递新参数构造方法 Rectangle(double newWidth,double newHeight){ width = newWidth; height = newHeight; } //计算面积 double getArea(){ return width*height; } //计算周长 double getPerimeter(){ return 2*(height+width); } }

程序运行结果:
在这里插入图片描述
UML图:
在这里插入图片描述

最后

以上就是迷人宝贝最近收集整理的关于JAVA作业记录7—矩形类的全部内容,更多相关JAVA作业记录7—矩形类内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(81)

评论列表共有 0 条评论

立即
投稿
返回
顶部