/home/caleb/ASDV-Java/Semester 2/Assignments/MP3_CalebFontenot/src/mp3_calebfontenot/MP3_CalebFontenot.java |
nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt
nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java
package mp3_calebfontenot;
import java.util.Scanner;
public class MP3_CalebFontenot {
args
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the first complex number: ");
double a = input.nextDouble();
double b = input.nextDouble();
Complex c1 = new Complex(a, b);
System.out.print("Enter the second complex number: ");
double c = input.nextDouble();
double d = input.nextDouble();
Complex c2 = new Complex(c, d);
System.out.println("(" + c1 + ")" + " + " + "(" + c2 + ")" + " = "
+ c1.add(c2));
System.out.println("(" + c1 + ")" + " - " + "(" + c2 + ")" + " = "
+ c1.subtract(c2));
System.out.println("(" + c1 + ")" + " * " + "(" + c2 + ")" + " = "
+ c1.multiply(c2));
System.out.println("(" + c1 + ")" + " / " + "(" + c2 + ")" + " = "
+ c1.divide(c2));
System.out.println("|" + c1 + "| = " + c1.abs());
Complex c3 = (Complex) c1.clone();
System.out.println(c1 == c3);
System.out.println(c3.getRealPart());
System.out.println(c3.getImaginaryPart());
}
}
class Complex {
private double a;
private double b;
private final double = Math.sqrt(-1);
public Complex()
{
this.a = 0;
}
public Complex(double a)
{
this.a = a;
this.b = 0;
}
public Complex(double a, double b)
{
this.a = a;
this.b = b;
}
public Complex add(double c, double d)
{
return new Complex(this.a + c, this.b + d);
}
public Complex add(Complex obj)
{
return new Complex(this.a + obj.a, this.b + obj.b);
}
public Complex subtract(double c, double d)
{
return new Complex(this.a - c, this.b - d);
}
public Complex subtract(Complex obj)
{
return new Complex(this.a - obj.a, this.b - obj.b);
}
public Complex multiply(double c, double d)
{
double r = this.a * c - this.b * d;
double i = this.a * d + this.b * c;
return new Complex(r, i);
}
public Complex multiply(Complex obj) {
double r = this.a * obj.a - this.b * obj.b;
double i = this.a * obj.b + this.b * obj.a;
return new Complex(r, i);
}
public Complex divide(double c, double d)
{
double denominator = (c * c) + (this.b * this.b);
double aNum = (this.a * c) + (this.b * this.b);
double iNum = (this.b * c) - (this.a * d);
double realResult = aNum / denominator;
double imaginaryResult = iNum / denominator;
return new Complex(realResult, imaginaryResult);
}
public Complex divide(Complex obj)
{
double denominator = (obj.a * obj.a) + (this.b * this.b);
double aNum = (this.a * obj.a) + (this.b * obj.b);
double iNum = (this.b * obj.a) - (this.a * obj.b);
double realResult = aNum / denominator;
double imaginaryResult = iNum / denominator;
return new Complex(realResult, imaginaryResult);
}
@Override
public String toString()
{
return a + " + " + b + "i";
}
public Complex clone() {
Complex obj = new Complex(this.a, this.b);
return obj;
}
public double abs() {
return Math.abs(this.a + this.b);
}
public double getRealPart() {
return this.a;
}
public double getImaginaryPart() {
return this.b;
}
}