Commit 852abf43 authored by jordan.dalby's avatar jordan.dalby

+ Added comments to code

parent 9eef9328
......@@ -3,71 +3,72 @@ package com.jordan;
public class Complex
{
private double realPart;
private double imaginaryPart;
private double realPart_;
private double imaginaryPart_;
public Complex(double realPart, double imaginaryPart)
{
this.realPart = realPart;
this.imaginaryPart = imaginaryPart;
this.realPart_ = realPart; // set the real
this.imaginaryPart_ = imaginaryPart;
}
public Complex()
{
this(0,0);
this(0,0); // initialize this class with the other constructor
}
public double real()
{
return this.realPart;
return this.realPart_; // return the realPart_ variable
}
public double imag()
{
return this.imaginaryPart;
return this.imaginaryPart_; // return the imaginaryPart_ variable
}
public double magnitude()
{
double val = Math.sqrt( ( Math.pow(realPart, 2) + Math.pow(imaginaryPart, 2) ) );
double val = Math.sqrt( ( Math.pow(this.realPart_, 2) + Math.pow(this.imaginaryPart_, 2) ) ); // calculate the magnitude of the Complex numbers
return val;
}
public double argument()
{
double val = Math.atan2(this.imaginaryPart, this.realPart);
double val = Math.atan2(this.imaginaryPart_, this.realPart_); // calculate the argument of the Complex numbers
return val;
}
public Complex add(Complex b)
{
double real = this.realPart + b.real();
double imag = this.imaginaryPart + b.imag();
double real = this.realPart_ + b.real(); // get the sum of the real numbers
double imag = this.imaginaryPart_ + b.imag(); // get the sum of the imaginary numbers
Complex c = new Complex(real, imag);
return c;
Complex c = new Complex(real, imag); // create a new instance of the Complex class to return
return c; // return the class
}
@Override
public String toString()
{
String s = "";
if (realPart != 0)
s+=String.valueOf(this.realPart);
if (realPart != 0 && imaginaryPart != 0)
if (this.realPart_ != 0) // if there is a real part
s+=String.valueOf(this.realPart_); // append the real part to the string
if (this.realPart_ != 0 && this.imaginaryPart_ != 0) // if there is a real part and an imaginary part
{
if (imaginaryPart < 0)
s += " - ";
if (this.imaginaryPart_ < 0) // if the imaginary part is less than 0
s += " - "; // append a minus
else
s += " + ";
s += " + "; // append a plus
}
if (imaginaryPart != 0)
if (this.imaginaryPart_ != 0) // if the imaginary part exists
{
s+="j" + String.valueOf(Math.abs(this.imaginaryPart));
s+="j" + String.valueOf(Math.abs(this.imaginaryPart_)); // append the imaginary part with a j to the string
}
return s;
return s; // return the string
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment