Question

JAVA PROGRAMMING

A complex number is a number in the form a + bi, where a and b are real numbers and i is V-1. The numbers a and b are known a

Design a class named Complex for representing complex numbers and the methods add, subtract, multiply, divide, and abs for pe

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;

class ComplexNumber implements Cloneable
{
private double rPart,iPart;
public ComplexNumber()//default constructor
{
rPart = 0;
iPart = 0;
}
public ComplexNumber(double rPart,double iPart)//argument constructor
{
this.rPart = rPart;
this.iPart = iPart;
}
//arithmetic operations on complex Numbers
public ComplexNumber add (ComplexNumber other)
{
ComplexNumber result = new ComplexNumber();
result.rPart = this.rPart + other.rPart;
result.iPart = this.iPart + other.iPart;
return result;
}
public ComplexNumber subtract (ComplexNumber other)
{
ComplexNumber result = new ComplexNumber();
result.rPart = this.rPart - other.rPart;
result.iPart = this.iPart - other.iPart;
return result;
}
public ComplexNumber multiply (ComplexNumber other)
{
ComplexNumber result = new ComplexNumber();
result.rPart = (this.rPart * other.rPart) - (this.iPart * other.iPart);
result.iPart = (this.rPart * other.iPart) + (this.iPart * other.rPart);
return result;
}
public ComplexNumber divide (ComplexNumber other)
{
ComplexNumber result = new ComplexNumber();
// multiply with complex conjugate
result.rPart = ((this.rPart * other.rPart) -(this.iPart *(-1)* other.iPart))/ ( (other.rPart * other.rPart) +( other.iPart * (-1)*(-other.iPart)));
result.iPart = ((this.rPart * other.iPart)) +(this.iPart*(other.rPart))/ ( (other.rPart * other.rPart) +( other.iPart *(-1)* (-other.iPart))) ;
return result;
}
public double absoluteValue()
{
   return Math.sqrt(rPart*rPart + iPart*iPart);
}
//get methods
public double getRPart()
{
return rPart;
}
public double getIPart()
{
return iPart;
}
public String toString()
{
return rPart + " + "+ iPart+" i";
}
public Object clone() throws CloneNotSupportedException
{
ComplexNumber objClone = new ComplexNumber();
       objClone.setReal(this.rPart);
       objClone.setImag(this.iPart);
       return objClone;
}
  
public void setReal(double rPart)
{
   this.rPart = rPart;
}
public void setImag(double iPart)
{
   this.iPart = iPart;
}
public double compareTo(ComplexNumber other)
{

return this.getRPart() - other.getRPart();
}
}

class ComplexNumberTester
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter the first complex number c1 : ");
double rPart = input.nextDouble();
double iPart = input.nextDouble();
ComplexNumber c1 = new ComplexNumber(rPart,iPart);

System.out.println("Enter the second complex number c2 : ");
rPart = input.nextDouble();
iPart = input.nextDouble();
ComplexNumber c2 = new ComplexNumber(rPart,iPart);


ComplexNumber add = new ComplexNumber();
ComplexNumber sub = new ComplexNumber();
ComplexNumber mult = new ComplexNumber();
ComplexNumber div = new ComplexNumber();
System.out.println("Complex Number c1 :"+c1);
System.out.println("Complex Number c2 :"+c2);
add = c1.add(c2);
System.out.println("\nAddition of c1 and c2 : "+ add);
sub = c1.subtract(c2);
System.out.println("\nSubtraction of c1 and c2 : "+ sub);
mult = c1.multiply(c2);
System.out.println("\nMutiplication of c1 and c2 : "+ mult);
div = c1.divide(c2);
System.out.println("\nDivision of c1 by c2 : "+ div);

System.out.println("Absolute value of c1 : "+c1.absoluteValue());
}
}

Output:

Success #stdin #stdout 0.07s 2184192KB

Enter the first complex number c1 : 3.5 5.5
Enter the second complex number c2 : -3.5 1
Complex Number c1 :3.5 + 5.5 i
Complex Number c2 :-3.5 + 1.0 i

Addition of c1 and c2 : 0.0 + 6.5 i

Subtraction of c1 and c2 : 7.0 + 4.5 i

Mutiplication of c1 and c2 : -17.75 + -15.75 i

Division of c1 by c2 : -0.5094339622641509 + 2.047169811320755 i
Absolute value of c1 : 6.519202405202649

Do ask if any doubt. Please upvote.

Add a comment
Know the answer?
Add Answer to:
JAVA PROGRAMMING A complex number is a number in the form a + bi, where a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A complex number is a number of the form a + bi, where a and b...

    A complex number is a number of the form a + bi, where a and b are real numbers √ and i is −1. The numbers a and b are known as the real and the imaginary parts, respectively, of the complex number. The operations addition, subtraction, multiplication, and division for complex num- bers are defined as follows: (a+bi)+(c+di) = (a+c)+(b+d)i (a+bi)−(c+di) = (a−c)+(b−d)i (a + bi) ∗ (c + di) = (ac − bd) + (bc + ad)i (a...

  • A complex number is a number in the form a + bi, where a and b...

    A complex number is a number in the form a + bi, where a and b are real numbers and i is sqrt( -1). The numbers a and b are known as the real part and imaginary part of the complex number, respectively. You can perform addition, subtraction, multiplication, and division for complex numbers using the following formulas: a + bi + c + di = (a + c) + (b + d)i a + bi - (c + di)...

  • 2. A complex number can be expressed as a + bi where a and b are...

    2. A complex number can be expressed as a + bi where a and b are real numbers and i is the imaginary unit. The multiplication of two complex numbers is defined as follows: (a+bi)(c+di) = (ac-bd) + (bc+ad)i Define a class which represents a complex number. The only member functions you have to define and implement are those which overload the * and *= symbols.

  • A complex number is a number in the form of a + bi, where a and...

    A complex number is a number in the form of a + bi, where a and b are real numbers and i is the square root of negative 1. Design and create a class called Complex for representing complex numbers. This class should have two attributes, a and b, which represent the parts of the complex number. This class should overload the +,-,*,/,++ and -- operators (both prefix and suffix) to perform basic complex number calculations according to the following...

  • c++ 2) Complex Class A complex number is of the form a+ bi where a and...

    c++ 2) Complex Class A complex number is of the form a+ bi where a and b are real numbers and i 21. For example, 2.4+ 5.2i and 5.73 - 6.9i are complex numbers. Here, a is called the real part of the complex number and bi the imaginary part. In this part you will create a class named Complex to represent complex numbers. (Some languages, including C++, have a complex number library; in this problem, however, you write the...

  • C++ Addition of Complex Numbers Background Knowledge A complex number can be written in the format of , where and are real numbers.   is the imaginary unit with the property of .   is called the r...

    C++ Addition of Complex Numbers Background Knowledge A complex number can be written in the format of , where and are real numbers.   is the imaginary unit with the property of .   is called the real part of the complex number and   is called the imaginary part of the complex number. The addition of two complex numbers will generate a new complex number. The addition is done by adding the real parts together (the result's real part) and adding the...

  • For the complex number given as: z = a + bi / c+di where i =...

    For the complex number given as: z = a + bi / c+di where i = √−1 is the imaginary unit. The parameters are defined as a = √2, b = 0, c = 0.5 and d = −0.5. (a) Find the real and the imaginary parts of z, and then draw the Argand dia- gram. (Hint: Use the conjugate of the denominator.) 2.5 (b) Based on the Argand diagram, find the distance r of the complex number z from...

  • Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form:...

    Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form: realPart + imaginaryPart * i where i is √-1 Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should contain default values of (1,1) i.e. 1 for the real part and 1 for the imaginary part. Provide public member functions that perform the following...

  • Complex number class:: Question Changed to just the Java portion Design a class in and Java...

    Complex number class:: Question Changed to just the Java portion Design a class in and Java that represents complex numbers and supports important operations such as addition, subtraction, multiplication and division. (the following is for the c++ and python version, not sure if its needed for this) op: Complex × Complex → Complex op: Complex × double → Complex op: double × Complex → Complex Where op is one of +, -, *, or /. In addition, you will need...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT