Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

* Can you create Comparable.java and Junit Tes t for the code below. Pleas included comment, so I can understand the code. use java language.

* Can you create Comparable.java and Junit Test for the code below. Pleas included comment, so I can understand the code. use java language.

package pro1;

public class Complex { private double re; private double im; //****************************** // Constructor 1 public Complex(double real, double imag) { re = real; im = imag; } // Constructor 2 public Complex(double real) { re = real; im = 0; } // Constructor 3 public Complex() { re = 0; im = 0; } public double abs() { return Math.sqrt(re * re + im * im); } //********************************** //get method for real part public double getRe() { return this.re; } public void setRe(double re) { this.re=re; } //get method for imaginary part public double getIm() { return this.im; } public void setIm(double im) { this.im=im; } //***************************************** // Setter method public void setreAndim(double re, double im) { this.setRe(re); this.setIm(im); } @Override public String toString() { return "Values:("+ re +")+("+im+"i)"; } public Complex plus(Complex temp) { Complex result=new Complex(); result.re=this.getRe()+temp.getRe(); result.im=this.getIm()+temp.getIm(); return result; } public Complex minus(Complex temp) { Complex result=new Complex(); result.re=this.getRe()-temp.getRe(); result.im=this.getIm()-temp.getIm(); return result; } public Complex times(Complex temp) { Complex result=new Complex(); result.re=(this.getRe()*temp.getRe())-(this.getIm()*temp.getIm()); result.im=(this.getRe()*temp.getIm())+(this.getIm()*temp.getRe()); return result; } public Complex division(Complex temp) { Complex result=new Complex(); result.re=(((this.getRe()*temp.getRe())+(this.getIm()*temp.getIm()))/(Math.pow(temp.getRe(),2)+ Math.pow(temp.getIm(),2))); result.im=(((this.getRe()*temp.getIm())-(this.getIm()*temp.getRe()))/(Math.pow(temp.getRe(),2)+ Math.pow(temp.getIm(),2))); return result; } //****************************** public static void main(String[] args) { Complex complex=new Complex(); Complex complex1=new Complex(1,4); Complex complex2=new Complex(3,-5); System.out.println("complex" + complex); System.out.println("complex1" + complex1); System.out.println("complex2" + complex2); System.out.println("Plus" + complex1.plus(complex2)); System.out.println("Minus" + complex1.minus(complex2)); System.out.println("Times" + complex1.times(complex2)); System.out.println("Division" + complex1.division(complex2)); } } image text in transcribed

CSC SE Problem 1 A complex number is a number in the form a + bi, where a and b are real numbers and iisv The numbers a and b respectively. You can perform addition, subtraction, multiplication, and division for complex are known as the real part and imaginary part of the complex number, numbers using the following formulas: Note: The following output after the equals sign results in re+ im i. addition subtraction multiplication: division (a + bi) (c + di) (a +c)+(b + di (a + bi) - (c +di) (a c)+ (b-d)i (a + bi) (c + di) (ac-bd) (bc + ad)i (a + bi) (ac + bd) (bc- ad) absolute value: A complex number can be interpreted as a point on a plane by identifying the (a, b) values as the coordinates of the point. The absolute value of the complex number corresponds to the distance of the point to the origin. Design a class named Complex for representing complex numbers and the methods plus, minus, times, divideBy, and abs for performing complex number operations, and override tostring for returning a string representation for a complex number. 1 The tostring method returns (re + im) as a string. If im is 0, it simply returns re. 2. Your Complex class should implement a generic (programmer defined) Comparable interface. Provide three constructors Complex (re,im), Complex (re), and Complex (). Complex () create a Complex object with a real number 0, Complex(re) creates a Complex object with real number re. 4. Also provide return value methods getRe() and get Im0 of the complex number respectively Draw the UML class diagram and implement the class. Write a test program for the Complex class

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

The Database Experts Guide To Database 2

Authors: Bruce L. Larson

1st Edition

0070232679, 978-0070232679

Students also viewed these Databases questions

Question

How are integral emotions related to behavior?

Answered: 1 week ago