【JAVA】实验二 类与对象

实验名称    实验二 类与对象

实验目的   

1. 深刻理解类的封装与继承;

2. 熟练掌握类的定义、包与路径、对象的创建、方法的调用、类的继承、方法的重写、运行时多态、访问权限修饰符的使用等;

3. 熟练运用JDK提供的常用类及API。  

实验内容(4学时)

1. 定义一个圆形类Circle,包括:

(1)属性:圆心、半径

(2)方法:求面积、周长;上下左右平移;缩放;绘图(虚拟表示)。

设计测试类CircleDemo,在测试类中测试上述方法。以后实验中均自行设计测试类。

class Circle {private double centerX;private double centerY;private double radius;public Circle(double centerX, double centerY, double radius) {this.centerX = centerX;this.centerY = centerY;this.radius = radius;}public double getArea() {return Math.PI * radius * radius;}public double getPerimeter() {return 2 * Math.PI * radius;}public void translate(double deltaX, double deltaY) {centerX += deltaX;centerY += deltaY;}public void scale(double factor) {radius *= factor;}public void draw() {System.out.println("Drawing a circle at (" + centerX + ", " + centerY + ") with radius " + radius);}
}public class CircleDemo {public static void main(String[] args) {Circle circle = new Circle(0, 0, 5.0);System.out.println("Area: " + circle.getArea());System.out.println("Perimeter: " + circle.getPerimeter());circle.translate(2.0, 3.0);circle.scale(1.5);circle.draw();}
}

2. 设计Bird类,包括:(1)属性:name;(2)方法:fly( ),fly方法以及后面提到的各种方法均以字符串输出来演示功能。

以Bird类为超类(父类),设计子类CarrierPigeon,

(1)为CarrierPigeon类新增方法:send(String address, String message)

(2)在CarrierPigeon 覆盖 Bird 的 fly() 方法

public class Bird {private String name;public Bird(String name) {this.name = name;}public String getName() {return name;}public void fly() {System.out.println(name + " is flying.");}
}public class CarrierPigeon extends Bird {public CarrierPigeon(String name) {super(name);}public void send(String address, String message) {System.out.println(getName() + " is sending a message to " + address + ": " + message);}@Overridepublic void fly() {System.out.println(getName() + " is flying with a message.");}
}public class BirdDemo {public static void main(String[] args) {Bird bird = new Bird("Sparrow");bird.fly();CarrierPigeon pigeon = new CarrierPigeon("Pigeon");pigeon.fly();pigeon.send("Recipient", "Important message");}
}

3. Java编程实现:设计复数类Complex,类中实部和虚部都是实数,实现加法、减法、乘法和除法。

public class Complex {private double real;private double imaginary;public Complex(double real, double imaginary) {this.real = real;this.imaginary = imaginary;}public Complex add(Complex other) {double newReal = this.real + other.real;double newImaginary = this.imaginary + other.imaginary;return new Complex(newReal, newImaginary);}public Complex subtract(Complex other) {double newReal = this.real - other.real;double newImaginary = this.imaginary - other.imaginary;return new Complex(newReal, newImaginary);}public Complex multiply(Complex other) {double newReal = this.real * other.real - this.imaginary * other.imaginary;double newImaginary = this.real * other.imaginary + this.imaginary * other.real;return new Complex(newReal, newImaginary);}public Complex divide(Complex other) {double denominator = other.real * other.real + other.imaginary * other.imaginary;double newReal = (this.real * other.real + this.imaginary * other.imaginary) / denominator;double newImaginary = (this.imaginary * other.real - this.real * other.imaginary) / denominator;return new Complex(newReal, newImaginary);}@Overridepublic String toString() {return real + " + " + imaginary + "i";}
}public class ComplexDemo {public static void main(String[] args) {Complex c1 = new Complex(2.0, 3.0);Complex c2 = new Complex(1.0, 1.0);Complex sum = c1.add(c2);Complex difference = c1.subtract(c2);Complex product = c1.multiply(c2);Complex quotient = c1.divide(c2);System.out.println("Sum: " + sum);System.out.println("Difference: " + difference);System.out.println("Product: " + product);System.out.println("Quotient: " + quotient);}
}

4. Java编程实现:设计矩阵类Matrix,类中的方法能对矩阵进行加法、减法和乘法运算。在矩阵中再定义一个方法,生成如下的矩阵:

public class Matrix {private int[][] data;public Matrix(int[][] data) {this.data = data;}// 获取矩阵的行数public int getRows() {return data.length;}// 获取矩阵的列数public int getColumns() {return data[0].length;}// 矩阵加法public Matrix add(Matrix other) {if (getRows() != other.getRows() || getColumns() != other.getColumns()) {throw new IllegalArgumentException("矩阵维度不匹配");}int[][] result = new int[getRows()][getColumns()];for (int i = 0; i < getRows(); i++) {for (int j = 0; j < getColumns(); j++) {result[i][j] = data[i][j] + other.data[i][j];}}return new Matrix(result);}// 矩阵减法public Matrix subtract(Matrix other) {if (getRows() != other.getRows() || getColumns() != other.getColumns()) {throw new IllegalArgumentException("矩阵维度不匹配");}int[][] result = new int[getRows()][getColumns()];for (int i = 0; i < getRows(); i++) {for (int j = 0; j < getColumns(); j++) {result[i][j] = data[i][j] - other.data[i][j];}}return new Matrix(result);}// 矩阵乘法public Matrix multiply(Matrix other) {if (getColumns() != other.getRows()) {throw new IllegalArgumentException("矩阵维度不匹配");}int[][] result = new int[getRows()][other.getColumns()];for (int i = 0; i < getRows(); i++) {for (int j = 0; j < other.getColumns(); j++) {int sum = 0;for (int k = 0; k < getColumns(); k++) {sum += data[i][k] * other.data[k][j];}result[i][j] = sum;}}return new Matrix(result);}// 生成指定的矩阵public static Matrix createMatrixE() {int[][] eMatrixData = {{1, 3, 8, 7, 5, 6},{3, 8, 7, 5, 6, 1},{8, 7, 5, 6, 1, 3},{7, 5, 6, 1, 3, 8},{5, 6, 1, 3, 8, 7},{6, 1, 3, 8, 7, 5}};return new Matrix(eMatrixData);}// 打印矩阵public void printMatrix() {for (int i = 0; i < getRows(); i++) {for (int j = 0; j < getColumns(); j++) {System.out.print(data[i][j] + " ");}System.out.println();}}public static void main(String[] args) {Matrix matrixE = createMatrixE();System.out.println("Matrix E:");matrixE.printMatrix();Matrix matrixA = new Matrix(new int[][] {{1, 2, 3},{4, 5, 6},{7, 8, 9}});Matrix matrixB = new Matrix(new int[][] {{9, 8, 7},{6, 5, 4},{3, 2, 1}});System.out.println("\nMatrix A:");matrixA.printMatrix();System.out.println("\nMatrix B:");matrixB.printMatrix();Matrix matrixSum = matrixA.add(matrixB);System.out.println("\nMatrix A + B:");matrixSum.printMatrix();Matrix matrixDifference = matrixA.subtract(matrixB);System.out.println("\nMatrix A - B:");matrixDifference.printMatrix();Matrix matrixProduct = matrixA.multiply(matrixB);System.out.println("\nMatrix A * B:");matrixProduct.printMatrix();}
}

实验程序及结果(附录)

思考

以C为代表的结构化编程语言和以Java为代表的面向对象编程语言有哪些本质不同?

关于结构化编程语言(以C为代表)和面向对象编程语言(以Java为代表)的本质不同:

  1. 抽象与封装:面向对象编程强调对象的抽象和封装,允许将数据和操作封装在对象内部,提供更好的信息隐藏和模块化。结构化编程相对较少使用对象,更多地依赖于函数和数据的分离。
  2. 继承与多态:面向对象编程支持继承和多态,允许创建层次结构的类,重用代码并实现多态性。结构化编程通常较少使用这些概念,更注重逻辑流程和模块化设计。
  3. 对象:面向对象编程以对象为中心,将数据和操作封装在对象中。结构化编程更倾向于使用数据结构和函数。
  4. 设计模式:面向对象编程强调设计模式,例如单例模式、工厂模式等,以提供更好的可维护性和可扩展性。结构化编程通常较少使用这些模式。
  5. 类型系统:面向对象编程通常有更强的类型系统,支持多态和动态绑定。结构化编程的类型系统通常较为简单。

总之,面向对象编程更注重数据和操作的封装、继承、多态等概念,而结构化编程更注重逻辑流程和分离数据和函数。不同编程范式适用于不同类型的问题和项目。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/599063.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

文件二维码能下载文件吗?扫码看文件效率更高

为了让文件更快的传递&#xff0c;现在将文件制作二维码图片后&#xff0c;让其他人通过扫码查看或者下载文件的方式&#xff0c;被越来越多的人应用。一般想要制作文件二维码&#xff0c;大多会使用文件二维码生成器&#xff08;文件二维码生成器_word、excel、ppt、pdf文档制…

linux下安装Nginx及其常用命令

安装Nginx 接下来在Linux服务器进行操作就可以了 安装插件 yum -y install gcc pcre-devel zlib-devel openssl openssl-devel直接使用wget进行安装(如果没有wget需要先安装wget) yum install wgetwget https://nginx.org/download/nginx-1.24.0.tar.gz解压 tar -zxvf nginx..…

代码训练day59|单调栈part02

参考&#xff1a; 代码随想录 如何高效解决接雨水问题 | labuladong 的算法笔记 503.下一个更大元素II 与下一个更大元素&#xff5c;的区别就是要把数组考虑为环形&#xff08;只有数组内最大值为-1&#xff09; 按照之前的环形为题解决经验&#xff0c;直接拼接两个数组解…

解决mock单元测试中 无法获取实体类xxx对应的表名

错误描述&#xff1a;在执行单元测试时&#xff0c;执行到new Example时抛出异常&#xff0c;提示无法获取实体类xxx对应的表名 Example example new Example(ServeSubscribeRecord.class);Example.Criteria criteria example.createCriteria();criteria.andEqualTo("se…

【Linux】Linux Page Cache页面缓存的原理

Page cache&#xff08;页面缓存&#xff09;是计算机操作系统中的一种机制&#xff0c;用于将频繁访问的数据从磁盘存储到内存中&#xff0c;以便更快地访问。当程序从磁盘请求数据时&#xff0c;操作系统会检查该数据是否已经存在于页面缓存中。如果存在&#xff0c;数据可以…

QT上位机开发(动态库dll的开发)

【 声明&#xff1a;版权所有&#xff0c;欢迎转载&#xff0c;请勿用于商业用途。 联系信箱&#xff1a;feixiaoxing 163.com】 有的时候&#xff0c;我们不想把所有的代码都放在一个exe里面&#xff0c;这个时候我们就需要引入dll动态库的概念。在windows平台上面&#xff0c…

IC工程师到底有哪些?每个岗位具体有哪些要求?

随着摩尔定律和技术的发展&#xff0c;芯片集成度也越来越高&#xff0c;与之伴随的就是岗位愈加细分。芯片产业链很长且环环相扣&#xff0c;每一个环节都需要不同的工程师角色分工协作。 很多人以为芯片工程师就是单纯搞芯片的工程师&#xff0c;殊不知这其中可能要分十几个…

FlagData 2.0:全面、高效的大模型训练数据治理工具集

数据是大模型训练至关重要的一环。数据规模、质量、配比&#xff0c;很大程度上决定了最后大模型的性能表现。无论是大规模的预训练数据、精益求精的SFT数据都依托于一个完整的“获取-准备-处理-分析”数据流程。然而&#xff0c;当前的开源工具通常只专注于流程中的某一环节&a…

ThreeJs通过canvas和Sprite添加标签

在3D场景中很多时候会用到给模型添加标签&#xff0c;以表示这个模型的代号&#xff0c;尤其是大量重复模型的时候&#xff0c;添加标签是为了更直观的看到场景中每个模型的数据和名称&#xff0c;比如在仓库中有很多货架&#xff0c;就会需要查看每个货架的编号&#xff0c;如…

Flink Connector 开发

Flink Streaming Connector Flink是新一代流批统一的计算引擎&#xff0c;它需要从不同的第三方存储引擎中把数据读过来&#xff0c;进行处理&#xff0c;然后再写出到另外的存储引擎中。Connector的作用就相当于一个连接器&#xff0c;连接Flink计算引擎跟外界存储系统。Flin…

ES6定义一个类(函数内部定义属性,,原型定义方法 ), 实现继承?

ES6中使用class关键字定义一个类&#xff0c;使用extends关键字实现继承。下面是一个示例&#xff1a; class Animal {constructor(name) {this.name name;}sayHello() {console.log(Hello, my name is ${this.name});} }class Dog extends Animal {constructor(name, breed)…

长亭牧云主机管理助手——免费轻量的服务器管理软件初体验

优点 安装十分简单&#xff0c;新手友好&#xff0c;一行命令搞定界面简洁&#xff0c;操作流畅无需公网 IP&#xff0c;可以面对复杂 NAT 环境进行救急可以统一管理大量主机&#xff0c;无需记住主机秘钥 地址 https://rivers.chaitin.cn/app/collie 安装 安装很简单&…

向下取整和向上取整的定义,各有什么用处。

问题描述&#xff1a;向下取整和向上取整的定义&#xff0c;各有什么用处。 问题解答&#xff1a; 向下取整&#xff08;Floor&#xff09;&#xff1a; 向下取整是指将一个实数向下舍入到最接近的、不超过它的整数。用数学符号表示为 ⌊x⌋&#xff0c;其中 x 是实数。例如&am…

基于 listmonk 的电子邮件营销解决方案

背景 电子邮件营销&#xff08;EDM&#xff09;在广告、电商、供应链物流等行业应用广泛&#xff0c;亚马逊云科技的市场部门持续不断的收到客户反馈&#xff0c;希望可以提供简单便捷的方案。 亚马逊云科技产品体验链接&#xff1a;点击我立即体验 对于发送邮件的需求&…

深耕汽车检测设备领域,引领行业技术革新

在汽车工业飞速发展的今天&#xff0c;汽车检测技术作为保障车辆安全、提升维修效率的重要手段&#xff0c;日益受到行业内外的高度关注。康士柏汽车检测线设备厂家&#xff0c;作为这一领域的佼佼者&#xff0c;凭借其深厚的技术积累和卓越的产品品质&#xff0c;正引领着行业…

c# 学习笔记 - 委托(Delegate)

文章目录 1. 委托1.1 委托概述1.2 委托使用1.3 委托的传播 2. 匿名方法2.1 匿名方法概述2.2 匿名方法 1. 委托 1.1 委托概述 委托简介 委托就是对方法的引用&#xff0c;可以理解为例如整型变量的容器可以存储整形数据&#xff0c;委托就是某种方法的容器&#xff0c;可以用来…

Future、CompletionService、CompletableFuture介绍与对比

目录 Future1、基本介绍2、按照提交任务的顺序获取执行结果 CompletionService1、介绍2、按照任务完成的先后顺序获取结果 CompletableFuture1、介绍2、CompletableFuture怎么非阻塞的获取任务结果 Future 1、基本介绍 Future是JDK1.5 提供的接口&#xff0c;是用来以阻塞的方…

python每日一题:连续子数组的最大和

这是一道关于动态规划的算法题&#xff1a; 题目描述&#xff1a; 给定一个整数数组 nums&#xff0c;请找出该数组中连续子数组的最大和&#xff0c;并返回这个最大和。 示例&#xff1a; 输入&#xff1a;[-2, 1, -3, 4, -1, 2, 1, -5, 4] 输出&#xff1a;6 解释&#xff…

如何用Rust编程访问未知结构的json串?

如何用Rust访问未知结构的JSON串&#xff0c;并包含对数组的访问&#xff1f;以下是一个简单的示例&#xff1a; use serde_json::{Value};fn main() {let json_str r#"{"name":"John","age":30,"city":"New York",&q…