Expand|Select|Wrap|Line Numbers
- import java.util.Scanner;
- public class Complex
- {
- /** declare variables */
- public static double real;
- public static double imaginary;
- /** default constructor */
- public Complex()
- {
- real = 0.0;
- imaginary = 0.0;
- }
- /** assigns r to real and i to imaginary */
- public Complex(double r, double i)
- {
- real = r;
- imaginary = i;
- }
- /** assigns r to real */
- public void setReal(double r)
- {
- real = r;
- }
- /** assigns i to imaginary */
- public void setImaginary(double i)
- {
- imaginary = i;
- }
- /** assigns real to r and returns it */
- public double getReal()
- {
- double r = real;
- return real;
- }
- /** assigns imaginary to i and returns it */
- public double getImaginary()
- {
- double i = imaginary;
- return imaginary;
- }
- /** declares and initializes realPart */
- // public void setRealPart(double realPart)
- // {
- // double alpha;
- // double realPart = realPart + alpha * i;
- // }
- /** initializes object to alpha */
- // public void setAlpha()
- // {
- // double alpha;
- // Complex = alpha + alpha * i;
- // }
- /** check for equality */
- public Boolean equals(Complex C)
- {
- return((real == C.real) && (imaginary == C.imaginary));
- }
- /** add */
- public static Complex add(Complex C)
- {
- /* Complex add;
- double r = real + C.real;
- double i = imaginary + C.imaginary;
- add = new Complex(r, i);
- return add; */
- return new Complex(real + C.real, imaginary + C.imaginary);
- }
- /** subtract */
- public static Complex subtract(Complex C)
- {
- /* Complex subtract;
- double r = real - C.real;
- double i = imaginary - C.imaginary;
- subtract = new Complex(r, i);
- return subtract; */
- return new Complex(real - C.real, imaginary - C.imaginary);
- }
- /** multiply */
- public static Complex multiply(Complex C)
- {
- /* Complex multiply;
- double r = real * C.real - imaginary * C.imaginary;
- double i = real * C.imaginary + imaginary * C.real;
- multiply = new Complex(r, i);
- return multiply; */
- return new Complex(real * C.real - imaginary * C.imaginary, real * C.imaginary + imaginary * C.real);
- }
- /** displays to screen */
- public String toString()
- {
- String imaginarySymbol = (imaginary < 0) ? " - " : " + ";
- return (real + imaginarySymbol + imaginary + "i");
- }
- /** driver */
- public static void main(String[] args)
- {
- Complex A = new Complex();
- Complex B = new Complex();
- Complex C = new Complex();
- Scanner input = new Scanner(System.in);
- /** input and scan first real number */
- System.out.println("Enter a double for the first real number: ");
- A.setReal(input.nextDouble());
- /** input and scan first imaginary number */
- System.out.println("\nEnter a double for the first imaginary number: ");
- A.setImaginary(input.nextDouble());
- /** input and scan second real number */
- System.out.println("\nEnter a double for the second real number: ");
- B.setReal(input.nextDouble());
- /** input and scan second imaginary number */
- System.out.println("\nEnter a double for the second imaginary number: ");
- B.setImaginary(input.nextDouble());
- /** add complex numbers and display to screen*/
- A.add(B);
- System.out.println(A.toString() + " + " + B.toString() + " = " + A.add(B));
- /** subtract complex numbers and display to screen*/
- A.subtract(B);
- System.out.println(A.toString() + " - " + B.toString() + " = " + A.subtract(B));
- /** multiply complex numbers and display to screen*/
- A.multiply(B);
- System.out.println(A.toString() + " * " + B.toString() + " = " + A.multiply(B));
- }
- }
- /**
- Enter a double for the first real number:
- 1.0
- Enter a double for the first imaginary number:
- 1.5
- Enter a double for the second real number:
- 2.0
- Enter a double for the second imaginary number:
- 2.5
- 4.0 + 5.0i + 4.0 + 5.0i = 8.0 + 10.0i
- 0.0 + 0.0i - 0.0 + 0.0i = 0.0 + 0.0i
- 0.0 + 0.0i * 0.0 + 0.0i = 0.0 + 0.0i
- Press any key to continue . . .
- */