Question

Solve it by JAVA please. DO NOT IMPORT PLEASE. THANKS Method 10: public static double nearestNeighbor(double...

Solve it by JAVA please.

DO NOT IMPORT PLEASE.

THANKS

Method 10:
public static double nearestNeighbor(double v, double[] values)

Return the value of the nearest neighbor to v. If v is some value, look through the list of values to find the one which is closest in value to v. For example, if v = 1.2 and the input array is {3.2, 1.7, 2.4, 0.9, 4.4}, then the method will return 0.9 because it is the value closest to 1.2 in the list. If there is a tie (for example, both 0.9 and 1.5 are the same distance to 1.2), then return the one which occurs first in the list. If the list is empty, then return v.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public static double nearestNeighbor(double v, double[] values) {
    double result = values[0], d1, d2;
    for (int i = 0; i < values.length; i++) {
        d1 = Math.abs(values[i] - v);
        d2 = Math.abs(result - v);
        if(d1 < d2) {
            result = values[i];
        }
    }
    return result;
}
Add a comment
Know the answer?
Add Answer to:
Solve it by JAVA please. DO NOT IMPORT PLEASE. THANKS Method 10: public static double nearestNeighbor(double...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • In Java for [computing] please. Thanks. Implement a method public static double[] difference(double[] vec1, double[] vec2)...

    In Java for [computing] please. Thanks. Implement a method public static double[] difference(double[] vec1, double[] vec2) to compute the (component-wise) difference of vectors vec1 and vec2 as a new vector. Assume that vec1 and vec2 are of the same length. The difference of vectors (a1, a2, . . . , an) and (b1, b2, . . . , bn) is (a1 − b1, a2 − b2, . . . , an − bn). For example, the difference of (−2, −4,...

  • Submit Chapter7.java with four (4) public static methods as follows: A. Write a method called mostCommon...

    Submit Chapter7.java with four (4) public static methods as follows: A. Write a method called mostCommon that accepts an array of integers as its only parameter, and returns the int that occurs most frequently. Break ties by returning the lower value For example, {1,2,2,3,4,4} would return 2 as the most common int. B. Write mostCommon (same as above) that accepts an array of doubles, and returns the double that occurs most frequently. Consider any double values that are within 0.1%...

  • Please Help! Need in Java C++ Object oriented programming. Thank you! Method 2: public static int...

    Please Help! Need in Java C++ Object oriented programming. Thank you! Method 2: public static int sumCapped(int[] nums, int x) This method will return the largest sum that is less than or equal x found in one pass of the array. This means that you must check each number in succession to determine whether or not you should add it in. For example, for the array {4, 2, 3, 5} with the value of the paramter x set to 7,...

  • public static double[] getVolumes(double[] base, double[] height, double[] length) Write a Java program called TriangularPrisms which...

    public static double[] getVolumes(double[] base, double[] height, double[] length) Write a Java program called TriangularPrisms which does the following: In the main method: Use a for loop which repeats three times. Within the loop, a. prompt the user to enter the base of the triangular prism and store the value in a double array called base b. prompt the user to enter the corresponding height and store the value in a double array called height c. prompt the user to...

  • Intro to JAVA see below: ----------------------------------------------- Rewrite the algorithm for computing the maximum using an enhanced...

    Intro to JAVA see below: ----------------------------------------------- Rewrite the algorithm for computing the maximum using an enhanced for loop. Complete the following code: CODE: import java.util.ArrayList; public class ArrayListUtil {    /**       Computes the maximum value of a nonempty array list.       @param values a non-empty array list       @return the largest value in the array list    */    public static double maximum(ArrayList<Double> values)    {       // Compute the largest value, using an enhanced for loop       ....

  • 1. Write a static method named mode that takes an array of integers as a parameter...

    1. Write a static method named mode that takes an array of integers as a parameter and that returns the value that occurs most frequently in the array. Assume that the integers in the array appear in sorted order. For example, if a variable called list stores the following values: ist -3, 1, 4, 4, 4, 6, 7,8, 8, 8, 8, 9, 11, 11, 11, 12, 14, int 141i Then the call of mode (li array, appearing four times. st,...

  • JAVA~ 1. Write a static method named countOdd(my_array) that returns the number of odd integers in...

    JAVA~ 1. Write a static method named countOdd(my_array) that returns the number of odd integers in a given array. If there are no odd numbers in the array, the method should return 0. If the array is empty, the method should also return 0. For example: Test Result System.out.println(countOdd(new int[]{2, 3, 5, 6})); 2 System.out.println(countOdd(new int[]{2})); 0 System.out.println(countOdd(new int[]{})); 0 System.out.println(countOdd(new int[]{-7, 2, 3, 8, 6, 6, 75, 38, 3, 2})); 4 public static int ----------------------------------------------------------------------------------------------------------- 2. Write a static...

  • *Java* Create a public static method named maxRes that takes an ArrayList of type Double and...

    *Java* Create a public static method named maxRes that takes an ArrayList of type Double and returns a double. This method returns the maximum result (do not return the original value) of taking the tangent of each value from the input

  • //Java (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList...

    //Java (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList list) Write a test program that does the following operations: 4. Creates an empty ArrayList of Integer elements; 5. Generates 20 integer values, drawn at random between 0 and 9 (included), and adds them to the array list; 6. Calls the method sort and prints both the original list, and the sorted one.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT