Complete lab5(4)
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
|
||||
*/
|
||||
package mp3_calebfontenot;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class MP3_CalebFontenot {
|
||||
|
||||
/**
|
||||
* @param args the command line arguments
|
||||
*/
|
||||
public static void main(String[] args)
|
||||
{
|
||||
Scanner input = new Scanner(System.in);
|
||||
System.out.print("Enter the first complex number: ");
|
||||
double a = 3;
|
||||
double b = 5;
|
||||
Complex c1 = new Complex(a, b);
|
||||
System.out.print("Enter the second complex number: ");
|
||||
double c = 7;
|
||||
double d = 9;
|
||||
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 i = 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;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user