Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

upload of the entire source code included in this assignment with the ADDITION o

ID: 3810010 • Letter: U

Question

upload of the entire source code included in this assignment with the ADDITION of conducting the two additional tests as commented in the source.

ArithmeticFunctions.java

public class ArithmeticFunctions {
   public int sub(int a, int b) {
       return a - b;
   }

   public int mult(int a, int b) {
       return a * b;
   }

   public int add(int a, int b) {
       return a + b;
   }

   public double div(int a, int b) {
       if (b != 0) {
           System.out.println("b cannot be 0");
           return 0.0;
       } else {
           return a / b;
       }

   }
}

ArithmeticTest.java

import junit.framework.TestCase;

public class ArithmeticTest extends TestCase {
   ArithmeticFunctions functions = new ArithmeticFunctions();
   ArithmeticFunctions functions1;

   public void test() {
       assertEquals("Addition of 5 and 7 is 12", 12, functions.add(5, 7));
   }
     
   public void testForSub() {
       assertEquals("Subtraction of 5 and 7 is 2", 2, functions.sub(5, 7));
   }
     
   public void testForNull(){
       assertNull("Null value found" , functions1);
   }
   //students: test again for null value given functions1.add()
  
   //students: create an assert to test for div by zero!

}

Explanation / Answer

Hi,

Please see below the updated classes

ArithmeticFunctions.java

public class ArithmeticFunctions {
public int sub(int a, int b) {
return a - b;
}
public int mult(int a, int b) {
return a * b;
}
public int add(int a, int b) {
return a + b;
}
public double div(int a, int b) {
if (b == 0) {
System.out.println("b cannot be 0");
return 0.0;
} else {
return a / b;
}
}
}

ArithmeticTest.java

import junit.framework.TestCase;
public class ArithmeticTest extends TestCase {
ArithmeticFunctions functions = new ArithmeticFunctions();
ArithmeticFunctions functions1;

public void test() {
assertEquals("Addition of 5 and 7 is 12", 12, functions.add(5, 7));
}

public void testForSub() {
assertEquals("Subtraction of 5 and 7 is 2", 2, functions.sub(7, 5));
}

public void testForNull(){
assertNull("Null value found" , functions1);
}
//students: test again for null value given functions1.add()
public void testForNotNull(){
   functions1 = new ArithmeticFunctions();
assertNotNull("No Null value found" , functions1.add(3, 3));
}
  
//students: create an assert to test for div by zero!
public void testForDivByZero(){
   assertEquals("Division of 8 and 0 ", 0.0, functions.div(8, 0));
}
}