Home Java javaTutorial How to define plural numbers in java?

How to define plural numbers in java?

May 22, 2019 am 11:48 AM

How to define plural numbers in java?

java creates a complex number class to perform mathematical operations on complex numbers. The complex numbers have the following format: RealPart ImaginaryPart*i, where i is the square root of -1. The specific requirements are as follows:

(1) Use floating point variables to represent this type of private data. Two constructors are provided, one is used to initialize the object when this type of declaration is made; the other is a parameterless constructor with default values.

(2) Provides operation methods for addition, subtraction and multiplication of two complex numbers.

(3) Print complex numbers in the format (a,b), where a is the real part and b is the imaginary part.

The Java code is as follows:

public class ComplexNumber implements Cloneable {
    private double realPart;  //复数的实部
    private double imaginaryPart;  //复数的虚部

    public ComplexNumber() {  //默认构造函数
        this.realPart = 0.0;
        this.imaginaryPart = 0.0;
    }

    public ComplexNumber(double a, double b) {  //重载构造函数
        this.realPart = a;
        this.imaginaryPart = b;
    }

    /**
     * 复数的加法运算 c = a + b的运算法则是:
     * c.实部 = a.实部 + b.实部
     * c.虚部 = a.虚部 + b.虚部
     */
    public ComplexNumber add(ComplexNumber aComNum) {
        if (aComNum == null) {
            System.err.println("对象不能够为null!");
            return new ComplexNumber();
        }
        return new ComplexNumber(this.realPart + aComNum.getRealPart(), this.imaginaryPart + aComNum.getImaginaryPart());
    }

    /**
     * 复数的减法运算 c = a - b的运算法则是:
     * c.实部 = a.实部 - b.实部
     * c.虚部 = a.虚部 - b.虚部
     */
    public ComplexNumber decrease(ComplexNumber aComNum) {
        if (aComNum == null) {
            System.err.println("对象不能够为null!");
            return new ComplexNumber();
        }
        return new ComplexNumber(this.realPart - aComNum.getRealPart(), this.imaginaryPart - aComNum.getImaginaryPart());
    }

    /**
     * 复数的乘法运算 c = a * b的运算法则是:
     * c.实部 = a.实部 * b.实部 - a.虚部 * b.虚部
     * c.虚部 = a.虚部 * b.实部 + a.实部 * b.虚部
     */
    public ComplexNumber multiply(ComplexNumber aComNum) {
        if (aComNum == null) {
            System.err.println("对象不能够为null!");
            return new ComplexNumber();
        }
        double newReal = this.realPart * aComNum.realPart - this.imaginaryPart * aComNum.imaginaryPart;
        double newImaginary = this.realPart * aComNum.imaginaryPart + this.imaginaryPart * aComNum.realPart;
        ComplexNumber result = new ComplexNumber(newReal, newImaginary);
        return result;
    }

    /**
     * 复数的除法运算 c = a / b 的运算法则是:
     * c.实部 = (a.实部 * b.实部 + a.虚部 * b.虚部) / (b.实部* b.实部 + b.虚部 * b.虚部)
     * c.虚部 = (a.虚部 * b.实部 - a.实部 * b.虚部) / (b.实部 * b.实部 + b.虚部 * b.虚部)
     */
    public ComplexNumber divide(ComplexNumber aComNum) {
        if (aComNum == null) {
            System.err.println("对象不能够为null!");
            return new ComplexNumber();
        }
        if ((aComNum.getRealPart() == 0) && (aComNum.getImaginaryPart() == 0)) {
            System.err.println("除数不能够为0!");
            return new ComplexNumber();
        }
        double temp = aComNum.getRealPart() * aComNum.getRealPart() + aComNum.getImaginaryPart() * aComNum.getImaginaryPart();
        double crealpart = (this.realPart * aComNum.getRealPart() + this.imaginaryPart * aComNum.getImaginaryPart()) / temp;
        double cimaginaryPart = (this.imaginaryPart * aComNum.getRealPart() - this.realPart * aComNum.getImaginaryPart()) / temp;
        return new ComplexNumber(crealpart, cimaginaryPart);
    }

    public String toString() {  //将一个复数显示为字符串
        return this.realPart + " + " + this.imaginaryPart + "i";
    }

    public boolean equals(Object obj) {  //比较一个对象是否和这个复数对象的值相等
        if (obj == null) {
            return false;
        }
        //首先判断a是不是一个复数对象,instanceof关键字是用来判断对象的类型
        if (obj instanceof ComplexNumber) {
            //如果a是复数对象,需要将它强制类型转换成复数对象,才能调用复数类提供的方法
            ComplexNumber b = (ComplexNumber) obj;
            if ((this.realPart == b.getRealPart()) && (this.imaginaryPart == b.getImaginaryPart())) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public int hashCode() {  //获得该复数对象的hashcode
        /**
         * 如果两个复数对象是equals的,那么它们的hashCode也必须相同
         * 两个值相等的复数对象通过toString()方法得到的输出字符串是一样的
         * 于是,可以把得到的字符串的hashCode当作复数对象的hashCode
         */
        return this.toString().hashCode();
    }

    public Object clone() {  //根据现有对象克隆一个新对象
        /**
         * 如果要使自定义的类能够被clone,就必须实现Cloneable接口并且重写它的clone()方法
         * 如果仅仅重写了clone方法而没有在类的声明中添加实现Cloneable接口
         * 调用clone方法时将会出现CloneNotSupportedException异常
         */
        try {
            ComplexNumber newObject = (ComplexNumber) super.clone();
            newObject.setRealPart(this.realPart);
            newObject.setImaginaryPart(this.imaginaryPart);
            return newObject;
        } catch (CloneNotSupportedException e) {  //如果没有实现Cloneable接口,抛出异常

            e.printStackTrace();
            return null;
        }
    }

    public double getImaginaryPart() {  //返回虚部
        return imaginaryPart;
    }

    public void setImaginaryPart(double imaginaryPart) {  //设置虚部
        this.imaginaryPart = imaginaryPart;
    }

    public double getRealPart() {  //返回实部
        return realPart;
    }

    public void setRealPart(double realPart) {  //设置实部
        this.realPart = realPart;
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        ComplexNumber a = new ComplexNumber(20, 15);
        ComplexNumber b = new ComplexNumber(11, 20);
        System.out.println("ComplexNumber a: " + a.toString());
        System.out.println("ComplexNumber b: " + b.toString());
        System.out.println("(a + b) = " + a.add(b).toString());
        System.out.println("(a - b) = " + a.decrease(b).toString());
        System.out.println("(a * b) = " + a.multiply(b).toString());
        System.out.println("(a / b) = " + a.divide(b).toString());
    }
}
Copy after login

Related learning recommendations: java basic tutorial

The above is the detailed content of How to define plural numbers in java?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? Apr 19, 2025 pm 09:51 PM

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...

See all articles