/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package javaapplication1; import java.util.ArrayList; import java.util.Arrays; /** * * @author zzaier */ public class JavaApplication1 { /** * @param args the command line arguments */ public static void main(String[] args) { ArrayList myList = new ArrayList(Arrays.asList(155, 9, 2, 0, 7, 13, 12, 17, 20, 27)); int result = closestInteger(myList, 133); System.out.println("the result is " + result); result = largestMultiple(myList, 133); System.out.println("the result is " + result); } public static int closestInteger(ArrayList myArray, int value) { int closestValue = myArray.get(0); int currentDifference, mainDifference; mainDifference = positiveDifference(closestValue, value); for (int i = 0; i < myArray.size(); i++) { currentDifference = positiveDifference(myArray.get(i), value); if (currentDifference < mainDifference) { mainDifference = currentDifference; closestValue = myArray.get(i); } } return closestValue; } public static int positiveDifference(int value1, int value2) { if (value1 < value2) { return value2 - value1; } else { return value1 - value2; } } public static int largestMultiple (ArrayList myArray, int value) { int position =-1; int largestMultiple=computeMinValue(myArray); for (int i = 0; i < myArray.size(); i++) { System.out.println("before "+position+" - " +largestMultiple); if(myArray.get(i)%value==0) if(myArray.get(i)>largestMultiple){ position=i; largestMultiple=myArray.get(i); System.out.println("after "+position+" - " +largestMultiple); } } return position; } public static int computeMinValue(ArrayList myArray) { int min=myArray.get(0); for (int i = 0; i < myArray.size(); i++) { if(min>myArray.get(i)) min= myArray.get(i); } return min; } }