4. Suppose a class called MyClass has a subroutine with signature * Takes two in
ID: 3606704 • Letter: 4
Question
4. Suppose a class called MyClass has a subroutine with signature * Takes two integer parameters and returns the absolute value of their difference. * Oparam x integer *Oparam y integer public static abs(int x, int y) which, according to the documentation, computes and returns the absolute value of the difference between its two parameters. (a) Show a code snippet demonstrating how one would use this function to compute the absolute value of two integers (can be hard-coded). (b) Write an implementation of the function in Java. (e) Describe a few test cases to be sure that your function computes the correct value for different possible parameters.Explanation / Answer
Ans:
a) code snippet
public static abs(int num1,int num2){
return num1-num2;
b) Java implementation(hard coded)
public class Calculator{
public static int abs(int num1,int num2){
num1=9;
num2=4;
return num1-num2;
}
}
c)Test case
import junit.framework.TestCase;
public class CalculatorTest extends TestCase {
Calculator cal=new Calculator();
public CalculatorTest(String name) {
super(name);
}
public void abs() {
assertEquals(5,cal.abs(9,4));
}
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.