Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help writing code for a program that will perform JUnit testing on the class: ComplexNum. Eclipse can be used for JUnit test. You

I need help writing code for a program that will perform JUnit testing on the class: "ComplexNum". Eclipse can be used for JUnit test. You need to write the TestComplexNum class which includes methods to test all the constructors and methods in your ComplexNum class. Make sure you also test all the different cases. The TestComplexNum class should extend the TestCase class that is imported from junit.framework.TestCase For most of the test methods, you can compare the actual result with the expected result, or to test if a condition is true or false. You may include the setUp method in your TestComplexNum class. The setUp method will be called before EACH test method is executed. Therefore you should only put the necessary code in the setUp method. For this assignment, you will need to submit your package with two .java files, one for ComplexNum class and one for TestComplexNum class.

Here is the ComplexNum Class:

/**

* Used to store and manipulate complex numbers

*

* The ComplexNum Class initializes the variables "real" and "imaginary" and sets them

* up in several computations using the formulas in the ComplexNum add, sub, mul, and

* neg methods. The ComplexNum Objects are then returned as a String Object and depending

* on if the Object in the parameter is or isn't a ComplexNum Object, True or false is

* returned respectively.

class ComplexNum {

// setting variables for "real" and "imaginary".

double real, imaginary;

// A default constructor that initializes both "real" and "imaginary" to zero by default.

public ComplexNum() {

this.real = 0;

this.imaginary = 0;

}

// Receives a single double value as a parameter used to initialize the "real" part.

// The "imaginary part will be initialized to zero.

public ComplexNum(double real) {

this.real = real;

this.imaginary = 0;

}

// Receives two double values as parameters, representing the "real" and "imaginary"

// parts respectively.

public ComplexNum(double real, double imaginary) {

this.real = real;

this.imaginary = imaginary;

}

// Used to retrieve the "real" part

public double getReal() {

return real;

}

// Used to retrieve the "imaginary" part

public double getImaginary() {

return imaginary;

}

// Used to modify the "real" part

public void setReal(double real) {

this.real = real;

}

// Used to modify the "imaginary" part

public void setImaginary(double imaginary) {

this.imaginary = imaginary;

}

// Receives a single ComplexNum Object as a parameter and adds it.

// Returns the answer as a new complex number Object

public ComplexNum add(ComplexNum c) {

return new ComplexNum(this.real + c.real, this.imaginary + c.imaginary);

}

// Receives a single complex number as a parameter and subtracts it.

// Returns the answer as a new complex number object.

public ComplexNum sub(ComplexNum c) {

return new ComplexNum(this.real - c.real, this.imaginary - c.imaginary);

}

// Receives a single complex number as a parameter and multiplies it.

// Returns the answers as a new complex number object.

public ComplexNum mul(ComplexNum c) {

return new ComplexNum(this.real * c.real - this.imaginary * c.imaginary,

this.real * c.imaginary + this.imaginary * c.real);

}

// Negates the current object and returns it as a new ComplexNum Object.

public ComplexNum neg() {

return new ComplexNum(-this.real, -this.imaginary);

}

// Returns the ComplexNum Object as a string.

@Override

public String toString() {

String ans = "";

if (this.real != 0) {

ans = ans + this.real;

}

if (this.imaginary != 0) {

if (this.imaginary > 0) {

ans = ans + " + " + this.imaginary + "i";

} else {

ans = ans + " - " + (-this.imaginary) + "i";

}

}

if (ans.isEmpty()) {

ans = "0";

}

return ans;

}

//Receives an Object as a parameter and returns a boolean value.

// True or false depending on if the Object in the parameter is not a ComplexNum Object.

@Override

public boolean equals(Object obj) {

ComplexNum c2 = (ComplexNum) obj;

return this.real == c2.real && this.imaginary == c2.imaginary;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Filing And Computer Database Projects

Authors: Jeffrey Stewart

2nd Edition

007822781X, 9780078227813

More Books

Students also viewed these Databases questions

Question

4. Does cultural aptitude impact ones emotional intelligence?

Answered: 1 week ago

Question

7. Do the organizations social activities reflect diversity?

Answered: 1 week ago