Hello! I’m new to testing in Java and I need some help. Please take the time to
ID: 3725296 • Letter: H
Question
Hello! I’m new to testing in Java and I need some help. Please take the time to explain things to me so I could understand it more. Thanks.
Here is what I have to do
1. Use assertNotNull() to test the Complex(double, double) constructor
2. Use assertEquals(double, double, double) to test if getReal() returns the correct value set using the Complex(int, int) constructor
3. Use assertEquals(double, double, double) to test if setReal() correctly resets the value set using the Complex(double, double) constructor
4. Use the no-arg Complex() constructor, and then use the real and imaginary setters to set new integer values. Then use assertTrue(boolean) to test the validity of your newly-added equals(int, int) method
5. Using assertTrue(boolean), test that two Complex numbers created using the Complex(String[]) constructor and the Complex(String, String) constructor give the correct result using the multiply() method. Use equals(Complex) to compare the actual and expected result
6. Divide two Complex numbers and use the result to test your equals(double expected, double actual, double delta) method, with delta = .0000001
Here is my class code
And here is me trying to do 1,2,3 which I failed horribly
eclipse-workspace-CS18284-Lab04/src/cst8284/calculator/Complexava-Eclipse File Edit Scurce Refatr Navigate Search Preject Rur Winduw Help 1 package cst8284.calculator 3 public class Conplex private double real = e; privata doublo ina // Complex ranstructor that takrs in a single string, r.E- ·2-4i. 9 public Conplex(String Str this(cstr.split"?V)"): splits cstr ator into an array of strings having two elenents The first elenrnt of thr resultant array wil1 he the rral portion, while the second is the imaginary porLio This array is passed to Lhe riext constructor 12 13 14 Complex constructor that takes in an array of two strings fron the above constructor 17 1 public Conplex(String] cStr) 19 this(cStr[el, cstr[11); 21 23 Conplex ranstructor that takrs two separate strings as parameters, r.g. "" and Ai" 24 public Conplex(Strirng r, String i) this(Integer parseint(r), Integer parselnt(i.replace"))) 06 Complex ronstructor that takrs in tun ints as paraneters, .e-2 and- public Conplex(int r, int i) 31 this (double)r, (double)i); 34 // Complex ranstructor that takes in tun ints and stares then as floats, r.E- as ).e and .4. 6 public Conplex(double , double i) 3/ this.setReal(r); this-set Tmng(1 ) ; 11 idefault Complex constructorg it should rhain autonatically 42 public Conplex)this(,8); 45 public double getReal Smart Insert71: 109Explanation / Answer
Find below the code for Complex.java and TestComplex.java. It has the functionality from Question 1, 2, 3, 4. Question 5 and 6 can't be attempted because of unavailability of ComplexCalculator.java. They need the multiply() and divide() method. Please read the comments to understand the functionality of each requirement.
I would appreciate if you spare a couple of seconds to rate the answer. Thanks.
********************************TestComplex.java*********************************
import static org.junit.Assert.*;
public class TestComplex {
/*
1. Use assertNotNull() to test the Complex(double, double) constructor
Asserts are used to validate the output of a function/ application. assertNotNull verifies that the object returned is a null or not
if the object returned turns out to be null then it throws an AssertionError.
*/
public void testNull() {
Complex c = new Complex(5.6, 56.5);
assertNotNull(c);
/*
As mentioned above it will give an error if the returned object is null. The error would be something like this
Exception in thread "main" java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:86)
at org.junit.Assert.assertTrue(Assert.java:41)
at org.junit.Assert.assertNotNull(Assert.java:712)
at org.junit.Assert.assertNotNull(Assert.java:722)
at testingapp.TestComplex.testNull(TestComplex.java:22)
at testingapp.TestComplex.main(TestComplex.java:39)
*/
}
/*
2. Use assertEquals(double, double, double) to test if getReal() returns the correct value set using the Complex(int, int) constructor
assertEquals(expected,actual) will check if the expected outcome and actual outcome are the same. In this case it will check if
c.getReal() returns the value set using the Complex(int, int) constructor.
*/
public void testReal() {
Complex c = new Complex(5, 6);
assertEquals(5, c.getReal());
//The Assertion fails since the constructor converts the int value to double before intializing real part of the complex number
//So the value returned is 5.0. However, we expect a value of 5. Hence, the expected and actual outcome vary resulting in an assertion fail
}
/*
3. Use assertEquals(double, double, double) to test if setReal() correctly resets the value set using the Complex(double, double) constructor
The 3 parameters over here are 1.expected outcome 2. actual outcome 3.delta
delta is the allowed deviation.
Because you are dealing with double values so it is possible that you might be looking for an almost same value.
1. expected outcome can be 9.85 and actual outcome is 9.845 and if you have set delta as 0.01. This would mean that a deviation
of maximum 0.01 is permitted and it must pass the assertion, not fail it.
*/
public void testResetValue(){
Complex c=new Complex(5.6,6.7);//create a Complex object with 5.6 as real and 6.7 as imaginary part
//reset the real part
c.setReal(9.8);//the new value becomes 9.8
//check if the value is successfully being reset
assertEquals(9.9,c.getReal(),0.1);
/*
0.1 is the maximum deviation/ difference that is allowed between the expected and actual outcome
so the assertion will succeed for any expected value from 9.71 to 9.9 and would throw an error for any other value
*/
}
/*
4. Use the no-arg Complex() constructor, and then use the real and imaginary setters to set new integer values.
Then use assertTrue(boolean) to test the validity of your newly-added equals(int, int) method
assertTrue(boolean a) verifies the value of a and throws an erros if a is false and succeeds if the value is true.
*/
public void testNoArg(){
Complex c=new Complex();
//Create a Complex object using default constructor
c.setReal(5);//Set the integer value of real part
c.setImag(6);//Set the integer value of imaginary part
assertTrue(c.equals(5, 6));
/*
PS: The assertion will fail if floating point numbers are set in the real or imaginary part
since the equals method must accept integer values
*/
}
/*
5. Using assertTrue(boolean), test that two Complex numbers created using the Complex(String[]) constructor and the Complex(String, String) constructor give the correct result using the multiply() method. Use equals(Complex) to compare the actual and expected result
*/
/*
6. Divide two Complex numbers and use the result to test your equals(double expected, double actual, double delta) method, with delta = .0000001
*/
public static void main(String[] args) {
TestComplex tc = new TestComplex();
tc.testNull();//Test 1
tc.testReal();//Test 2-> Will fail. You can comment this to test others at the same time
tc.testResetValue();//Test 3
tc.testNoArg();//Test 4
}
}
*********************************************Complex.java************************************************
public class Complex {
private double real=0;
private double imag=0;
public Complex(String cStr){
this(cStr.split("(?=\+)|(?=\-)"));
}
public Complex(String[] cStr){
this(cStr[0],cStr[1]);
}
public Complex(String r, String i){
this(Integer.parseInt(r),Integer.parseInt(i.replace("i", "")));
}
public Complex(int r,int i){
this((double)r,(double)i);
}
public Complex(double r, double i){
this.setReal(r);
this.setImag(i);
}
public Complex(){
this(0,0);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
}
public double getReal() {
return real;
}
public void setReal(double real) {
this.real = real;
}
public double getImag() {
return imag;
}
public void setImag(double imag) {
this.imag = imag;
}
public Complex getCompex(){
return this;
}
public String toString(){
if(getImag()<0) return getReal()+"-"+(-getImag())+"i";
return getReal()+"+"+getImag()+"i";
}
public boolean equals(Complex c){
return(Double.compare(this.real, 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)<delta &&Math.abs(this.getImag()-imag)<delta;
}
public boolean equals(String c){
return equals(new Complex(c));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.