public static int numOfCoins(int[] coinsList, int value){ /** Write your code he
ID: 3592464 • Letter: P
Question
public static int numOfCoins(int[] coinsList, int value){
/** Write your code here to find out minimum number of coins required to provide the change for a given value.
This method will have a coinsList array, which can be any set of coin values that should be used to test the code. You cannot hard code this array because the array value will be giving at runtime, and an int value which specifies the value for which we need to calculate the minimum number of coins to be changed. This method should return the number of coins
}
/** Test The Program via Command line or Terminal:
javac -cp .:junit-4.12.jar CoinChangeTest.java
java -cp .:junit-4.12.jar:hamcrest-core-1.3.jar org.junit.runner.JUnitCore CoinChangeTest
Here the file we will use to test your code:
//Test File:
import static org.junit.Assert.*;
import org.junit.Test;
import java.util.Arrays;
public class CoinChangeTest {
@Test
public void testCoinChange1() {
CoinChange c = new CoinChange();
int[] denomination = {1,5,10,25};
int value = 37;
int answer = 4;
assertEquals(c.NumberofCoins(denomination,value),answer);
}
@Test
public void testCoinChange2() {
CoinChange c = new CoinChange();
int[] denomination = {1,6,10};
int value = 11;
int answer = 2;
assertEquals(c.NumberofCoins(denomination,value),answer);
}
@Test
public void testCoinChange3() {
CoinChange c = new CoinChange();
int[] denomination = {1,6,10};
int value = 13;
int answer = 3;
assertEquals(c.NumberofCoins(denomination,value),answer);
}
@Test
public void testCoinChange4() {
CoinChange c = new CoinChange();
int[] denomination = {1,6,10};
int value = 17;
int answer = 3;
assertEquals(c.NumberofCoins(denomination,value),answer);
}
@Test
public void testCoinChange5() {
CoinChange c = new CoinChange();
int[] denomination = {1,9,15};
int value = 37;
int answer = 5;
assertEquals(c.NumberofCoins(denomination,value),answer);
}
@Test
public void testCoinChange6() {
CoinChange c = new CoinChange();
int[] denomination = {1,9,15};
int value = 54;
int answer = 4;
assertEquals(c.NumberofCoins(denomination,value),answer);
}
}
Possible errors:
testCoinChange1(CoinChangeTest)
java.lang.AssertionError: expected:<24> but was:<4> at CoinChangeTest.testCoinChange1(CoinChangeTest.java:15)
testCoinChange2(CoinChangeTest)
java.lang.AssertionError: expected:<3> but was:<2> at CoinChangeTest.testCoinChange2(CoinChangeTest.java:24)
testCoinChange3(CoinChangeTest)
java.lang.AssertionError: expected:<4> but was:<3> at CoinChangeTest.testCoinChange3(CoinChangeTest.java:33)
Explanation / Answer
public class CoinChange
{
static int minCoins(int coinList[], int m, int inputnum)
{
if (inputnum == 0)
return 0;
int res = Integer.MAX_VALUE;
for (int i=0; i<m; i++)
{
if (coinList[i] <= V)
{
int sub_res = minCoins(coinList, m, V-coinList[i]);
if (sub_res != Integer.MAX_VALUE && sub_res + 1 < res)
res = sub_res + 1;
}
}
return res;
}
public static void main(String args[])
{
int coinList[] = {9, 6, 5, 1};
int m = coinList.length;
int V = 11;
System.out.println("minimum number of coins required to provide the change for a given value"+ minCoins(coinList, m, V) );
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.