【Java每日一题】— —第二十四题:编程定义一个长方形类Rectangle(2023.10.08)

🕸️Hollow,各位小伙伴,今天我们要做的是第二十四题。

🎯问题:

**(**1)定义成员变量:长(int height),宽(int width);

(2)定义无参构造方法,带参构造方法;

(3)定义以上成员变量对应的getXxx()/setXxx()方法;以及一个显示所有成员信息的toString()方法;

(4)定义求周长的zhouChang()方法和求面积的area()方法;

(5)定义一个测试类RectangleDemo, 进行测试,分别用无参构造方法和带参构造方法创建对象,计算周长和面积。测试结果如下:

🎯 结果:

java 复制代码
public class Java2{
		private int height;
		private int width;
		public Java2() {//无参构造方法
		}
		public Java2(int heingt,int width) {//有参构造方法
			this.height=heingt;
			this.width=width;
		}
		public int getHeight() {
			return height;
		}
		public void setHeight(int height) {
			this.height = height;
		}
		public int getWidth() {
			return width;
		}
		public void setWidth(int width) {
			this.width = width;
		}
		@Override
		public String toString() {
			// TODO Auto-generated method stub
			return "Rectangle [ width="+width+",height="+height;
		}
		public int zhouChang() {
			return 2*(height+width);
		}
		public int area() {
			return height*width;
		}
		public static void main(String[] args) {
			 // 创建无参构造方法的对象
	        Java2 rectangle1 = new Java2();
	        rectangle1.setHeight(10);
	        rectangle1.setWidth(8);
	        System.out.println(rectangle1.toString());
	        System.out.println("周长为:" + rectangle1.zhouChang());
	        System.out.println("面积为:" + rectangle1.area());

	        // 创建带参构造方法的对象
	        Java2 rectangle2 = new Java2(12, 9);
	        System.out.println(rectangle2.toString());
	        System.out.println("周长为:" + rectangle2.zhouChang());
	        System.out.println("面积为:" + rectangle2.area());
	    }
	}
相关推荐
yuniko-n2 分钟前
【JUC】集合线程安全
java
FPGA小徐36 分钟前
【一生一芯 / PA】异常响应机制:RISC-V 中 ecall → mtvec → mret 的完整代
开发语言·数据库·c#
北京盛世宏博38 分钟前
工业环境监测选型:Modbus TCP以太网温湿度传感器部署、调试、避坑全指南
开发语言·网络·php
沫璃染墨1 小时前
《Linux工程实践篇(二):线程池实战——从线程安全单例到完整源码实现》
linux·开发语言·c++·单例模式·设计模式·系统架构
starzy19901 小时前
Flink SlotManager启动流程源码深度剖析:从ResourceManager到Slot分配的完整链路
java·大数据·flink
极光通讯1 小时前
服务器内存来料检验(IQC)实操:批次追溯、外观判定与上机验证
java·服务器·算法
4SAPI1 小时前
AI API Gateway平台哪个好?从架构视角看企业多模型网关选型与实践
java·大数据·人工智能·gateway·php
EatFan1 小时前
2026 后端 AI 工程化:Spring AI 2.0、MCP 协议与 Agent 内嵌如何收进 Java 生产系统
java·人工智能·spring·agent·spring ai·spring boot 3·mcp
泡海椒1 小时前
JQuick-Excel 多字段 TRANSFORM:让当前行字段、JContext 与展示列各归其位
开发语言·python·excel
JPower_mr.g1 小时前
SmartCall 音色管理技术解析:基于 SPI 的可扩展音色注册架构
java·开发语言·人工智能·ai·架构·开源