As a graduate I went for an interview for a java development role and was doing pretty well in the technical examinations until i came up against this question.
If i
public class VendeningMachine
{
public static final int[] coins = {1, 5, 10, 25};
public static final int[] coinMax = {200, 40, 20, 8};
public static final int[] coinsString = { "Penny", "Nickle", "Dime", "Quarter"};
public static void main(String[] args)
{
}
public static void vendingMachine()
{
for ( int a = 0; a <= coinMax[0]; a++ ) {
for ( int b = 0; b <= coinMax[1]; b++ ) {
for ( int c = 0; c < coinMax[2]; c++ ) {
for ( int d = 0; d < coinMax[3]; d++ ) {
checkCoins(a, b, c, d);
}
}
}
}
}
public static void checkCoins(int penny, int nickle, int dime, int quarter)
{
int total = coins[0] * penny + coins[1] * nickle + coins[2] * dime + coins[3] * quarter;
if ( total == 200 )
printCoins(penny, nickle, dime, quarter);
}
public static void printCoins(int penny, int nickle, int dime, int quarter)
{
System.out.println("Penney : " + penny + " Nickle: " + nickle + " Dime: " + dime + " Quarter: " + quarter);
}
}enter code here