Answer to Question #200230 in Java | JSP | JSF for Muqeet Waraich

Question #200230

Exercise 1:

 

1.     //The max function the max between a and b, it returns a if a == //b

public double max(double a, double b);  

 

2.     //The mult function returns the result of a * b public double mult(double a, double b);

 

3.     //The exist in array function returns the index of the element //‘a’ if //‘a’ exist in array ‘arr’ otherwise returns ‘-1’ public int existsInArray(int [] arr, int a);

 

4.     //Are array equals method compares two arrays and returns true // if the elements of array ‘a’ are equal to elements of array

// ‘b’, element by element. If equals it returns 0, it returns -

// 1 if not

public int areArrayEquals(int [] a, int [] b); 

 

 

Devise four executable test cases for every method in the JUnit notation. See the attached handout for a refresher on the notation.  

 


1
Expert's answer
2021-05-30T07:22:41-0400
// some changes
public class Calculator {


	public double max(double a, double b) {
		if (b > a)
			return b;
		return a;
	}


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


	public int existsInArray(int[] arr, int a) {
		for (int i = 0; i < arr.length; i++) {
			if (arr[i] == a)
				return i;
		}
		return -1;
	}


	public int areArrayEquals(int[] a, int[] b) {
		if (a.length <= b.length)
			return 1;
		for (int i = 0; i < a.length; i++)
			if (a[i] != b[i])
				return 1;
		return 0;
	}
}

import org.junit.Test;
import static org.junit.Assert.assertEquals;


public class CalculatorTest {
	public double maxTest() {
		Calculator calc = new Calculator();
		double res = calc.max(8, 9);
		Assert.AreEqual(9, res);
	}


	public double multTest() {
		Calculator calc = new Calculator();
		double res = calc.mult(7, 8);
		Assert.AreEqual(56, res);
	}


	public int existsInArrayTest() {
		Calculator calc = new Calculator();
		int res = calc.existsInArray(new int[] { 8, 9, 10 }, 10);
		Assert.AreEqual(2, res);
	}


	public int areArrayEqualsTest() {
		Calculator calc = new Calculator();
		int res = calc.areArrayEquals(new int[] { 8, 9, 10 }, new int[] { 8, 9, 11 });
		Assert.AreEqual(1, res);
	}


}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS