Contest1387 – 【Java程序设计】第二次作业【课程组】

发布于 2024-03-17  533 次阅读


Contest1387 - 【Java程序设计】第二次作业【课程组】

A : [OJ2042] Java习题:声明并测试一个复数类

import java.util.Scanner;
class ComplexNumber {
    private double real;
    private double image;

    //构造方法
    /******** Begin ********/
    public ComplexNumber(double real, double image){
        this.real = real;
        this.image = image;
    }
    /********  End  ********/

    //不带参数的构造方法
    /******** Begin ********/
    public ComplexNumber(){
        this.real = 0.0;
        this.image = 0.0;
    }
    /********  End  ********/

    //修改属性的方法
    /******** Begin ********/
    public void setReal(double real){
        this.real = real;
    }

    public void setImage(double image){
        this.image = image;
    }
    /********  End  ********/

    //读取属性的方法
    /******** Begin ********/
    public double readReal(){
        return real;
    }

    public double readImage(){
        return image;
    }
    /********  End  ********/

    //toString方法
    /******** Begin ********/
    public String toString(){
        if (image == 0){
            return real + "";
        }
        else if (real == 0){
            return image + "i";
        }
        else if (image >0){
            return real + "+" + image + "i";
        }
        else {
            return real + "-" + -image + "i";
        }
    }
    /********  End  ********/

    //加,减,乘三运算的定义,返回一个新的复数,当前复数不改变
    public static ComplexNumber add(ComplexNumber a, ComplexNumber b) {
        /******** Begin ********/
        double creal = a.real + b.real;
        double cimage = a.image + b.image;
        return new ComplexNumber(creal, cimage);
        /********  End  ********/
    }
    public static ComplexNumber substract(ComplexNumber a, ComplexNumber b) {
        /********  Begin ********/
        double creal = a.real - b.real;
        double cimage = a.image - b.image;
        return new ComplexNumber(creal, cimage);
        /********  End  ********/
    }
    public static ComplexNumber multiply(ComplexNumber a, ComplexNumber b) {
        /******** Begin ********/
        double creal = a.real * b.real - a.image *  b.image;
        double cimage = a.real * b.image + a.image *  b.real;
        return new ComplexNumber(creal, cimage);
        /********  End  ********/
    }
    //加,减,乘三运算的定义,对当前对象执行加,减,乘操作
    public void add(ComplexNumber a) {
        /******** Begin ********/
        this.real += a.real;
        this.image += a.image;
        /********  End  ********/
    }
    public void substract(ComplexNumber a) {
        /******** Begin *******/
        this.real -= a.real;
        this.image -= a.image;
        /********  End  ********/
    }
    public void multiply(ComplexNumber a) {
        /******** Begin  ********/
        double creal = this.real * a.real - this.image * a.image;
        double cimage = this.real * a.image + this.image * a.real;
        this.real = creal;
        this.image = cimage;
        /********  End  ********/
    }
}

public class Main {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        //分两行输入两个复数的 实部 虚部
        int a1 = input.nextInt();
        int a2 = input.nextInt();
        int b1 = input.nextInt();
        int b2 = input.nextInt();

        //根据 输入数据 构造 两个 ComplexNumber对象c1,c2
        ComplexNumber c1 = new ComplexNumber(a1, a2);
        ComplexNumber c2 = new ComplexNumber(b1, b2);

        //调用 静态方法add实现c1+c2 输出结果
        ComplexNumber addresult = ComplexNumber.add(c1, c2);
        System.out.println("c1+c2=" + addresult);

        //调用 静态方法substract实现c1-c2 输出结果
        ComplexNumber substractresult = ComplexNumber.substract(c1, c2);
        System.out.println("c1-c2=" + substractresult);

        //调用 静态方法multiply实现c1*c2 输出结果
        ComplexNumber multiplyresult = ComplexNumber.multiply(c1, c2);
        System.out.println("c1*c2=" + multiplyresult);

        //调用c1对象的实例方法add, 将c1+c2,并输出c1的结果
        c1.add(c2);
        System.out.println("c1.add(c2):" + c1);

        //调用c1对象的实例方法substract, 将c1-c2,并输出c1的结果
        c1.substract(c2);
        System.out.println("c1.substract(c2):" + c1);

        //调用c1对象的实例方法multiply,  将c1*c2,并输出c1的结果
        c1.multiply(c2);
        System.out.println("c1.multiply(c2):" + c1);

    }
}