Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can you clean this up for me? I am trying to write a program that prompts the us

ID: 3642051 • Letter: C

Question

Can you clean this up for me? I am trying to write a program that prompts the user to enter two complex numbers and display the result of their addition, subtraction, multiplication and division. Working in NetBeans 7.1. Thanks community!
Compile: javac ComplexTest.java
Run: java ComplexTest



/*
* Mike
*
*
*/
package complex;

public class Complex {

// Import java file

import java.util.Scanner;

import java.math.*;

// Declare class complexnumbers

class complex

{
// Declare variable

private double r;

private double i;

// Construct

Complex(double rr, double ii)

{
r = rr;

i = ii;
}

// Declare toString function

public string toString ()

{
StringBuffer sb = new StringBuffer().append(r);

if (i>0)

sb.append('+');

return sb.append(i).append('i').toString();

}

// Declare getRealPart method

public double getRealPart()

{
return r;

}
// Declare getImaginaryPart method

public double getImaginaryPart()

{
return i;

// Declare magnitude method

public double magnitude()

{

return Math.sqrt(r*r + i*i);

}

// Declare the add method

public Complex add (Complex other)

{

return add (this, other);

}

// Now add two complexes

public static Complex add(Complex c1, Complex c2)

{
return new Complex(c1.r + c2.r, c1.i + c2.i);

// Declare the subtract method

public Complex subtract(Complex other)

{
return subtract(this, other);

}

// Now subtract two complex numbers

public static Complex subtract(Complex c1, Complex c2)

{

return new Complex (c1.r c2.r, c1.i-c2.i);

}

// Now multiply complex numbers

public Complex multiply(Complex other)

{
return multiply(this, other);

}

// Multiply two complex numbers

public static Complex multiply(Complex c1, Complex c2)

{

return new Complex(c1.r*c2.r - c1.i*c2.i, c1.r*c2.i + c1.i*c2.r);


}

// Now divide complex numbers

{

}
public static Complex divide(Complex c1, Complex c2)

{

return new Complex((c1.r*c2.r+c1.i*c2.i)/(c2.r*c2.r+c2.i*c2.i),
(c1.i*c2.r-c1.r*c2.i)/(c2,r*c2.r+c2.i*c2.i));

}

// Comparing different complex numbers

public boolean equals(Object o)

{

if (!(o instanceof Complex))

throw new IllegalArgumentException(
"Complex.equals argument must be a Complex");

Complex other = (Complex)o;

return r == other.r && i == other.i;

}

// Compute the hashcoade

public int hashcode()

{

return (int)(r) | (int)(i);

}

}

// Declare class

public class complexnumbers

{

// Main fct

public static void main(String[] args)

{

// Declare variable

double a, b;

// Create scanner

Scanner input = new Scanner(System.in);

// Promt user for number entry

System.out.print("Enter your first complex number:");

// Read the numbers from the keyboard

a = input.nextDouble();

b = input.nextDouble();

Complex c = new Complex(a, b);

double k = (Math.abs(a) + Math.abs(b));

// Prompt user to enter numbers

System.out.print("Eneter your first complex number: ");

// Read the numbers from the keyboard

a = input.nextDouble();

b = input.nextDouble();

Complex d = new Complex(a,b);

// Displaying the output

System.out.println(c + "+" + d + "=" + c.add(d));

System.out.println(c + "-" + d + "=" + c.subtract(c,d));

System.out.println(c + "*" + d + "=" + c.multiply(c,d));

System.out.println(c + "/" + d + "=" + Complex.divide(c,d));

double l = (Math.abs(a) + Math.abs(b));

System.out.println("The absolute value equals "+Math.sqrt(l+k));

}

}


Explanation / Answer

package complex;

import java.util.Scanner;
/**
*
* @author Mike
*/
// Declare class complexnu1mbers
class Complex
{
    // Declare variable
    private double r;
    private double i;
  
    // Constructor
    Complex(double rr, double ii)
    {
        r = rr;
        i = ii;
    }
  
    // Declare toString function
    @Override
    public String toString ()
    {
        StringBuffer sb = new StringBuffer().append(r);
        if (i>0)
            sb.append('+');
        return sb.append(i).append('i').toString();
    }
  
    // Declare getRealPart method
    public double getRealPart()
    {
        return r;
    }
    // Declare getImaginaryPart method
    public double getImaginaryPart()
    {
        return i;
    }
  
    // Declare magnitude method
    public double magnitude()
    {
        return Math.sqrt(r*r + i*i);
    }
  
    // Declare the add method
    public Complex add (Complex other)
    {
        return add (this, other);
    }

    // Now add two complexes
    public static Complex add(Complex c1, Complex c2)
    {
        return new Complex(c1.r + c2.r, c1.i + c2.i);
    }
  
    // Declare the subtract method
    public Complex subtract(Complex other)
    {
        return subtract(this, other);
    }

    // Now subtract two complex numbers
    public static Complex subtract(Complex c1, Complex c2)
    {
        return new Complex (c1.r-c2.r, c1.i-c2.i);
    }
  
    // Now multiply complex numbers
    public Complex multiply(Complex other)
    {
        return multiply(this, other);
    }

    // Multiply two complex numbers
    public static Complex multiply(Complex c1, Complex c2)
    {
        return new Complex(c1.r*c2.r - c1.i*c2.i, c1.r*c2.i + c1.i*c2.r);
    }
  
    // Now divide complex numbers
    public Complex divide(Complex other)
    {
        return divide(this, other);
    }
  
    public static Complex divide(Complex c1, Complex c2)
    {
        return new Complex( (c1.r*c2.r+c1.i*c2.i)/(c2.r*c2.r+c2.i*c2.i),
                            (c1.i*c2.r-c1.r*c2.i)/(c2.r*c2.r+c2.i*c2.i));
    }
  
    // Comparing different complex numbers
    @Override
    public boolean equals(Object o)
    {
        if (!(o instanceof Complex))
            throw new IllegalArgumentException("Complex.equals argument must be a Complex");
        Complex other = (Complex)o;
        return r == other.r && i == other.i;
    }
  
    // Compute the hashcoade
    @Override
    public int hashCode()
    {
        return (int)(r) | (int)(i);
    }
}

// Declare class
public class ComplexTest
{
    // Main fct
    public static void main(String[] args)
    {
        // Declare variable
        double a, b;
        // Create scanner
        Scanner input = new Scanner(System.in);
        // Promt user for number entry
        System.out.println("Enter your first complex number(real and imaginary parts):");
        // Read the numbers from the keyboard
        a = input.nextDouble();
        b = input.nextDouble();
        Complex c = new Complex(a, b);
        double k = (Math.abs(a) + Math.abs(b));
      
        // Prompt user to enter numbers
        System.out.print("Enter your second complex number(real and imaginary parts): ");
        // Read the numbers from the keyboard
        a = input.nextDouble();
        b = input.nextDouble();
        Complex d = new Complex(a,b);
        // Displaying the output
        System.out.println(c + "+" + d + "=" + c.add(d));
        System.out.println(c + "-" + d + "=" + Complex.subtract(c,d));
        System.out.println(c + "*" + d + "=" + Complex.multiply(c,d));
        System.out.println(c + "/" + d + "=" + Complex.divide(c,d));
        double l = (Math.abs(a) + Math.abs(b));
        System.out.println("The absolute value equals "+Math.sqrt(l+k));
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote