import java.util.ArrayList; import java.util.Arrays; /** * Multiples Lab. * @author cs416 * @version 1 */ public class Multiples { private int[] result; private ArrayList numbers; /** * Constructor. * @param numbers Values to distribute into multiples location */ public Multiples(ArrayList numbers) { this.numbers = numbers; this.result = new int[numbers.size()]; } /** * return true if number is a multiple of divisor. * @param num The number * @param div The divisor * @return boolean */ public boolean isMultiple(int num, int div) { throw new UnsupportedOperationException("TO BE IMPLEMENTED"); } /** * Assigns the paramter i to the index n in the result array. * @param n The index in the array * @param i The value to store */ public void assignValue(int n, int i) { throw new UnsupportedOperationException("TO BE IMPLEMENTED"); } /** * unassign the value at given position. * @param n The index to unassign */ public void unassignValue(int n) { throw new UnsupportedOperationException("TO BE IMPLEMENTED"); } /** * DO NOT CHANGE! * Method to begin solving the problem for the first index in results. * * @return True if solution was found, false if none was possible. */ public boolean solve() { return solve(0); } /** * Recursive method to solve the problem from a given index onward. Assumes * that any index before n was already assigned a valid value. * * @param n The current index in the results array to find a value for. * @return True if solution was found, false if none was possible from this index. */ private boolean solve(int n) { throw new UnsupportedOperationException("TO BE IMPLEMENTED"); } /** * returns the retult array. * @return int[] */ public int[] getResult() { return result; } /** * return the numbers ArrayList. * @return ArrayList */ public ArrayList getNumbers() { return numbers; } /** * main function. * @param args Command line args */ public static void main(String[] args) { Integer[] numbers; Multiples m; //FIRST TEST (SOLUTION POSSIBLE WITHOUT BACKTRACKING) numbers = new Integer[]{1, 6, 21, 40, 25}; m = new Multiples(new ArrayList(Arrays.asList(numbers))); if (m.solve()) { System.out.println("Found solution for " + Arrays.toString(numbers)); } else { System.out.println("No solution possible!"); } System.out.println(Arrays.toString(m.getResult())); System.out.println(); //SECOND TEST (SOLUTION POSSIBLE WITH BACKTRACKING) numbers = new Integer[]{15, 10, 6, 12, 16}; m = new Multiples(new ArrayList(Arrays.asList(numbers))); if (m.solve()) { System.out.println("Found solution for " + Arrays.toString(numbers)); } else { System.out.println("No solution possible!"); } System.out.println(Arrays.toString(m.getResult())); System.out.println(); //THIRD TEST (NO SOLUTION) numbers = new Integer[]{15, 10, 6, 12, 17}; m = new Multiples(new ArrayList(Arrays.asList(numbers))); if (m.solve()) { System.out.println("Found solution for " + Arrays.toString(numbers)); } else { System.out.println("No solution possible!"); } System.out.println(Arrays.toString(m.getResult())); } }