Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Name: Christopher Watkins
- * Date: 2/23/17
- * Course Number: CSC-112
- * Course Name: Intermediate Java
- * Problem Number: Complex number Class
- * Email: cmwatkins@student.stcc.edu
- * Implement a basic idea of complex numbers in Java
- */
- public class Complex {
- private double real = 0;
- private double imag = 0;
- /** Operations */
- public Complex add(Complex c){
- return new Complex(this.real+c.real,this.imag+c.imag);
- }
- public Complex subtract(Complex c){
- return new Complex(this.real-c.real,this.imag-c.imag);
- }
- public Complex multiply(Complex c){
- return new Complex((c.real * this.real) - (c.imag * this.imag),(c.real * this.imag) + (this.real * c.imag));
- }
- public Complex divide(Complex c){
- /** Removing 'i' from the denominator */
- Complex numerator = new Complex((this.real*c.real)+(this.imag*c.imag),(c.real * this.imag) + (this.real * c.imag));
- double denom = (c.real*c.real) + (c.imag*c.imag);
- /** The actual Division */
- return new Complex(numerator.real/denom,numerator.imag/denom);
- }
- public double abs(){
- return Math.sqrt(Math.pow(this.real,2)+Math.pow(this.imag,2));
- }
- public Complex negate(){
- return new Complex(this.real * -1,this.imag * -1);
- }
- public Complex conjugate(){
- return new Complex(this.real,this.imag * -1);
- }
- public double distance(Complex c){
- return Math.sqrt(Math.pow(this.real - c.real,2) + Math.pow(this.imag - c.imag,2));
- }
- public boolean equals(Complex c){
- if(distance(this) == distance(c))
- return true;
- return false;
- }
- public boolean greaterThan(Complex c){
- Complex origin = new Complex();
- // Checks distance between each Complex number and 0,0 then compares
- if(Math.sqrt(Math.pow(this.real - origin.real,2) + Math.pow(this.imag - origin.imag,2)) > Math.sqrt(Math.pow(origin.real - c.real,2) + Math.pow(origin.imag - c.imag,2)))
- return true;
- return false;
- }
- public boolean lessThan(Complex c){
- Complex origin = new Complex();
- // Checks distance between each Complex number and 0,0 then compares
- if(Math.sqrt(Math.pow(this.real - origin.real,2) + Math.pow(this.imag - origin.imag,2)) < Math.sqrt(Math.pow(origin.real - c.real,2) + Math.pow(origin.imag - c.imag,2)))
- return true;
- return false;
- }
- /** Basic Constructors */
- public Complex(){
- this(0);
- }
- public Complex(double real){
- this(real,0);
- }
- public Complex(double real, double imag){
- this.real = real;
- this.imag = imag;
- }
- /** Print Out */
- @Override
- public String toString() {
- return this.real + " + " + this.imag + " i";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement