Domanda

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!

Several requirements I need to follow:

  • 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.
  • You must not add any fields, either public or private, to the Selector class.
  • You must not import anything other than java.util.Arrays, but you are not required to import this at all.
  • You are only allowed to use sorting as part of your solution in the nearest and farthest methods.
  • 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.


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< a.length; c++){
         int cdistance = Math.abs(a[c] - val);
         if(cdistance < 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< a.length; c++){
       int cdistance = Math.abs(a[c] - val);
       if(cdistance > 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 > 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 < 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 < k; i++){
       if (k < 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 > 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 < 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 >= array.length-k; i--){
       if (k < 0){
       throw new IllegalArgumentException("k is not invalid!");
       }
       else if (k == 0 || k > 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] > val
   *
   */
  public static int numGreater(int[] a, int val) {
     int w = 0;
     for (int i = 0; i < a.length; i++){
       if ((a[i] - val)>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] > val
   *
   */
  public static int[] greater(int[] a, int val){
     int[] b = new int[w];
     int w = 0;
     for (int i = 0; i < a.length; i++){
       if ((a[i] - val)>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] < val
   *
   */
  public static int numLess(int[] a, int val) {
     int w = 0;
     for (int i = 0; i < a.length; i++){
       if ((a[i] - val)<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] < val
   *
   */
  public static int[] less(int[] a, int val) {
     int[] b = new int[w];
     int w = 0;
     for (int i = 0; i < a.length; i++){
       if ((a[i] - val)<0){
       b[w] = a[i];
       w++;
       }
     }
     if (w = 0){
     int[] b = {};
     }
  return b;
  }
}
È stato utile?

Soluzione 2

I think you're missing brackets or semi-colons:

This should be:

  public static int numGreater(int[] a, int val) {
     int w = 0;
     for (int i = 0; i < a.length; i++){
       if ((a[i] - val)>0){
       w++;
     }

  return w;
  }

Should be:

  public static int numGreater(int[] a, int val) {
     int w = 0;
     for (int i = 0; i < a.length; i++){
       if ((a[i] - val)>0){
           w++;
       }//<---missing this fella
     }

     return w;
  }

Altri suggerimenti

You problem is in lines 194-197: you aren't closing your if statement, so your close curly braces after that are all messed up. To fix it, change numLess to:

public static int numLess(int[] a, int val) {
    int w = 0;
    for (int i = 0; i < a.length; i++){
        if ((a[i] - val)<0){
            w++;
        } // This is the curly brace you are missing
    }
    return w;
}

EDIT: the other answer is right as well; you have the same problem in both numGreater and numLess. Add the close-curly brace in both functions and it should compile correctly.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top