第九章第一题(Rectangle类)(Rectangle class)
第九章第一题(Rectangle类)(Rectangle class)
9.1(Rectangle类)遵照9.2节中 Circle 类的例子,设计一个名为 Rectangle 的类表示矩形。
这个类包括:- 两个名为 width 和 height 的 double 型数据域,它们分别表示矩形的宽和高。width 和 height 的默认- 值都为1。
- 一个创建默认矩形的无参构造方法。
- 一个创建 width 和 height 为指定值的矩形的构造方法。
- 一个名为 getArea() 的方法返回这个矩形的面积。
- 一个名为 getPerimeter() 的方法返回周长。
画出该矩形的 UML 图并实现这个类。编写一个测试程序,创建两个 Rectangle 对象——一个矩形的宽为 4 而高为 40,另一个矩形的宽为 3.5 而高为 35.9 。按照这个顺序显示每个矩形的宽、高、面积、周长。
9.1(rectangle class) following the example of circle class in Section 9.2, design a class named rectangle to represent rectangle.
This class includes:- Two double data fields named width and height represent the width and height of the rectangle, respectively. The default values for width and height are 1.
- A nonparametric construction method for creating default rectangles.
- A construction method for creating a rectangle with the specified values of width and height.
- A method called getarea() returns the area of the rectangle.
- A method called getperimeter () returns the perimeter.
Draw a UML diagram of the rectangle and implement the class. Write a test program and create two rectangle objects - one rectangle is 4 in width and 40 in height, and the other is 3.5 in width and 35.9 in height. Display the width, height, area, perimeter of each rectangle in this order.
参考代码:
package chapter09;public class Code_01 {public static void main(String[] args){Rectangle r1 = new Rectangle(4,40);System.out.println("宽为" + r1.weight + "高为" + r1.height + "的矩形周长是:" + r1.getPerineter() + ",面积是:" + r1.getArea());Rectangle r2 = new Rectangle(3.5,35.9);System.out.println("宽为" + r2.weight + "高为" + r2.height + "的矩形周长是:" + r2.getPerineter() + ",面积是:" + r2.getArea());}
}
class Rectangle{double weight = 1;double height = 1;Rectangle(){}Rectangle(double newweight,double newhieght){weight = newweight;height = newhieght;}double getArea(){return height * weight;}double getPerineter(){return 2 * (height + weight);}
}
- 结果显示:
宽为4.0高为40.0的矩形周长是:88.0,面积是:160.0
宽为3.5高为35.9的矩形周长是:78.8,面积是:125.64999999999999Process finished with exit code 0
第九章第一题(Rectangle类)(Rectangle class)相关推荐
- java第九章第一题_Java第九章第五题
//编写一个应用程序,要求将LinkedList创建的对象写入到文件,然后读出一个LinkedList对象,并遍历LinkedList节点中的数据. import java.io.FileInputS ...
- C++ Primer Plus 第九章编程题练习
C++ Primer Plus 第九章编程题练习 第一题 题目描述 下面是一个头文件: const int Len = 40;struct golf {char fullname[Len];int h ...
- 用筛选法求100之内的素数。谭浩强《c语言程序设计》第六章第一题
题目 本题是谭浩强<c语言程序设计>第六章第一题 题目:用筛选法求100之内的素数. 以下是本篇文章正文内容,欢迎朋友们进行指正,一起探讨,共同进步.--来自考研路上的lwj 一.解题思路 ...
- c语言苏小江第九章实验题答案,蓝桥杯C语言C组校内赛题目解析
今天事非常多,所有事情都凑在一起忙的不可开交.早上社团原本要搞活动,又恰好凑上实训课,三四节来不及搬东西过去,只能下课后再过去帮忙,手机没电,还是要忙到接近12点半才来得及回宿舍吃外卖. 饭还没吃几口 ...
- 疯狂java的第四章答案_疯狂java讲义第四章第一题
package chapter04; /**** * 本帖最后由 kongyeeku 于 2012-3-11 23:02 编辑 看到有一些读者询问关于该题的解题思路,其实这个题是有一些很绕的地方. 大 ...
- 第九章总结 java常用类
9.1 包装类 Java是一种面向对象的语言,但在Java中不能定义基本数据类型的对象,为了能将基本数据类型视为对象进行处理,Java提出了包装类的概念,它主要是将基本数据类型封装在包装类中,如in ...
- 第九章:Java_枚举类和注解
一.枚举类 1.如何自定义枚举类. 枚举类:类的对象是有限个的,确定的. 1.1 私有化类的构造器,保证不能在类的外部创建其对象 1.2 在类的内部创建枚举类的实例.声明为:public static ...
- 数据结构与算法python语言描述答案_《数据结构与算法Python语言描述》习题第二章第一题(python版)...
1 #!/usr/bin/env python 2 #-*- coding:utf-8 -*- 3 4 """ 5 定义一个表示时间的类Time6 a)Time(hour ...
- c++primer plus第六版第十二章第一题
//h文件 #pragma once class cow {char name[20];char*hobby;double weight; public:cow();cow(const char*m, ...
最新文章
- MySQL 优化必经之路, Explain执行计划 ?
- 全球及中国皮肤癌药物行业深度研究及项目可行性调研报告2022-2027年
- [云炬创业基础笔记]第六章商业模式测试16
- php run js,PHP switch 语句
- GitHub 基本常用知识解答2
- 虚拟机无法访问主机mysql_虚拟机linux端mysql数据库无法远程访问的解决办法
- SpringMVC之数据传递三Ajax与Controller交互
- 红外线遥控c语言程序,红外线遥控解码接收程序-c语言讲解学习.pdf
- 服务器上装双系统,服务器上安装双系统
- 【日常水题——快速幂系列】
- 30种常用管理工具模型整理分享(下)
- 构造函数this指向
- Mac/Linux下使用vim出现E45: ‘readonly‘ option is set (add ! to override)错误
- 经历3个失败的游戏创业公司,我所亲历的痛与教训
- 图解 魔方快速还原 7步法
- unreal无损音乐百度云_Roxette女主唱Marie Fredriksson - Den Standiga Resan 个人瑞典语专辑 [APE]...
- 让我们恭喜阿根廷队夺得大力神杯
- 软件测试五大特性,《软件测试52讲》——代码测试篇
- 【Raft】学习九:成员变更ConfChangeV2
- 《算法竞赛进阶》学习笔记
热门文章
- 【搜索神器——Everything】的下载安装使用教程
- python的三维坐标轴设置_如何更改matplotlib中的三维轴设置
- 简单总结常用PCB元器件封装
- 非图形化流程定义部件的要素
- 雅诗兰黛公司启用旅游零售渠道专用的加尔盖嫩先进分销中心,加强全球履约网络
- 计算机办公软件应用实训,计算机专业职业教育实训系列教材:Office 2007办公软件实训教程...
- ReplayKit2 Broadcast Upload Extension 直播app列表不出现自己的app
- 购物中心户外广场美陈设计方案
- RGB565调色板Ver1.0.0
- Nhibernate介绍与使用