package Test;
import java.util.Scanner;
public class Complex implements Cloneable{
private double realPart = 0;
private double ImaginaryPart = 0;
Complex(){
this(0,0);
}
Complex(double a){
this(a,0);
}
Complex(double a,double b){
this.realPart = a;
this.ImaginaryPart = b;
}
public double getRealPart() {
return realPart;
}
public double getImaginaryPart() {
return ImaginaryPart;
}
public Complex add(Complex num) {
double a = this.realPart + num.getRealPart();
double b = this.ImaginaryPart + num.getImaginaryPart();
return new Complex(a,b);
}
public Complex substract(Complex num) {
double a = this.realPart - num.getRealPart();
double b = this.ImaginaryPart - num.getImaginaryPart();
return new Complex(a,b);
}
public Complex divide(Complex num) {
double a = (this.realPart * num.getRealPart() + this.ImaginaryPart * num.getImaginaryPart())
/ (long)(Math.pow(num.getRealPart(), 2) + Math.pow(num.getImaginaryPart(), 2));
double b = (this.ImaginaryPart * num.getRealPart() - this.realPart * num.getImaginaryPart())
/ (long)(Math.pow(num.getRealPart(), 2) + Math.pow(num.getImaginaryPart(), 2));
return new Complex(a,b);
}
public Complex multiply(Complex num) {
double a = this.realPart * num.getRealPart() - this.ImaginaryPart * num.getImaginaryPart();
double b = this.ImaginaryPart * num.getRealPart() + this.realPart * num.getImaginaryPart();
return new Complex(a,b);
}
public double abs() {
return Math.pow(Math.pow(this.realPart, 2) + Math.pow(this.ImaginaryPart, 2), 0.5);
}
public String toString() {
if(this.ImaginaryPart == 0)
return this.realPart + "";
else
return this.realPart + " + " + this.ImaginaryPart + "i";
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the frist complex number(eg:1 2,3 0,1.5 -1,...): ");
double num1 = input.nextDouble();
double num2 = input.nextDouble();
Complex com1 = new Complex(num1,num2);
System.out.print("Enter the second complex number(eg:1 2,3 0,1.5 -1....): ");
double num3 = input.nextDouble();
double num4 = input.nextDouble();
Complex com2 = new Complex(num3,num4);
System.out.println("(" + com1.toString() + ") + (" + com2.toString() + ") = " + com1.add(com2).toString());
System.out.println("(" + com1.toString() + ") - (" + com2.toString() + ") = " + com1.substract(com2).toString());
System.out.println("(" + com1.toString() + ") x (" + com2.toString() + ") = " + com1.multiply(com2).toString());
System.out.println("(" + com1.toString() + ") / (" + com2.toString() + ") = " + com1.divide(com2).toString());
System.out.println("|(" + com1.toString() + ")|" + " = " + com1.abs());
}
}
package Test;
import java.util.Scanner;
public class FuncionComplex {
public static void main(String[] args) {
System.out.print(“Enter a, b, c:”);
Scanner input = new Scanner(System.in);
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
double exit = Math.pow(b, 2) - 4 * a * c;
if(exit > 0) {
double result1 = (-b + Math.pow(exit, 0.5)) / ( 2 * a );
double result2 = (-b - Math.pow(exit, 0.5)) / ( 2 * a );
System.out.println("The roots are " + result1 + " and " + result2);
}else if(exit == 0) {
double result = - b / 2 / a;
System.out.println("The root is " + result);
}else {
b = -1 * b;
Complex high = new Complex(-b,Math.pow(-exit, 0.5));
Complex low = new Complex(2 * a, 0);
Complex result1 = high.divide(low);
Complex high2 = new Complex(-b,-Math.pow(-exit, 0.5));
Complex low2 = new Complex(2 * a, 0);
Complex result2 = high2.divide(low2);
System.out.println("The root are " + result1.toString() + " and " + result2.toString());
}
}
}



