Question
Can someone check if my assertions are correct? I did test them and they worked fine, but there are still some concerns which I commented
Can someone check if my assertions are correct? I did test them and they worked fine, but there are still some concerns which I commented on my test class attached below (top comments are the tests I needed to make)
I have provided the code for Complex class
package cst8284.calculator;
public class Complex {
private double real = 0;
private double imag = 0;
// Complex constructor that takes in a single string, e.g. "2-4i"
public Complex(String cStr){
this(cStr.split("(?=\\+)|(?=\\-)")); // splits cStr at + or - into an array of strings having two elements
// The first element of the resultant array will be the real portion,
// while the second is the imaginary portion. This array is passed to the next constructor.
}
// Complex constructor that takes in an array of two strings from the above constructor
// e.g. cStr[0]="2", cStr[1]="-4i"
public Complex(String[] cStr){
this(cStr[0], cStr[1]);
}
// Complex constructor that takes two separate strings as parameters, e.g. "2" and "-4i"
public Complex(String r, String i){
this(Integer.parseInt(r), Integer.parseInt(i.replace("i", "")));
}
// Complex constructor that takes in two ints as parameters, e.g. 2 and -4
public Complex(int r, int i){
this((double)r, (double)i);
}
// Complex constructor that takes in two ints and stores them as floats, e.g. as 2.0 and -4.0
public Complex(double r, double i){
this.setReal(r);
this.setImag(i);
}
//default Complex constructor; it should chain automatically
public Complex(){this(0,0); }
public double getReal () {
return real;
}
public double getImag () {
return imag;
}
public void setReal (double real) {
this.real = real;
}
public void setImag (double imag) {
this.imag = imag;
}
public Complex getComplex () {
return this;
}
public String toString () {
if (getImag()
return getReal() + " + " + getImag() + "i";
}
public boolean equals (Complex c) {
return (Double.compare(this.getReal(), c.real) == 0 && Double.compare(this.getImag(), c.imag) == 0);
}
public boolean equals (int real, int imag) {
return equals(new Complex(real, imag));
}
public boolean equals (double real, double imag, double delta) {
return Math.abs(this.getReal()-real)
}
public boolean equals (String c) {
return equals(new Complex(c));
}
}
And ComplexCalculator class
package cst8284.calculator;
public class ComplexCalculator {
private java.util.Scanner op = new java.util.Scanner(System.in);
private Complex c; // stores result of current calculation for use in next calculation
public ComplexCalculator(Complex c1, Complex c2){
System.out.println("Which math operation do you wish to perform? Enter +, -, *, /");
char mathOp = op.nextLine().charAt(0);
switch (mathOp){
case '+':
c = plus(c1, c2);
break;
case '-':
c = subtract(c1, c2);
break;
case '*':
c = multiply(c1, c2);
break;
case '/':
c = divide(c1, c2);
break;
default:
System.out.println("Unknown operation requested");
}
}
public ComplexCalculator () {}
public Complex plus(Complex c1, Complex c2){
double real = c1.getReal() + c2.getReal();
double imag = c1.getImag() + c2.getImag();
c = new Complex(real, imag); // save the result to this class's Complex number
return(c);
}
public Complex subtract(Complex c1, Complex c2){
double real = c1.getReal() - c2.getReal();
double imag = c1.getImag() - c2.getImag();
c = new Complex (real, imag);
return (c);
}
public Complex multiply(Complex c1, Complex c2){
double real = c1.getReal() * c2.getReal() - c1.getImag() * c2.getImag();
double imag = c1.getReal() * c2.getImag() + c2.getReal() * c1.getImag();
c = new Complex (real, imag);
return (c);
}
public Complex divide(Complex c1, Complex c2){
double real, imag;
if ((Math.pow(c2.getReal(),2) == 0) || (Math.pow(c2.getImag(), 2) == 0)) {
real = 0;
imag = 0;
System.out.println("Divided-by-zero error detected.");
c= new Complex (real, imag);
} else {
real = (c1.getReal() * c2.getReal() + c1.getImag() * c2.getImag()) / (c2.getReal() * c2.getReal() + c2.getImag() * c2.getImag());
imag = (c2.getReal() * c1.getImag() - c1.getReal() * c2.getImag()) / (c2.getReal() * c2.getReal() + c2.getImag() * c2.getImag());
c = new Complex (real, imag);
}
return (c);
}
public Complex getComplexResult(){
return c;
}
public String toString(){
return c.toString();
}
}
eclipse-workspace-CS18284-L File Edit Scurce Refatr Navigate Search Preject Rur Winduw Help E Navigator JUnit & , e-lAEl q, 9, | 1 package test . c st8284. calculator ; Finishod after 185.764 scconds 3import static org-junitAssert.; Errors: 0 Failures: 0 1. Use assertNotul) to test thr Complex(doublr, douhle) ranstructorl test.cst8284 calculator.TestComplex [Runner JUnit 4 2. Use asserttquals (double, double, double) to test if geteal) returns the correct value set using the Complex(int, int) constructor 9 3. Use assertEquals(double, double, double) to test if setReal) errectly resets the value set using the Conplex(double, double) censLructor 1 4 . Ikr the no-are cnmlex() constructor, and then use the rral and imaginary setter to set nm. intreer values 12 Then use asser LTrue(boolean) Lo test the validity of your nely-aded equals(in, int) neLhed 1 sing assertTrue(hoolran), test that tun Complrx numbers rreatrd using the Complex(Stringl]) canstructor and thr Conplex(Strine, String) 15 cens tructor give the correct result using the multiply ed Use equals (Complex) to compare the actual and expected result 6.Divide tun Complex nunhers and use thr result to test your equals(ouhle experted, double actual, double drlta) nethod, with delta -.001 import are. junit.Test; 21 22 import cst8284.calculator.Conplex; 3 import cst828 rallator.canplexCalrulatar; 24 25 publ1c class estonplex >126 27 Test hailure Irace #28 public void testLomplextonstructor() { 09 1e 31 Complex now Complrx (3.0, 7.0); asserCNotmill(); 33 Test |14 public void testoetReal(){ Complex nw Capx3, ); assertEquals(c.getReal), 3, 1-5); 16 9 Test 49 public void testsetReal() { Complex-naw Coprx3., .0); c.setReal(); double a c.getReal(); assertFquais(a, A, IF-14); 43 45 Smart Insert 5:73Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started