实现复数的加减乘除(构造函数的妙用)

在复数类中,加减乘除各用两种方法实现。举个例子,就比如a = b + c和a += b;

在该复数类中,用到了static修饰符,如此,被修饰的方法可以用类名通过圆点运算符直接调用。可以解决没有对象调用的问题。用类名直接调用,传入两个参数。

减法利用加法实现,除法利用乘法实现。

在除法中,如果除数为0,则会抛出异常。

编写了四种构造方法:无参,单参,双参以及直接传入一个复数。使用this()的方法调用双参构造函数完成无参和单参构造。

并实现了getter和setter方法,以便取得和注入初值。

重载了toString方法,自定义输出形式。

可以实现复数的加减乘除。因为有a += b这类方法,使得不光可以实现两个复数的运算,还可以实现连加,连乘等操作。

具体代码如下:

/*
 * @auther-yc
 * 2018/9/23
 */
package com.mec.complex;

public class Complex {
	//定义两个全局变量,分别表示复数的实部和虚部;
	private double row;
	private double col;
	
	//无参构造
	public Complex() {
		//使用this()方法调用双参构造,使得默认初始值为0.0 + 0.0i;
		this(0.0, 0.0);
	}

	public Complex(double row) {
		this(row, 0.0);
	}
	
	public Complex(Complex complex) {
		this(complex.row, complex.col);
	}
	
	public Complex(double row, double col) {
		this.setRow(row);
		this.setCol(col);
	}
	
	//实现一系列的getter和setter方法;
	public double getRow() {
		return row;
	}

	public void setRow(double row) {
		this.row = row;
	}

	public double getCol() {
		return col;
	}

	public void setCol(double col) {
		this.col = col;
	}

	//该方法可取得一个复数的相反数,这样就可以利用加法来实现减法运算。
	public static Complex opposite(Complex complex) {
		complex.row = - complex.row;
		complex.col = - complex.col;
		
		return complex;
	}
	
	//该方法可取的一个复数的倒数,这样就可以利用乘法来实现除法运算。
	public static Complex reciprocal(Complex complex) {
		double row = (complex.row) / (complex.row * complex.row + complex.col * complex.col);
		double col = (-complex.col) / (complex.row * complex.row + complex.col * complex.col);
		
		return new Complex(row, col);
	}
	
	//a = b + c;将该方法定义为静态,可以使用类名直接调用,然后输出结果。不用新定义一个复数去接收其和值;
	//且该方法实质上是用其中一个参数调用单参的加法实现。
	public static Complex add(Complex one, Complex another) {
		return new Complex(one).add(another);
	}
	
	//a += b;
	public Complex add(Complex one) {
		this.row += one.row;
		this.col += one.col;
		
		return this;
	}
	
	//a = b - c;
	public static Complex sub(Complex one, Complex another) {
		return new Complex(one).add(opposite(another));
	}
	
	public Complex sub(Complex complex) {
		return this.add(opposite(complex));
	}
	
	//a = b * c;
	public static Complex mul(Complex one, Complex another) {
		return new Complex(one).mul(another); 
	}
	
	//a *= b;
	public Complex mul(Complex complex) {
		double row = this.row;
		this.row = this.row * complex.row - this.col * complex.col;
		this.col = row * complex.col + this.col * complex.row;
		
		return this;
	}
	
	//a /= b;此处抛出自定义的异常“除零错”,当传入的参数作为除数运算是就会报异常!
	public Complex div(Complex complex) throws DivideZeroException {	
		if((Math.abs(complex.row) < 1e-6) && (Math.abs(complex.col) < 1e-6)) {
			throw new DivideZeroException("除零错");
		}
		return new Complex(this.mul(reciprocal(complex)));
	}
	
	//a = b / c;
	public static Complex div(Complex one, Complex another) throws DivideZeroException {
		if((Math.abs(another.row) < 1e-6) && (Math.abs(another.col) < 1e-6)) {
			throw new DivideZeroException("除零错");
		}
		return new Complex(one).mul(reciprocal(another));
	}
	
	//重载toString方法,可以按自定义的格式输出;
	@Override
	public String toString() {
		return "(" + row + ", " + col + ")";
	}
	
}



/*
 * 异常类
 * @auther-yc
 * 2018/9/23
 */
package com.mec.complex;

public class DivideZeroException extends Exception {
	private static final long serialVersionUID = -917710262683840175L;

	public DivideZeroException() {
	}

	public DivideZeroException(String arg0, Throwable arg1, boolean arg2, boolean arg3) {
		super(arg0, arg1, arg2, arg3);
	}

	public DivideZeroException(String arg0, Throwable arg1) {
		super(arg0, arg1);
	}

	public DivideZeroException(String arg0) {
		super(arg0);
	}

	public DivideZeroException(Throwable arg0) {
		super(arg0);
	}
	
}


 

 

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮您解决这个问题。 首先,我们需要创建一个Java图形界面来获取两个复数的实部和虚部。您可以使用Java Swing来创建这个GUI。 下面是一个简单的GUI示例代码: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ComplexCalculator extends JFrame implements ActionListener { private JLabel real1Label, imaginary1Label, real2Label, imaginary2Label; private JTextField real1Field, imaginary1Field, real2Field, imaginary2Field; private JButton addButton, subtractButton, multiplyButton, divideButton; private JLabel resultLabel; public ComplexCalculator() { // 设置窗口标题和大小 setTitle("复数计算器"); setSize(400, 300); // 创建GUI控件并添加到窗口 JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints constraints = new GridBagConstraints(); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.insets = new Insets(5, 5, 5, 5); real1Label = new JLabel("第一个复数的实部:"); constraints.gridx = 0; constraints.gridy = 0; panel.add(real1Label, constraints); real1Field = new JTextField(10); constraints.gridx = 1; constraints.gridy = 0; panel.add(real1Field, constraints); imaginary1Label = new JLabel("第一个复数的虚部:"); constraints.gridx = 0; constraints.gridy = 1; panel.add(imaginary1Label, constraints); imaginary1Field = new JTextField(10); constraints.gridx = 1; constraints.gridy = 1; panel.add(imaginary1Field, constraints); real2Label = new JLabel("第二个复数的实部:"); constraints.gridx = 0; constraints.gridy = 2; panel.add(real2Label, constraints); real2Field = new JTextField(10); constraints.gridx = 1; constraints.gridy = 2; panel.add(real2Field, constraints); imaginary2Label = new JLabel("第二个复数的虚部:"); constraints.gridx = 0; constraints.gridy = 3; panel.add(imaginary2Label, constraints); imaginary2Field = new JTextField(10); constraints.gridx = 1; constraints.gridy = 3; panel.add(imaginary2Field, constraints); addButton = new JButton("加"); addButton.addActionListener(this); constraints.gridx = 0; constraints.gridy = 4; panel.add(addButton, constraints); subtractButton = new JButton("减"); subtractButton.addActionListener(this); constraints.gridx = 1; constraints.gridy = 4; panel.add(subtractButton, constraints); multiplyButton = new JButton("乘"); multiplyButton.addActionListener(this); constraints.gridx = 0; constraints.gridy = 5; panel.add(multiplyButton, constraints); divideButton = new JButton("除"); divideButton.addActionListener(this); constraints.gridx = 1; constraints.gridy = 5; panel.add(divideButton, constraints); resultLabel = new JLabel(""); constraints.gridx = 0; constraints.gridy = 6; constraints.gridwidth = 2; panel.add(resultLabel, constraints); add(panel); // 设置窗口可见性和关闭操作 setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { double real1 = Double.parseDouble(real1Field.getText()); double imaginary1 = Double.parseDouble(imaginary1Field.getText()); double real2 = Double.parseDouble(real2Field.getText()); double imaginary2 = Double.parseDouble(imaginary2Field.getText()); if (e.getSource() == addButton) { ComplexNumber result = ComplexNumber.add(new ComplexNumber(real1, imaginary1), new ComplexNumber(real2, imaginary2)); resultLabel.setText("结果为:" + result.toString()); } else if (e.getSource() == subtractButton) { ComplexNumber result = ComplexNumber.subtract(new ComplexNumber(real1, imaginary1), new ComplexNumber(real2, imaginary2)); resultLabel.setText("结果为:" + result.toString()); } else if (e.getSource() == multiplyButton) { ComplexNumber result = ComplexNumber.multiply(new ComplexNumber(real1, imaginary1), new ComplexNumber(real2, imaginary2)); resultLabel.setText("结果为:" + result.toString()); } else if (e.getSource() == divideButton) { ComplexNumber result = ComplexNumber.divide(new ComplexNumber(real1, imaginary1), new ComplexNumber(real2, imaginary2)); resultLabel.setText("结果为:" + result.toString()); } } public static void main(String[] args) { ComplexCalculator calculator = new ComplexCalculator(); } } ``` 上述代码创建了一个Java Swing窗口,其中包含四个文本框用于输入两个复数的实部和虚部,以及四个按钮用于执行加、减、乘和除操作。当用户单击这些按钮时,将调用`actionPerformed`方法来执行相应的操作并显示结果。 接下来,您需要实现`ComplexNumber`类来执行复数加减乘除操作。下面是一个示例实现: ```java public class ComplexNumber { private double real; private double imaginary; public ComplexNumber(double real, double imaginary) { this.real = real; this.imaginary = imaginary; } public static ComplexNumber add(ComplexNumber c1, ComplexNumber c2) { return new ComplexNumber(c1.real + c2.real, c1.imaginary + c2.imaginary); } public static ComplexNumber subtract(ComplexNumber c1, ComplexNumber c2) { return new ComplexNumber(c1.real - c2.real, c1.imaginary - c2.imaginary); } public static ComplexNumber multiply(ComplexNumber c1, ComplexNumber c2) { double real = c1.real * c2.real - c1.imaginary * c2.imaginary; double imaginary = c1.real * c2.imaginary + c1.imaginary * c2.real; return new ComplexNumber(real, imaginary); } public static ComplexNumber divide(ComplexNumber c1, ComplexNumber c2) { double denominator = c2.real * c2.real + c2.imaginary * c2.imaginary; double real = (c1.real * c2.real + c1.imaginary * c2.imaginary) / denominator; double imaginary = (c1.imaginary * c2.real - c1.real * c2.imaginary) / denominator; return new ComplexNumber(real, imaginary); } public String toString() { if (imaginary >= 0) { return real + "+" + imaginary + "i"; } else { return real + "" + imaginary + "i"; } } } ``` 上述代码定义了一个`ComplexNumber`类,用于表示复数并执行加、减、乘和除操作。这个类包含一个构造函数和四个静态方法,分别用于执行加、减、乘和除操作。这个类还包含一个`toString`方法,用于将复数转换为字符串表示。 现在,您可以将这两个类组合在一起,创建一个Java应用程序,用于执行复数加减乘除操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值