Question

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 to overload the stream insertion operator << to print objects of this type.

A constructor must be defined as well as overloading the assignment operator to allow for implicit conversion from doubles to Complex. Any other methods you deem appropriate should also be included. The more complete your class the better.

The Java version will not have as many methods because Java does not allow for operator overloading or friend functions. Again, the more complete your Java class the better. Override the toString() method.

Complex.java file that is the Java implementation, and a Main.java file that instantiates and tests all methods of the Complex class.

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

Below is the implementation in C++

complex.cc

#include "complex.h"

ostream &operator<<(ostream &out, const Complex &c)
{
if (c.real == 0 && c.virl == 0) {
out << 0;
} else if (c.real == 0) {
out << c.virl << "j";
} else if (c.virl == 0) {
out << c.real;
} else if (c.virl > 0) {
if (c.virl == 1) {
out << c.real << "+j";
} else {
out << c.real << "+" << c.virl << "j";
}
} else {
if (c.virl == -1) {
cout << c.real << "-j";
} else {
out << c.real << c.virl << "j";
}
}
return out;
}

Complex::Complex(double real, double virl) :
real(real), virl(virl)
{
}

Complex::Complex(double real) :
real(real), virl(0)
{
}

Complex::~Complex()
{
}

// operator overloading
Complex Complex::operator+(const Complex &that) const
{
return Complex(this->real + that.real, this->virl + that.virl);
}

Complex Complex::operator-(const Complex &that) const
{
return Complex(this->real - that.real, this->virl - that.virl);
}

Complex Complex::operator*(const Complex &that) const
{
double real = this->real * that.real - this->virl * that.virl;
double virl = this->real * that.virl + this->virl * that.real;
return Complex(real, virl);
}

Complex Complex::operator/(const Complex &that) const
{
Complex result = *this * Complex(that.real, -that.virl);
double abs = that.real * that.real + that.virl * that.virl;
result.real /= abs;
result.virl /= abs;
return result;
}

Complex operator+(const double x, const Complex &that)
{
return that + x;
}

Complex operator-(const double x, const Complex &that)
{
return Complex(x) - that;
}

Complex operator*(const double x, const Complex &that)
{
return that * x;
}

Complex operator/(const double x, const Complex &that)
{
return Complex(x) / that;
}

double Complex::getReal() const
{
return real;
}

double Complex::getVirl() const
{
return virl;
}

complex.h

#ifndef _COMPLEX_H
#define _COMPLEX_H

#include <iostream>
using namespace std;

class Complex {
private:
double real;
double virl;

public:
Complex(double real, double virl);
// this constructor will converts a double to Complex implictly
Complex(double real);
~Complex();

// operator overloading
Complex operator+(const Complex &that) const;
Complex operator-(const Complex &that) const;
Complex operator*(const Complex &that) const;
Complex operator/(const Complex &that) const;
  

friend Complex operator+(const double x, const Complex &that);
friend Complex operator-(const double x, const Complex &that);
friend Complex operator*(const double x, const Complex &that);
friend Complex operator/(const double x, const Complex &that);

friend ostream &operator<<(ostream &out, const Complex &c);

double getReal() const;
double getVirl() const;
};


#endif

main.cc

#include "complex.h"

int main() {
// test the functions of Complex class
Complex a(1, 2);
Complex b(3, 4);

cout << "a = " << a << endl;
cout << "b = " << b << endl;

// operators
// two complex
cout << "a + b = " << a + b << endl;
cout << "a - b = " << a - b << endl;
cout << "a * b = " << a * b << endl;
cout << "a / b = " << a / b << endl;

// complex with double
cout << "a + 1 = " << a + 1 << endl;
cout << "a - 1 = " << a - 1 << endl;
cout << "a * 2 = " << a * 2 << endl;
cout << "a / 2 = " << a / 2 << endl;

// double with complex
cout << "-3 + b = " << -3 + b << endl;
cout << "3 - b = " << 3 - b << endl;
cout << "3 * b = " << 3 * b << endl;
cout << "3 / b = " << 3 / b << endl;

return 0;
}

Sample Run: -

a = 1+2j
b = 3+4j
a + b = 4+6j
a - b = -2-2j
a * b = -5+10j
a / b = 0.44+0.08j
a + 1 = 2+2j
a - 1 = 2j
a * 2 = 2+4j
a / 2 = 0.5+j
-3 + b = 4j
3 - b = -4j
3 * b = 9+12j
3 / b = 0.36-0.48j

Here is Java implementation

Complex.java

public class Complex {

private final double real;

private final double virl;

public Complex(double real, double virl) {

this.real = real;

this.virl = virl;

}

public double getReal() {

return real;

}

public double getVirl() {

return virl;

}

public Complex add(Complex that) {

return new Complex(this.real + that.real, this.virl + that.virl);

}

public Complex subtract(Complex that) {

return new Complex(this.real - that.real, this.virl - that.virl);

}

public Complex multiply(Complex that) {

double real = this.real * that.real - this.virl * that.virl;

double virl = this.real * that.virl + this.virl * that.real;

return new Complex(real, virl);

}

public Complex divide(Complex that) {

Complex result = this.multiply(new Complex(that.real, -that.virl));

double abs = that.real * that.real + that.virl * that.virl;

return new Complex(result.real / abs, result.virl / abs);

}

public Complex add(double that) {

return this.add(new Complex(that, 0));

}

public Complex subtract(double that) {

return this.subtract(new Complex(that, 0));

}

public Complex multiply(double that) {

return this.multiply(new Complex(that, 0));

}

public Complex divide(double that) {

return this.divide(new Complex(that, 0));

}

@Override

public String toString() {

Complex c = this;

if (c.real == 0 && c.virl == 0) {

return "0";

} else if (c.real == 0) {

return "" + c.virl + "j";

} else if (c.virl == 0) {

return "" + c.real;

} else if (c.virl > 0) {

if (c.virl == 1) {

return "" + c.real + "+j";

} else {

return "" + c.real + "+" + c.virl + "j";

}

} else {

if (c.virl == -1) {

return "" + c.real + "-j";

} else {

return "" + c.real + c.virl + "j";

}

}

}

}

Main.java

public class Main {

public static void main(String[] args) {

Complex a = new Complex(1, 2);

Complex b = new Complex(3, 4);

System.out.printf("a = %s\n", a);

System.out.printf("b = %s\n", a);

// operators

System.out.printf("a + b = %s\n", a.add(b));

System.out.printf("a - b = %s\n", a.subtract(b));

System.out.printf("a * b = %s\n", a.multiply(b));

System.out.printf("a / b = %s\n", a.divide(b));

System.out.printf("a + 1 = %s\n", a.add(1));

System.out.printf("a - 1 = %s\n", a.subtract(1));

System.out.printf("a * 2 = %s\n", a.multiply(2));

System.out.printf("a / 2 = %s\n", a.divide(2));

}

}

Sample Run: -

a = 1.0+2.0j
b = 1.0+2.0j
a + b = 4.0+6.0j
a - b = -2.0-2.0j
a * b = -5.0+10.0j
a / b = 0.44+0.08j
a + 1 = 2.0+2.0j
a - 1 = 2.0j
a * 2 = 2.0+4.0j
a / 2 = 0.5+j

Add a comment
Know the answer?
Add Answer to:
Complex number class:: Question Changed to just the Java portion Design a class in and Java...
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
  • JAVA PROGRAMMING A complex number is a number in the form a + bi, where a...

    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 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 + di a + bi - (c +...

  • Java project Project Complex number class. Tuesday April 17 Write a Complex number class. It will...

    Java project Project Complex number class. Tuesday April 17 Write a Complex number class. It will have a default constructor, explicit constructor, and the following methods: read O public Complex add(Complex), public Complex subtract(Complex), public Complex multiply(Complex), public Complex divide(Complex), public boolean equals(complex) and a toString) method. Include get and set methods as well. On my website is an explanation of complex numbers and examples with expected answers along with a main demo program. Study the explanation and use your...

  • C++ OPTION A (Basic): Complex Numbers A complex number, c, is an ordered pair of real...

    C++ OPTION A (Basic): Complex Numbers A complex number, c, is an ordered pair of real numbers (doubles). For example, for any two real numbers, s and t, we can form the complex number: This is only part of what makes a complex number complex. Another important aspect is the definition of special rules for adding, multiplying, dividing, etc. these ordered pairs. Complex numbers are more than simply x-y coordinates because of these operations. Examples of complex numbers in this...

  • Write a code in java by considering the following conditions. Task :- 1. Design, implement and test a class RationalNumber. 2. Your class must have a constructor. 3. Your class must implement an inter...

    Write a code in java by considering the following conditions. Task :- 1. Design, implement and test a class RationalNumber. 2. Your class must have a constructor. 3. Your class must implement an interface Number that has methods to perform different operations on a rational numbers (addition, subtraction, reciprocal, multiplication and division). 4. Also design some methods to check whether two rational numbers are same or not (consider numbers are in reduced form). 5. You must develop an appropriate GUI...

  • In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we...

    In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class should contain the following functions: •...

  • This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleTyp...

    This Program should run in C++ In an effort to develop in-depth knowledge of base class, derived class, and operator overloading, we will expand on our in-class exercise of developing the rectangleType class and overload the following operators: +, -, *, ++, --, ==, !=, >>, and <<. Your program should contain the following features: a. Create a base class named rectangleType with following private data members: • length with double type • width with double type b. rectangleType class...

  • Java Please - Design and implement a class Triangle. A constructor should accept the lengths of...

    Java Please - Design and implement a class Triangle. A constructor should accept the lengths of a triangle’s 3 sides (as integers) and verify that the sum of any 2 sides is greater than the 3rd(i.e., that the 3 sides satisfy the triangle inequality). The constructor should mark the triangle as valid or invalid; do not throw an exception. Provide get and set methods for the 3 sides, and recheck for validity in the set methods. Provide a toString method...

  • C++ must use header files and implementation files as separate files. I’ll need a header file,...

    C++ must use header files and implementation files as separate files. I’ll need a header file, implementation file and the main program file at a minimum. Compress these files into one compressed file. Make sure you have adequate documentation. We like to manipulate some matrix operations. Design and implement a class named matrixMagic that can store a matrix of any size. 1. Overload the addition, subtraction, and multiplication operations. 2. Overload the extraction (>>) and insertion (<<) operators to read...

  • I have Majority of the code written but I just need to implement the <,>,and =...

    I have Majority of the code written but I just need to implement the <,>,and = sign in the code. I have posted the instructions for the whole code. I will add what I have at the end. Objectives: Implement basic class concepts in Java Implement inheritance with super and sub-classes Implement basic polymorphism concepts Problem: The TARDIS has been infected by a virus which means it is up to Doctor Who to manually enter calculations into the TARDIS interface....

  • NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------...

    NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------------------------------------------------- Tasks to complete: ---------------------------------------------------------------------------------------------------------------------------------------- given code: ----------------------------------------------------------------------------------------------------------------------------------------------- main.cpp #include <iostream> #include <iomanip> #include "fractionType.h" using namespace std; int main() { fractionType num1(5, 6); fractionType num2; fractionType num3; cout << fixed; cout << showpoint; cout << setprecision(2); cout << "Num1 = " << num1 << endl; cout << "Num2 = " << num2 << endl; cout << "Enter the fraction in the form a / b: "; cin >> num2; cout << endl; cout <<...

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