JAVA实现复数的相关操作

每一个点开我的文章的小伙伴大家好!

在发这篇文章的时候,我是一名大二的学生,我将在博客记载我的学习之路!

这是我的第十篇博客文章,是关于JAVA作业的。(嘻嘻,赶在实验课前一天发上来希望能给同学一点思路)

这第二份作业比较基础,是关于通过构造一个复数类,掌握面向对象核心设计思想的封装性、构造方法重载等内容。

题目要求是这样的:

  1. 设计一个复数类,命名为ComplexNumber;
  2. 一个复数是形如a+bi的数,其中a,b均为实数(可用浮点数表示),i为虚数的标识符,例如复数2+3i等;
  3. 所编写的ComplexNumber类应当至少包含2个构造方法,分别为无参的构造方法和有2个参(即给a,b进行传值)的构造方法,并在有参构造方法中使用this()调用无参构造方法;
  4. 所编写的ComplexNumber同时应当有获取和设置a,b两个参数的方法,在方法中使用this关键字访问成员变量;
  5. 所编写的ComplexNumber应当提供复数的加减乘除四则运算法则;

运算法则在这里:

    1. 加法法则:复数的加法按照以下规定的法则进行:设z1=a+bi,z2=c+di是任意两个复数,则它们的和是 (a+bi)+(c+di)=(a+c)+(b+d)i。
    2. 减法法则:复数的减法按照以下规定的法则进行:设z1=a+bi,z2=c+di是任意两个复数,则它们的差是 (a+bi)-(c+di)=(a-c)+(b-d)i。
    3. 乘法法则:规定复数的乘法按照以下的法则进行:设z1=a+bi,z2=c+di是任意两个复数,那么它们的积(a+bi)(c+di)=(ac-bd)+(bc+ad)i。
    4. 除法法则:复数除法定义:满足(c+di)(x+yi)=(a+bi)的复数x+yi(x,y∈R)叫复数a+bi除以复数c+di的商

题目不难所以不多bb直接上代码!

先是封装类的

public class ComplexNumber {
    float a;
    float b;

    public ComplexNumber(float a, float b) {
        this.a = a;
        this.b = b;
    }

     public ComplexNumber() {
    }

    public float getA() {
        return a;
    }

    public void setA(float a) {
        this.a = a;
    }

    public float getB() {
        return b;
    }

    public void setB(float b) {
        this.b = b;
    }



    /*  下面操作的代码有点啰嗦,好处是可以一眼看明白  */


    public ComplexNumber add(ComplexNumber Z1)
    {
            float A=this.a + Z1.a;
            float B=this.b + Z1.b;
            return new ComplexNumber(A,B);
    }

    public ComplexNumber abs(ComplexNumber Z1)
    {
        float A=this.a - Z1.a;
        float B=this.b - Z1.b;
        return new ComplexNumber(A,B);
    }

    public ComplexNumber mul(ComplexNumber Z1)
    {
        float A=this.a* Z1.a-this.b*Z1.b;
        float B=this.b* Z1.a+this.a* Z1.b;
        return new ComplexNumber(A,B);
    }

    public ComplexNumber div(ComplexNumber Z1)
    {
        float A=(this.a* Z1.a+this.b*Z1.b)/ (Z1.a* Z1.a+ Z1.b* Z1.b);
        float B=(this.b* Z1.a-this.a*Z1.b)/ (Z1.a* Z1.a+ Z1.b* Z1.b);
        return new ComplexNumber(A,B);
    }

    @Override
    public String toString() {
        if(b>0)
            return (a+"+"+b+"i");
        if (b<0)
          return (a+""+b+"i");
        return a+",该数虚部为零";
    }
    
}

接着是测试类的

import java.util.Scanner;

public class testComplexNumber {
    public static void main(String[] args) {
        float a=0;
        float b=0;
        System.out.println("输入第一个复数的实部和虚部:");
        Scanner sc=new Scanner(System.in);
        a= sc.nextFloat();
        b= sc.nextFloat();
        ComplexNumber Z1=new ComplexNumber(a,b);
        System.out.println(Z1);

        System.out.println("输入第二个复数的实部和虚部:");
        a= sc.nextFloat();
        b= sc.nextFloat();
        ComplexNumber Z2=new ComplexNumber(a,b);
        System.out.println(Z2);

     
       ComplexNumber add=Z1.add(Z2);
       ComplexNumber abs=Z1.abs(Z2);
       ComplexNumber mul=Z1.mul(Z2);
       ComplexNumber div=Z1.div(Z2);

       System.out.println("我是---------------------------分隔线");

       System.out.println("两个复数相加后的结果为"+add);

       System.out.println("两个复数相减后的结果为"+abs);

       System.out.println("两个复数相乘后的结果为"+mul);

       System.out.println("两个复数相除后的结果为"+div);

    }
}

最后可以试试结果啦!

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java可以通过自定义复数类来实现复数的加减乘除操作。首先,需要定义一个复数类,该类包含实部(real)和虚部(imaginary)两个属性。然后,可以在复数类中定义加法、减法、乘法和除法的方法,根据特定的计算规则进行运算。 以下是一个示例的复数类及其方法实现: ```java public class ComplexNumber { private double real; private double imaginary; public ComplexNumber(double real, double imaginary) { this.real = real; this.imaginary = imaginary; } public ComplexNumber add(ComplexNumber other) { double newReal = this.real + other.real; double newImaginary = this.imaginary + other.imaginary; return new ComplexNumber(newReal, newImaginary); } public ComplexNumber subtract(ComplexNumber other) { double newReal = this.real - other.real; double newImaginary = this.imaginary - other.imaginary; return new ComplexNumber(newReal, newImaginary); } public ComplexNumber multiply(ComplexNumber other) { double newReal = this.real * other.real - this.imaginary * other.imaginary; double newImaginary = this.real * other.imaginary + this.imaginary * other.real; return new ComplexNumber(newReal, newImaginary); } public ComplexNumber divide(ComplexNumber other) { double divisor = Math.pow(other.real, 2) + Math.pow(other.imaginary, 2); double newReal = (this.real * other.real + this.imaginary * other.imaginary) / divisor; double newImaginary = (this.imaginary * other.real - this.real * other.imaginary) / divisor; return new ComplexNumber(newReal, newImaginary); } public String toString() { return String.format("%.1f %.1fi", real, imaginary); } } // 在主函数中调用复数类的方法进行运算 public class Main { public static void main(String[] args) { ComplexNumber c1 = new ComplexNumber(1, 2); ComplexNumber c2 = new ComplexNumber(1, -2); ComplexNumber sum = c1.add(c2); ComplexNumber difference = c1.subtract(c2); ComplexNumber product = c1.multiply(c2); ComplexNumber quotient = c1.divide(c2); System.out.println("复数: " + c1 + " 与复数: " + c2 + " 相加得: " + sum); System.out.println("复数: " + c1 + " 与复数: " + c2 + " 相减得: " + difference); System.out.println("复数: " + c1 + " 与复数: " + c2 + " 相乘得: " + product); System.out.println("复数: " + c1 + " 与复数: " + c2 + " 相除得: " + quotient); } } ``` 以上代码示例中定义了一个ComplexNumber类,包含了加法(add)、减法(subtract)、乘法(multiply)和除法(divide)的方法。在主函数中,创建两个复数对象,然后调用复数类的方法进行加减乘除运算,并打印结果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值