Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need this done in Java please. JUnit 4 Need a class that tests all constructors and methods for the following class. All different cases need

Need this done in Java please. JUnit 4

Need a class that tests all constructors and methods for the following class. All different cases need to be tested. setUp method should also be included in the class. should be called before each test method is executed.

import java.util.*;

class ComplexNum {

private double real; private double imaginary;

public ComplexNum() { setReal(0); setImaginary(0); } public ComplexNum(double real) { setReal(real); setImaginary(0); } public ComplexNum(double real, double imaginary) { setReal(real); setImaginary(imaginary); } public double getReal() { return real; } public double getImaginary() { return imaginary; } public void setReal(double real) { this.real = real;

} public void setImaginary(double imaginary) { this.imaginary = imaginary; }

public ComplexNum add(ComplexNum obj) { double reals = this.real + obj.real; double imaginaries = this.imaginary + obj.imaginary; return new ComplexNum(reals, imaginaries);

}

public ComplexNum sub(ComplexNum obj) { double reals = this.real - obj.real; double imaginaries = this.imaginary - obj.imaginary;

return new ComplexNum(reals, imaginaries); }

public ComplexNum mul(ComplexNum obj){ double reals = (this.real * obj.real) - (this.imaginary * obj.imaginary); double imaginaries = (this.real * obj.imaginary) + (this.imaginary * obj.real);

return new ComplexNum(reals, imaginaries); } public ComplexNum neg() { return new ComplexNum(-this.real, -this.imaginary); } public String toString() { StringBuilder sb = new StringBuilder(); if (this.real != 0) { sb.append(this.real); if (this.imaginary > 0) { sb.append("+"); } } if (this.imaginary != 0) { sb.append(this.imaginary).append("i"); } if (sb.length() == 0) { sb.append("0"); } return sb.toString(); }

@Override public boolean equals(Object obj) { if (!(obj instanceof ComplexNum)) { return false; } ComplexNum other = (ComplexNum) obj; return this.real == other.real && this.imaginary == other.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

MySQL Crash Course A Hands On Introduction To Database Development

Authors: Rick Silva

1st Edition

1718503008, 978-1718503007

More Books

Students also viewed these Databases questions

Question

5.6 Describe alternatives to recruitment?

Answered: 1 week ago