Created
February 11, 2021 16:13
-
-
Save jananpatel2002/a7bfc27b67cf04bbf5b9a41ec6f114ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Name: Janan Patel | |
* Date: 2/11/2021 | |
* Course Number: CSC-112-D01 | |
* Course Name: Computer Science | |
* Problem Number: 3 | |
* Email: jkpatel2001@student.stcc.edu | |
* Short Description of the Problem: Complex numbers | |
*/ | |
package Complex; | |
public class Complex { | |
private double real; | |
private double imag; | |
public Complex(double real, double imag) { | |
this.real = real; | |
this.imag = imag; | |
} | |
public Complex(double real) { | |
this.real = real; | |
this.imag = 0; | |
} | |
public Complex() { | |
this.real = 0; | |
this.imag = 0; | |
} | |
public Complex add(Complex c2) { | |
double real = this.real + c2.real; | |
double imag = this.imag + c2.imag; | |
return new Complex(real, imag); | |
} | |
public Complex subtract(Complex c2) { | |
Complex a = this; | |
return a.add(c2.negate()); | |
} | |
public Complex multiply(Complex c2) { | |
double real = (this.real * c2.real) - (this.imag * c2.imag); | |
double imag = (this.real * c2.imag) + (this.imag * c2.real); | |
return new Complex(real, imag); | |
} | |
public Complex divide(Complex c2) { | |
double real = ((this.real * c2.real) + (this.imag * c2.imag)) / ((c2.real * c2.real) + (c2.imag * c2.imag)); | |
double imag = ((this.imag * c2.real) - (this.real * c2.imag)) / ((c2.real * c2.real) + (c2.imag * c2.imag)); | |
return new Complex(real, imag); | |
} | |
public double abs() { | |
double absolutevalue = Math.sqrt((this.real * this.real) + (this.imag * this.imag)); | |
return absolutevalue; | |
} | |
public Complex negate() { | |
return new Complex(-this.real, -this.imag); | |
} | |
public Complex conjugate() { | |
return new Complex(this.real, -this.imag); | |
} | |
public double distance(Complex c2) { | |
double distance = Math.sqrt( | |
((this.real - c2.real) * (this.real - c2.real)) + ((this.imag - c2.imag) * (this.imag - c2.imag))); | |
return distance; | |
} | |
public boolean equals(Complex c2) { | |
boolean x = Math.abs(this.distance(c2) / Math.max(this.abs(), c2.abs())) < 1E-6; | |
if (x == true) { | |
return true; | |
} else | |
return false; | |
} | |
public boolean greaterThan(Complex c2) { | |
boolean isOrisnt; | |
if (this.abs() > c2.abs()) { | |
isOrisnt = true; | |
} else | |
isOrisnt = false; | |
return isOrisnt; | |
} | |
public boolean lessThan(Complex c2) { | |
boolean isOrisnt; | |
if (this.abs() < c2.abs()) { | |
isOrisnt = true; | |
} else | |
isOrisnt = false; | |
return isOrisnt; | |
} | |
public String toString() { | |
return this.real + " + (" + this.imag + " i)"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Name: Janan Patel | |
* Date: 2/11/2021 | |
* Course Number: CSC-112-D01 | |
* Course Name: Computer Science | |
* Problem Number: 3 | |
* Email: jkpatel2001@student.stcc.edu | |
* Short Description of the Problem: Complex numbers | |
*/ | |
package Complex; | |
import java.util.Scanner; | |
public class ComplexNumberDriver { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
double real; | |
double imag; | |
while (true) { | |
System.out.print("Enter Real And Imaginary Components of Complex Number 1: "); | |
real = sc.nextDouble(); | |
imag = sc.nextDouble(); | |
Complex c1 = new Complex(real, imag); | |
System.out.print("Enter Real And Imaginary Components of Complex Number 2: "); | |
real = sc.nextDouble(); | |
imag = sc.nextDouble(); | |
Complex c2 = new Complex(real, imag); | |
sc.nextLine(); | |
Complex plus = c1.add(c2); | |
Complex minus = c1.subtract(c2); | |
Complex times = c1.multiply(c2); | |
Complex quot = c1.divide(c2); | |
System.out.println("c1 = " + c1); | |
System.out.println("c2 = " + c2); | |
System.out.println("c1 + c2 = " + plus); | |
System.out.println("c1 - c2 = " + minus); | |
System.out.println("c1 * c2 = " + times); | |
System.out.println("c1 / c2 = " + quot); | |
System.out.println("-c1 = " + c1.negate()); | |
System.out.println("-c2 = " + c2.negate()); | |
System.out.println("Conj c1 = " + c1.conjugate()); | |
System.out.println("Conj c2 = " + c2.conjugate()); | |
System.out.println("|c1| = " + c1.abs()); | |
System.out.println("|c2| = " + c2.abs()); | |
System.out.println("Dist = " + c1.distance(c2)); | |
System.out.println("==? = " + c1.equals(c2)); | |
System.out.println(">? = " + c1.greaterThan(c2)); | |
System.out.println("<? = " + c1.lessThan(c2)); | |
System.out.print("Do again? "); | |
if (!sc.next().toUpperCase().equals("Y")) | |
break; | |
sc.nextLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment