Java练习之复数运算

##声明并测试一个复数类,其方法包括复数的加、减、乘运算。

  • 1、定义了两个私有属性(变量),分别表示复数的实部和虚部,并为每一个属性提供get和set方法,以便外界进行修改复数的值。如果不想让外界修改某属性的值,可以将该属性的访问控制设为私有并且不提供它的set方法。

  • 2、默认构造函数构造一个实部为0,虚部也为0的复数对象,带参数的构造函数允许使用者指定复数对象的实部和虚部的值。

      import java.util.Scanner;
      public class ComplexNumber {
          private double realPart;
          private double imaginaryPart;
          /*默认构造函数*/
          public ComplexNumber() {
      	this.realPart = 0.0;
      	this.imaginaryPart =0.0;
          }
          /*
          * 构造函数
          * @param a 实部
          * @param b 虚部
          * */
          public ComplexNumber(double a, double b) {
      	this.realPart = a;
      	this.imaginaryPart = b;
          }
          /**
           * 复数的加法运算。
           * c = a + b 的运算法则是:
           * c.实部 = a.实部 + b.实部; c.虚部 = a.虚部 + b.虚部
           * @param aComNum 加数
           * @return 加法运算的结果,为一个复数对象
           */
          public ComplexNumber add(ComplexNumber aComNum){
      	if (aComNum == null){
      	    System.out.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.虚部
           * @param aComNum 减数
           * @return 减法运算的结果,为一个复数对象
           */
          public ComplexNumber decrease(ComplexNumber aComNum){
      	if (aComNum == null){
      	    System.out.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.虚部;
           * @param aComNum 乘数
           * @return 乘法运算的结果,为一个复数对象
           */
          public ComplexNumber multiply(ComplexNumber aComNum){
      	if (aComNum == null){
      	    System.out.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;
          }
          /*@return 返回realPart*/
          public double getRealPart() {
      	return realPart;
          }
          /** @param realPart 要设置的 realPart。*/
          public void setRealPart(double realPart) {
      	this.realPart = realPart;
          }
          /*@return 返回imaginaryPart*/
          public double getImaginaryPart() {
      	return imaginaryPart;
          }
          /** @param imaginaryPart 要设置的 imaginaryPart。*/
          public void setImaginaryPart(double imaginaryPart) {
      	this.imaginaryPart = imaginaryPart;
          }
          /** 将一个复数显示为字符串 */
          @Override
          public String toString() {
      	return this.realPart + " + " + this.imaginaryPart + "i";
          }
          /*主函数*/
          public static void main(String[] args) {
      	Scanner scr = new Scanner(System.in);
      		System.out.println("请输入第一个复数的实部和虚部(空格为分隔符)");
      	ComplexNumber c1 = new ComplexNumber(scr.nextInt(),scr.nextInt());
      		System.out.println("请输入第二个复数的实部和虚部(空格为分隔符)");
      	ComplexNumber c2 = new ComplexNumber(scr.nextInt(),scr.nextInt());
      	System.out.println("ComplexNumber a" + c1.add(c1).toString());
      	System.out.println("ComplexNumber b" + c2.add(c2).toString());
      	System.out.println("(a + b) = " + c1.add(c2).toString());
      	System.out.println("(a - b) = " + c1.decrease(c2).toString());
      	System.out.println("(a * b) = " + c1.multiply(c2).toString());
          }
      }
    

一起成长,一起分享相关的学习技术!Fighting!!!
欢迎关注微信公众号:“Coding World”
获取更多相关的技术
扫码关注

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 编写一个类实现复数运算复数类ComplexNumber的属性: m_dRealPart:实部,代表复数的实数部分。 m_dImaginPart:虚部,代表复数的虚数部分。 复数类ComplexNumber的方法: ComplexNumber():构造函数,将实部、虚部都置为0。 ComplexNumber(double r,double i):构造函数,创建复数对象的同时完成复数的实部、虚部的初始化,r为实部的初值,i为虚部的初值。 getRealPart():获得复数对象的实部。 getImaginPart():获得复数对象的虚部。 setRealPart(double d):把当前复数对象的实部设置为给定的形式参数的数字。 setImaginaryPart(double d):把当前复数对象的虚部设置为给定的形式参数的数字。 complexAdd(ComplexNumber c):当前复数对象与形式参数复数对象相加,所得的结果也是复数值,返回给此方法的调用者。 complexAdd(double c):当前复数对象与形式参数实数对象相加,所得的结果仍是复数值,返回给此方法的调用者。 complexMinus(ComplexNumber c) :当前复数对象与形式参数复数对象相减,所得的结果也是复数值,返回给此方法的调用者。 complexMinus(double c) :当前复数对象与形式参数实数对象相减,所得的结果仍是复数值,返回给此方法的调用者。 complexMulti(ComplexNumber c):当前复数对象与形式参数复数对象相乘,所得的结果也是复数值,返回给此方法的调用者。 complexMulti(double c):当前复数对象与形式参数实数对象相乘,所得的结果仍是复数值,返回给此方法的调用者。 toString():把当前复数对象的实部、虚部组合成a+bi的字符串形式,其中和分别为实部和虚部的数据。 2. 编写Java Application程序使用上题定义的类,检查类定义是否正确。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值