Note that there are some explanatory texts on larger screens.

plurals
  1. POerror: illegal start of expression; reached end of file while parsing
    text
    copied!<p>I'm new to Java programming. This is part of homework questions. I need to provide a set of comparison methods on arrays. I tried my best and this is my attempt so far. Can somebody enlighten me? Any help will be much appreciated!</p> <p>Several requirements I need to follow:</p> <ul> <li>You must not add any public methods to the Selector class. You are free to add any private methods that you think are appropriate, but you can't add any public methods.</li> <li>You must not add any fields, either public or private, to the Selector class.</li> <li>You must not import anything other than java.util.Arrays, but you are not required to import this at all. </li> <li>You are only allowed to use sorting as part of your solution in the nearest and farthest methods.</li> <li>You must not modify the existing constructor or add other constructors. This class is designed to be strictly a provider of static methods and should not be instantiated.</li> </ul> <p><br></p> <pre><code>import java.util.Arrays; /** * A class that contains various selection methods on arrays. * All methods assume that the array is completely filled with * non-null values. If this is not true, the behavior is not specified. * All the methods throw an IllegalArgumentException if the array * parameter is null or has zero length. The array parameter to * each method is guaranteed to not be changed as a result of the call. */ public final class Comparision{ /** * C A N N O T I N S T A N T I A T E T H I S C L A S S . * */ private Comparision(){ } /** * Return the element of a nearest to val. This method throws an * IllegalArgumentException if a is null or has zero-length. * The array a is not changed as a result of calling this method. * * @param a the array to be searched * @param val the reference value * @return the element a[i] such that ABS(a[i] - val) is minimum * */ public static int nearest(int[] a, int val) { int[] a = new int[10]; if (a == null || a.length == 0){ throw new IllegalArgumentException("a is null or has zero-length"); } int idx = 0; int distance = Math.abs(a[0]-val); for(int c = 1; c&lt; a.length; c++){ int cdistance = Math.abs(a[c] - val); if(cdistance &lt; distance){ idx=c; distance = cdistance; } } int theNumber = a[idx]; return theNumber; } /** * Return the element of a farthest from val. This method throws an * IllegalArgumentException if a is null or has zero-length. * The array a is not changed as a result of calling this method. * * @param a the array to be searched * @param val the reference value * @return the element a[i] such that ABS(a[i] - val) is maximum * */ public static int farthest(int[] a, int val) { int[] a = new int[10]; if (a == null || a.length == 0){ throw new IllegalArgumentException("a is null or has zero-length"); } int idx = 0; int distance = Math.abs(a[0]-val); for(int c = 1; c&lt; a.length; c++){ int cdistance = Math.abs(a[c] - val); if(cdistance &gt; distance){ idx=c; distance = cdistance; } } int theNumber = a[idx]; return theNumber; } /** * Return the k elements of a nearest to val. * The array a is not changed as a result of calling this method. * This method throws an IllegalArgumentException if k is negative. * This method returns an array of zero length if k == 0 or if * k &gt; a.length. * * @param a the array to be searched * @param val the reference value * @param k the number of near elements to identify * @return the k elements a[i] such that ABS(a[i] - val) * are the k smallest evaluations * */ public static int[] nearestK(int[] a, int val, int k) { int[] b = new int[10]; for (int i = 0; i &lt; b.length; i++){ b[i] = Math.abs(a[i] - val); } Arrays.sort(b); int[] c = new int[w]; w = 0; for (int i = 0; i &lt; k; i++){ if (k &lt; 0){ throw new IllegalArgumentException("k is not invalid!"); } c[w] = b[i]; w++; } return c; } /** * Return the k elements of a farthest from val. * The array a is not changed as a result of calling this method. * This method throws an IllegalArgumentException if k is negative. * This method returns an array of zero length if k == 0 or if * k &gt; a.length. * * @param a the array to be searched * @param val the reference value * @param k the number of far elements to identify * @return the k elements a[i] such that ABS(a[i] - val) * are the k largest evaluations * */ public static int[] farthestK(int[] a, int val, int k) { int[] b = new int[10]; for (int i = 0; i &lt; 10; i++){ b[i] = Math.abs(a[i] - val); } Arrays.sort(b); int[] c = new int[w]; int w = 0; for (int i = array.length-1; i &gt;= array.length-k; i--){ if (k &lt; 0){ throw new IllegalArgumentException("k is not invalid!"); } else if (k == 0 || k &gt; a.length){ int[] c = ""; } c[w] = b[i]; w++; } return c; } /** * Return the number of elements of a that are greater than val. * * @param a the array to be searched * @param val the reference value * @return the number of elements a[i] such that a[i] &gt; val * */ public static int numGreater(int[] a, int val) { int w = 0; for (int i = 0; i &lt; a.length; i++){ if ((a[i] - val)&gt;0){ w++; } return w; } /** * Return an array of all the elements of a that are greater than val. * If a contains no elements greater than val, this method returns an * array of zero length. * * @param a the array to be searched * @param val the reference value * @return the elements a[i] such that a[i] &gt; val * */ public static int[] greater(int[] a, int val){ int[] b = new int[w]; int w = 0; for (int i = 0; i &lt; a.length; i++){ if ((a[i] - val)&gt;0){ b[w] = a[i]; w++; } } if (w = 0){ int[] b = {}; } return b; } /** * Return the number of elements of a that are less than val. * * @param a the array to be searched * @param val the reference value * @return the number of elements a[i] such that a[i] &lt; val * */ public static int numLess(int[] a, int val) { int w = 0; for (int i = 0; i &lt; a.length; i++){ if ((a[i] - val)&lt;0){ w++; } return w; } /** * Return an array of all the elements of a that are less than val. * If a contains no elements less than val, this method returns an * array of zero length. * * @param a the array to be searched * @param val the reference value * @return the elements a[i] such that a[i] &lt; val * */ public static int[] less(int[] a, int val) { int[] b = new int[w]; int w = 0; for (int i = 0; i &lt; a.length; i++){ if ((a[i] - val)&lt;0){ b[w] = a[i]; w++; } } if (w = 0){ int[] b = {}; } return b; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload