Problem: 4.17


 Question 4.17:Write a Java class Complex for dealing with complex number. Your class must have the following features:

• Instance variables :

o realPart for the real part of type double

o imaginaryPart for imaginary part of type double.

• Constructor:

o public Complex (): A default constructor, it should initialize the number to 0, 0)

o public Complex (double realPart, double imaginaryPart): A constructor with

parameters, it creates the complex object by setting the two fields to the passed values.

• Instance methods:

o public Complex add (Complex otherNumber): This method will find the sum of the current complex number and the passed complex number. The methods returns a new Complex number which is the sum of the two.

o public Complex subtract (Complex otherNumber): This method will find the

difference of the current complex number and the passed complex number. The

methods returns a new Complex number which is the difference of the two.

o public Complex multiply (Complex otherNumber): This method will find the

product of the current complex number and the passed complex number. The methods returns a new Complex number which is the product of the two.

o public Complex divide (Complex otherNumber): This method will find the ... of the current complex number and the passed complex number. The methods returns a new Complex number which is the ... of the two.

o public void setRealPart (double realPart): Used to set the real part of this complex number.

o public void setImaginaryPart (double realPart): Used to set the imaginary part of this complex number.

o public double getRealPart(): This method returns the real part of the complex number

o public double getImaginaryPart(): This method returns the imaginary part of the complex number

o public String toString(): This method allows the complex number to be easily printed out to the screen

Write a separate class ComplexDemo with a main() method and test the Complex class methods.


Solution: 



package com.company;
import java.util.Scanner;
public class lab4problem17{

    public static void main(String[] args) {
Scanner Obj = new Scanner(System.in);
        System.out.print("Enter your real number: " );
        double  realNumber = Obj.nextDouble();
        System.out.print("Enter your imaginary number: ");
        double  imaginaryNamber = Obj.nextDouble();

    Complex com = new Complex(realNumber,imaginaryNamber);
        System.out.print("Enter your 2nd real number: " );
        double realNamber2 = Obj.nextDouble();
        System.out.print("Enter your 2nd imaginary number: ");
        double imaginaryNamer2 = Obj.nextDouble();
        Complex com2 = new Complex (realNamber2 ,imaginaryNamer2);
        Complex com3 = new Complex();
         com3 = com3.Add(com,com2);
        System.out.println("Add number: "+com3);
         com3 = com3.subtract(com,com2);
        System.out.println("subtracting: "+com3);
         com3 = com3.multiply(com,com2);
        System.out.println("multiply: "+com3);
         com3 = com3.divide(com,com2);
        System.out.println("divide: "+com3);

    }

}

public class Complex {
    double realpart;
    double imaginarypart;

    public Complex(double realpart, double imaginarypart) {
        this.realpart = realpart;
        this.imaginarypart = imaginarypart;
    }
    public Complex(){

    }
    public Complex Add(Complex com, Complex com2){
        Complex temp = new Complex();
        temp.realpart= com.realpart + com2.realpart;
        temp.imaginarypart = com.imaginarypart + com2.imaginarypart;
        return temp;
    }

    public Complex subtract(Complex com, Complex com2){
        Complex temp = new Complex();
        temp.realpart= com.realpart - com2.realpart;
        temp.imaginarypart = com.imaginarypart - com2.imaginarypart;
        return temp;
    }

    public Complex multiply(Complex com, Complex com2){
        Complex temp = new Complex();
        temp.realpart= com.realpart * com2.realpart;
        temp.imaginarypart = com.imaginarypart * com2.imaginarypart;
        return temp;
    }

    public Complex divide(Complex com, Complex com2){
        Complex temp = new Complex();
        temp.realpart= com.realpart / com2.realpart;
        temp.imaginarypart = com.imaginarypart / com2.imaginarypart;
        return temp;
    }

    public void setRealpart(double realpart) {
        this.realpart = realpart;
    }

    public void setImaginarypart(double imaginarypart) {
        this.imaginarypart = imaginarypart;
    }

    public double getRealpart() {
        return realpart;
    }

    public double getImaginarypart() {
        return imaginarypart;
    }

    @Override
    public String toString() {
        return  this.realpart +
                "+" +this.imaginarypart+"i";
    }
}



Previous Post Next Post