日撸 Java 三百行day31

文章目录

  • day31 整数矩阵及其运算
    • 面向对象思想
    • java异常处理
    • java中的getter和setter方法
    • 代码

day31 整数矩阵及其运算

面向对象思想

结合之前day7和day8面向过程开发,只关注了矩阵加法和矩阵乘法的功能。而day31是面向对象开发,一个矩阵类,在这个类对象中包含有矩阵的加法,乘法,获取数据等功能(如add,multiply方法)。同时通过get,set方法来让用户通过方法获取类相关数据(getData,getRows,getColumns,setValue等),而非直接获取数据。在IntMatrix类,方法名为add的有两个,但这两个方法的区别在于传参以及返回不同,这体现了方法重载。
面向对象的三大特点(定义的描述来自百度):

  • 封装:
    (1)定义:将数据和对数据的操作封装在一个对象内部,对外部隐藏对象的实现细节,保证了程序的安全性和可靠性。
    (2)IntMatrix类就体现了封装性,将数据和相关的操作封装在对象内部,我们对外只提供相应饿方法,如我们调用矩阵相乘就可以直接调用multiply方法即可
  • 继承
    (1)定义:通过定义父类和子类,子类可以继承父类的属性和方法,减少代码重复,提高代码的可维护性
  • 多态
    (1)定义:同一个方法可以根据不同的对象调用不同的实现方式,从而提高代码的灵活性和可扩展性。多态一般是通过继承或接口来实现的

java异常处理

在之前写哈夫曼树时已经涉及异常处理了,java的异常处理方法

  • try-catch-finally: 将可能要出现异常的代码放入try中,catch 捕获 try 中的异常,并处理,不管有没有异常,finally中的代码都会执行。(finally不是必须)

  • throw: 一般是语句抛出一个异常, 一般是手动抛出,并且可以抛出更为明确的异常

  • throws:一般是方法抛出一个异常,在方法后面声明异常(表示该方法可能会产生异常)

java中的getter和setter方法

一般在创建java实体类时,会把类相关属性设置为私有private(这是从安全角度去考虑),想要获取或设置这些私有属性可以通过方法去获取或设置,即getXXX,setXXX,而不是直接去操作这一个变量。这也体现了java的一大特点:封装性。
访问权限修饰符(private,procted,public,default)不同的访问权限,访问的范围不一样(从网上找了一个这样的图)
在这里插入图片描述
在项目中使用lombok可以减少写getter/setter/toString等方法的编写

代码

package matrix;
import java.util.Arrays;
public class IntMatrix {
    int[][] data;

    /**
     * The first constructor.
     * @param paraRows The number of rows
     * @param paraColumns  The number of columns
     */
    public IntMatrix(int paraRows, int paraColumns){
        data = new int[paraRows][paraColumns];
    }

    /**
     * The second constructor. Construct a copy of the given matrix.
     * @param paraMatrix The given matrix.
     */
    public IntMatrix(int[][] paraMatrix){
        data = new int[paraMatrix.length][paraMatrix[0].length];
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[0].length; j++) {
                data[i][j] = paraMatrix[i][j];
            }
        }
    }

    /**
     * The third constructor. Construct a copy of the given matrix.
     * @param paraMatrix The given matrix.
     */
    public IntMatrix(IntMatrix paraMatrix) {
        this(paraMatrix.getData());
    }

    /**
     * Get identity matrix. The values at the diagonal are all 1
     * @param paraRows
     * @return
     */
    public static IntMatrix getIdentityMatrix(int paraRows) {
        IntMatrix resultMatrix = new IntMatrix(paraRows, paraRows);
        for (int i = 0; i < paraRows; i++) {
            // According to access control, resultMatrix.data can be visited
            resultMatrix.data[i][i] = 1;
        }
        return resultMatrix;
    }

    /**
     * Overrides the method claimed in Object, the superclass of any class.
     * @return
     */
    @Override
    public String toString() {
        return Arrays.deepToString(data);
    }

    /**
     * Get my data. Warning, the reference to the data instead of a copy of the data is returned.
     * @return
     */
    public int[][] getData() {
        return data;
    }

    public int getRows() {
        return data.length;
    }

    public int getColumns() {
        return data[0].length;
    }

    /**
     * Set one the value of one element.
     * @param paraRow  The row of the element.
     * @param paraColumn The column of the element.
     * @param paraValue  The new value.
     */
    public void setValue(int paraRow, int paraColumn, int paraValue){
        data[paraRow][paraColumn] = paraValue;
    }

    /**
     * Get the value of one element.
     * @param paraRow The row of the element.
     * @param paraColumn The column of the element.
     * @return
     */
    public int getValue(int paraRow, int paraColumn) {
        return data[paraRow][paraColumn];
    }

    /**
     * Add another matrix to me.
     * @param paraMatrix  The other matrix.
     * @throws Exception
     */
    public void add(IntMatrix paraMatrix) throws Exception {
        // Step 1. Get the data of the given matrix.
        int[][] tempData = paraMatrix.getData();

        // Step 2. Size check.
        if (data.length != tempData.length) {
            throw new Exception("Cannot add matrices. Rows not match: " + data.length + " vs. "
                    + tempData.length + ".");
        }
        if (data[0].length != tempData[0].length) {
            throw new Exception("Cannot add matrices. Rows not match: " + data[0].length + " vs. "
                    + tempData[0].length + ".");
        }

        // Step 3. Add to me.
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[0].length; j++) {
                data[i][j] += tempData[i][j];
            }
        }
    }

    /**
     * Add two existing matrices.
     * @param paraMatrix1 The first matrix.
     * @param paraMatrix2 The second matrix.
     * @return A new matrix.
     * @throws Exception
     */
    public static IntMatrix add(IntMatrix paraMatrix1, IntMatrix paraMatrix2) throws Exception {
        // Step 1. Clone the first matrix.
        IntMatrix resultMatrix = new IntMatrix(paraMatrix1);

        // Step 2. Add the second one.
        resultMatrix.add(paraMatrix2);

        return resultMatrix;
    }


    /**
     * Multiply two existing matrices.
     * @param paraMatrix1 The first matrix.
     * @param paraMatrix2 The second matrix.
     * @return A new matrix.
     * @throws Exception
     */
    public static IntMatrix multiply(IntMatrix paraMatrix1, IntMatrix paraMatrix2) throws Exception {
        // Step 1. Check size.
        int[][] tempData1 = paraMatrix1.getData();
        int[][] tempData2 = paraMatrix2.getData();
        if (tempData1[0].length != tempData2.length) {
            throw new Exception("Cannot multiply matrices: " + tempData1[0].length + " vs. "
                    + tempData2.length + ".");
        }

        // Step 2. Allocate space.
        int[][] resultData = new int[tempData1.length][tempData2[0].length];

        // Step 3. Multiply.
        for (int i = 0; i < tempData1.length; i++) {
            for (int j = 0; j < tempData2[0].length; j++) {
                for (int k = 0; k < tempData1[0].length; k++) {
                    resultData[i][j] += tempData1[i][k] * tempData2[k][j];
                }
            }
        }

        // Step 4. Construct the matrix object.
        IntMatrix resultMatrix = new IntMatrix(resultData);

        return resultMatrix;
    }

    public static void main(String args[]) {
        IntMatrix tempMatrix1 = new IntMatrix(3, 3);
        tempMatrix1.setValue(0, 1, 1);
        tempMatrix1.setValue(1, 0, 1);
        tempMatrix1.setValue(1, 2, 1);
        tempMatrix1.setValue(2, 1, 1);
        System.out.println("The original matrix is: " + tempMatrix1);

        IntMatrix tempMatrix2 = null;
        try {
            tempMatrix2 = IntMatrix.multiply(tempMatrix1, tempMatrix1);
        } catch (Exception ee) {
            System.out.println(ee);
        }
        System.out.println("The square matrix is: " + tempMatrix2);

        IntMatrix tempMatrix3 = new IntMatrix(tempMatrix2);
        try {
            tempMatrix3.add(tempMatrix1);
        } catch (Exception ee) {
            System.out.println(ee);
        }
        System.out.println("The connectivity matrix is: " + tempMatrix3);
    }
}

在这里插入图片描述

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

傅盛“追风”GPT,猎户星空春天来了?

GPT的横空出世&#xff0c;让冷清已久的商用服务机器人市场&#xff0c;又有了“新故事”。 从技术底层逻辑而言&#xff0c;服务机器人受到这类新技术的影响会更为明显。因为抛开硬件&#xff0c;服务机器人的内核其实就是AI&#xff0c;GPT大模型的出现显然成了现阶段该产业进…

KDSL-82轻型升流器

一、产品概述 KDSL-82 1000A大电流发生器是一种作为检验用的电流源&#xff0c;大电流试验器采用ARM芯片控制输出工艺和大容量的环形变压器&#xff0c;并且配有液晶屏显示的表计&#xff0c;同时显示一、二次电流、变比和秒表接点(或电位)的动作时间。外配铝合金机箱&#xff…

Mybatis核心

文章目录前言一、Configuration二、MappedStatement三、SqlSession四、Executor五、StatementHandler六、ParameterHandler七、ResultSetHandler八、TypeHandler总结前言 SqlSession是MyBatis提供的面向用户的操作数据库API。那么MyBatis底层是如何工作的呢&#xff1f;为了解…

SpringCloud-Gateway实现网关

网关作为流量的入口&#xff0c;常用的功能包括路由转发、权限校验、限流等Spring Cloud 是Spring官方推出的第二代网关框架&#xff0c;由WebFluxNettyReactor实现的响应式的API网关&#xff0c;它不能在传统的servlet容器工作&#xff0c;也不能构建war包。基于Filter的方式提…

​破除“内卷”,什么才是高阶智能座舱更优方案?

下一代智能座舱雏形已现。 从多屏互动到舱内全场景交互&#xff0c;从中控娱乐快速延伸到更多元化的车内娱乐平台&#xff1b;越来越多元化功能集中上车&#xff0c;座舱空间的营造&#xff08;包括氛围灯、香氛等&#xff09;以及AR技术的应用等等&#xff0c;开始深刻影响着…

关于ROS机器人-文心一言和CatGPT怎么看-

交流截图&#xff1a; 文字版本如下&#xff08;W-文心&#xff1b;C-猿如意&#xff09;&#xff1a; 如何通过蓝桥云课学习ROS机器人&#xff1f; W&#xff1a; 如果你想通过蓝桥云课学习ROS机器人&#xff0c;可以按照以下步骤进行&#xff1a; 确认ROS机器人的版本和教…

能自动翻译的软件-最精准的翻译软件

批量翻译软件是一种利用自然语言处理技术和机器学习算法&#xff0c;可以快速翻译大量文本内容的工具。批量翻译软件可以处理多种格式的文本&#xff0c;包括文档、网页、邮件、PDF等等&#xff0c;更符合掌握多语言的计算机化需求。 147CGPT翻译软件特点&#xff1a; 1.批量任…

opencv:介绍 SIFT(尺度不变特征变换)及其使用(一)

在本章中 我们将了解 SIFT 算法的概念 我们将学习如何找到 SIFT 关键点和描述符。 理论 在过去的几章中,我们了解了一些角点检测器,如 Harris 等。它们具有旋转不变性,这意味着即使图像旋转,我们也可以找到相同的角点。这是显而易见的,因为旋转后的图像中的角点仍然是角点…

(链表专题) 328. 奇偶链表 ——【Leetcode每日一题】

328. 奇偶链表 给定单链表的头节点 head &#xff0c;将所有索引为奇数的节点和索引为偶数的节点分别组合在一起&#xff0c;然后返回重新排序的列表。 第一个 节点的索引被认为是 奇数 &#xff0c; 第二个 节点的索引为 偶数 &#xff0c;以此类推。 请注意&#xff0c;偶…

腾讯云轻量应用服务器搭建网站教程(WordPress为例)

腾讯云轻量应用服务器搭建WordPress网站教程&#xff0c;先安装WordPress应用镜像&#xff0c;然后远程连接轻量应用服务器获取WP用户名和密码&#xff0c;域名DNS解析到轻量服务器IP地址&#xff0c;登陆WordPress后台管理全过程&#xff0c;腾讯云百科来详细说下腾讯云轻量服…

弹塑性力学--应变硬化

在单轴拉伸试验中&#xff0c;当应力超过屈服强度后&#xff0c;需要施加更大的载荷产生更大的应力&#xff0c;才会使材料发生更多的塑性变形。随着塑性应变的增加&#xff0c;材料变得更强、更难以变形了&#xff0c;因此这个阶段称为“应变硬化”&#xff08;Strain Hardeni…

Vue语法糖<script setup>详解,用最快的方式让你看懂和<script>的区别

前言 Vue3出来已经3年了&#xff0c;但是前两天在百度上搜索有关setup语法糖的细节时&#xff0c;发现很多博客关于语法糖细节部分&#xff0c;还是讲的很粗糙&#xff0c;因此决定自己来写一篇入门的博客&#xff0c;方便大家快速上手。 <script setup>简介 它是Vue3…

项目沟通管理流程的6大规范步骤

1、建立沟通计划 需要对整个项目的沟通对象、沟通内容、沟通频率、沟通方法等各方面&#xff0c;进行计划和安排。尤其需明确沟通机制&#xff0c;建立完整的沟通计划。并根据项目沟通的具体情况&#xff0c;实时添加和修订计划&#xff0c;以保证沟通管理计划的持续适用性。 项…

拐点!智能座舱破局2023

“这是我们看到的整个座舱域控渗透率&#xff0c;2022年是8.28%&#xff0c;主力的搭载车型仍然是30-35万区间。”3月29日&#xff0c;2023年度&#xff08;第五届&#xff09;高工智能汽车市场峰会上&#xff0c;高工智能汽车研究院首发《2022-2025年中国智能汽车产业链市场数…

每日学术速递4.11

CV - 计算机视觉 | ML - 机器学习 | RL - 强化学习 | NLP 自然语言处理 Subjects: cs.CV 1.InstantBooth: Personalized Text-to-Image Generation without Test-Time Finetuning 标题&#xff1a;InstantBooth&#xff1a;无需测试时间微调的个性化文本到图像生成 作者&a…

HTTP HTTPS简介

一篇文章带你走进HTTP HTTPS场景复现核心干货HTTP/HTTPS简介&#xff08;简单比较&#xff09;HTTP工作原理HTTPS作用场景复现 最近在对前端的深入学习过程中&#xff0c;接触到了与网络请求相关的内容&#xff0c;于是打算出一个专栏&#xff0c;从HTTP与HTTPS入手&#xff0…

基于t-SNE的Digits数据集降维与可视化

基于t-SNE的Digits数据集降维与可视化 描述 t-SNE(t-分布随机邻域嵌入)是一种基于流形学习的非线性降维算法&#xff0c;非常适用于将高维数据降维到2维或者3维&#xff0c;进行可视化观察。t-SNE被认为是效果最好的数据降维算法之一&#xff0c;缺点是计算复杂度高、占用内存…

运行时内存数据区之程序计数器

内存是非常重要的系统资源&#xff0c;是硬盘和CPU的中间仓库及桥梁&#xff0c;承载着操作系统和应用程序的实时选行。JVM内存布局规定了Java在运行过程中内存申请、分配、管理的策略&#xff0c;保证了JVM的高效稳定运行。 不同的VM对于内存的划分方式和管理机制存在着部分差…

英特尔CEO基辛格:开创可持续计算新时代

近日&#xff0c;帕特基辛格作为英特尔CEO后&#xff0c;第一次来华访问。在2023英特尔可持续发展高峰论坛上&#xff0c;笔者有幸聆听他的演讲。他严谨又不乏幽默地给我们分享了英特尔如何践行可持续发展的思考和举措。 基辛格表示&#xff0c;身处科技行业&#xff0c;我们不…

DeepFM论文翻译

1.摘要 为了最大化推荐系统的CTR&#xff0c;学习用户行为的复杂交叉特征很关键。 尽管有很大进步&#xff0c;现有的方法无论对低阶还是高阶的交叉特征&#xff0c;似乎还是有很强的bias, 或者需要专门的特征工程。 本文&#xff0c;我们证明了得出一个能强化高阶和低阶交叉特…
最新文章