【JAVA】实验二 类与对象

news2024/10/5 17:12:30

实验名称    实验二 类与对象

实验目的   

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);
    }

    @Override
    public 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);
    }

    @Override
    public 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.coloradmin.cn/o/1350656.html

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

相关文章

测试自动创建设备节点的功能

一. 简介 上一篇文章在 新设备驱动框架代码的基础上&#xff0c;添加了自动创建设备节点的代码。文章地址如下&#xff1a; 自动创建设备节点代码的实现-CSDN博客 本文对自动创建设备节点的功能进行测试。 二. 自动创建设备节点代码的测试 1. 编译驱动&#xff0c;并拷贝…

Python 数据库(一):使用 mysql-connector-python 操作 MySQL 数据库

大家好&#xff0c;我是水滴~~ 当涉及到使用 Python 操作 MySQL 数据库时&#xff0c;mysql-connector-python 库是一个强大而常用的选择。该库提供了与 MySQL 数据库的交互功能&#xff0c;使您能够执行各种数据库操作&#xff0c;如连接数据库、执行查询和插入数据等。在本文…

2024年美赛数学建模ABCDEF题思路选题分析

文章目录 1 赛题思路2 美赛比赛日期和时间3 赛题类型4 美赛常见数模问题5 建模资料 1 赛题思路 (赛题出来以后第一时间在CSDN分享) https://blog.csdn.net/dc_sinor?typeblog 2 美赛比赛日期和时间 比赛开始时间&#xff1a;北京时间2024年2月2日&#xff08;周五&#xff…

[笔记] GICv3/v4 ITS 与 LPI

0. 写在前面 由于移植一个 pcie 设备驱动时&#xff0c;需要处理该 pcie 设备的 msi 中断(message signaled interrup)。 在 ARM 中&#xff0c; ARM 建议 msi 中断实现方式为&#xff1a; pcie 设备往 cpu 的一段特殊内存&#xff08;寄存器&#xff09;写某一个值&#xff0…

浅谈开关量信号隔离器在钢铁厂除鳞系统的应用-安科瑞 蒋静

摘要&#xff1a;在钢铁生产线中&#xff0c;轧制是其中一项重要的加工工艺。通过轧制将金属坯料进行延展和定型&#xff0c;满足不同行业的使用要求。在轧制前需要进行除鳞&#xff0c;除鳞系统是通过高压水形成扇形水束&#xff0c;喷射到钢坯表面将氧化铁层剥离。高压水由高…

精益生产敏捷实践手册:软件行业的精益转型之路——张驰咨询

精益生产培训的内容相当广泛&#xff0c;涵盖创立精益的理念、工具、执行策略和管理方法。下面将详细介绍各种培训内容以及它们的作用&#xff1a; 理念建设 精益生产基本概念&#xff1a;什么是精益&#xff0c;它的历史和核心理念等。 组织变革管理&#xff1a;怎样在组织…

数据库攻防学习之Redis

Redis 0x01 redis学习 在渗透测试面试或者网络安全面试中可能会常问redis未授权等一些知识&#xff0c;那么什么是redis&#xff1f;redis就是个数据库&#xff0c;常见端口为6379&#xff0c;常见漏洞为未授权访问。 0x02 环境搭建 这里可以自己搭建一个redis环境&#xf…

2024网络安全趋势—— “双刃剑”效应带来全新冲击和挑战

“生成式AI”正以前所未有的方式影响着人们的生活和工作方式。 在网络安全方面&#xff0c;这项技术也正深刻改变着对抗形态和攻防模式&#xff0c;其在打开人类认知世界新路径的同时&#xff0c;也成为黑客开展网络攻击的“利器”。随着生成式AI的深入发展&#xff0c;“双刃…

别划走!3分钟看懂 Git 底层工作原理

这是一篇能让你迅速了解 Git 工作原理的文章&#xff0c;实战案例解析&#xff0c;相信我&#xff0c;3 分钟&#xff0c;绝对能够有收获&#xff01; Git 目录结构 Git 的本质是一个文件系统&#xff08;很重要&#xff0c;记住这句话&#xff0c;理解这句话&#xff09;&am…

oracle-undo

tips&#xff1a;串行化隔离级别&#xff1a;事务开始后&#xff0c;对一张表不会被别人影响&#xff0c;对于审计工作比较有用&#xff0c;避免了幻读。 undo表空间&#xff1a;自动生成段&#xff0c;自动生成区&#xff0c;自动维护的&#xff0c;不像一般的表空间&#xff…

【响应式编程-01】Lambda表达式初体验

一、简要描述 Lambda初体验Lambda表达式的语法格式Lambda表达式应用举例Lambda表达式底层实现 二、什么是Lambda表达式 Java8新特性&#xff0c;来源于数学中的λ[l:mdə]演算 是一套关于函数(f(x))定义、输入量、输出量的计算方案 Lambda表达式 -> 函数 使代码变得简洁…

智慧园区物联综合管理平台之架构简述

总体架构 系统总体划分为物联感知系统层、 核心平台层、 综合运营服务平台和展示层四部分。 物联感知系统层 物联感知系统主要是支撑园区智能化运行的各子系统, 包括门禁系统、 视频监控系统、 车辆管理系统等。 核心平台层 核心平台层包括: 园区物联综合管理平台和园区…

6.4 通过IO实现文件的读取与写入

6.4 通过IO实现文件的读取与写入 1. File类及常用方法2. 通过字节字符流实现文件读取与写入1. 流2. 字节输入流 InputStream 3.2. 1. File类及常用方法 package com.imooc.io;import java.io.File; import java.io.IOException;public class FileSample {public static void ma…

Premiere分屏特效图文内容幻灯片展示视频素材PR模板下载

Premiere Pro 模板&#xff0c;多屏幕内容展示PR幻灯片模板&#xff0c;分屏特效图文视频素材pr模板下载。 这是一个高质量、组织良好且易于自定义的视频剪辑模板。只需替换图像或视频&#xff0c;编辑文本&#xff0c;添加音频&#xff0c;微微调整即可&#xff01; 来自PR模板…

Java经典框架之SpringBoot

SpringBoot Java 是第一大编程语言和开发平台。它有助于企业降低成本、缩短开发周期、推动创新以及改善应用服务。如今全球有数百万开发人员运行着超过 51 亿个 Java 虚拟机&#xff0c;Java 仍是企业和开发人员的首选开发平台。 课程内容的介绍 1. SpringBoot基础 2. Spring…

GraalVM Native学习及使用

概述 在开发Spring Boot 应用或者其他JAVA程序的过程中&#xff0c;启动慢、内存占用大是比较头疼的问题&#xff0c;往往需要更多的资源去部署&#xff0c;成本大幅提高。为了优化上述问题&#xff0c;常常使用优化程序、使用更小消耗的JVM、使用容器等措施。 现在有一个叫做…

权限修饰符和代码块

权限修饰符&#xff1a;是用来控制一个成员能够被访问的范围的。、 可以修饰成员变量、方法、构造方法、内部类。 权限修饰符的范围 权限修饰符的使用规则&#xff1a; 实际开发中&#xff0c;一般只用private和public 成员变量私有 方法公开 特例&#xff1a;如果方法中的…

Debezium日常分享系列之:向 Debezium 连接器发送信号

Debezium日常分享系列之&#xff1a;向 Debezium 连接器发送信号 一、概述二、激活源信号通道三、信令数据集合的结构四、创建信令数据集合五、激活kafka信号通道六、数据格式七、激活JMX信号通道八、自定义信令通道九、Debezium 核心模块依赖项十、部署自定义信令通道十一、信…

前端文件上传组件最全封装+删除+下载+预览

前言&#xff1a;使用的是若依的框架element uivue2封装的。如果有不对的地方欢迎指出。后台管理使用&#xff0c;文件需要上传。回显列表&#xff0c;详情也需要回显预览 // 开始封装组件&#xff1a;封装在 src/components/FileUpload/index.vue中 <template><div c…

诺派克ROPEX热封控制器维修RES-408/400VAC

德国希尔科诺派克ROPEX热封控制器维修型号包括&#xff1a;RES-401&#xff0c;RES-402&#xff0c;RES-403&#xff0c;RES-406&#xff0c;RES-407&#xff0c;RES-408&#xff0c;RES-409&#xff0c;RES-420&#xff0c;RES-440&#xff0c;MSW-2&#xff0c;PEX-W3&#x…