All code should be in Java. Don\'t worry about including class information as I
ID: 3887497 • Letter: A
Question
All code should be in Java. Don't worry about including class information as I am more focused on how I can complete the meat of the problem. The method declaration CANNOT be changed it must stay: public static int posOfElementClosestTo(double theVal, double[] list).
Thank you!
/**
* posOfElementClosestTo returns the position of the element in the array that is
* closest to the theVal parameter, in the absolute value sense.
* In the event of a tie, return the position of the first value found
* (starting from 0)
*
* You can assume the array is nonempty and all values are unique. Your solution
* must go through the array exactly once and use Math.abs. Here are some examples (using "==" informally):
*
* <pre>
* 0 == posOfElementClosestTo(3, new double[] { -7 }) // -7 is closest to 3, it's in pos 0
* 5 == posOfElementClosestTo(3, new double[] { 11, -4, -7, 7, 8, 1 }), // 1 is closest to 3, it's in pos 5
* 2 == posOfElementClosestTo(-6, new double[] { 1, -4, -7, 7, 8, 11 }), // -7 is closest to -6, it's in pos 2
*
* The code below is a stub version, you should replace the line of code
* labeled TODO with code that achieves the above specification
* </pre>
*/
public static int posOfElementClosestTo(double theVal, double[] list) {
//TODO 3: fix this
}
Explanation / Answer
Below is your function : -
/**
* posOfElementClosestTo returns the position of the element in the array
* that is closest to the theVal parameter, in the absolute value sense. In
* the event of a tie, return the position of the first value found
* (starting from 0)
*
* You can assume the array is nonempty and all values are unique. Your
* solution must go through the array exactly once and use Math.abs. Here
* are some examples (using "==" informally):
*
* <pre>
* 0 == posOfElementClosestTo(3, new double[] { -7 }) // -7 is closest to 3, it's in pos 0
* 5 == posOfElementClosestTo(3, new double[] { 11, -4, -7, 7, 8, 1 }), // 1 is closest to 3, it's in pos 5
* 2 == posOfElementClosestTo(-6, new double[] { 1, -4, -7, 7, 8, 11 }), // -7 is closest to -6, it's in pos 2
*
* The code below is a stub version, you should replace the line of code
* labeled TODO with code that achieves the above specification
* </pre>
*/
public static int posOfElementClosestTo(double theVal, double[] list) {
double diff = 0, minDif = Integer.MAX_VALUE;
int index = 0;
for (int i = 0; i < list.length; i++) {
diff = Math.abs(list[i] - theVal);
if (diff < minDif) {
minDif = diff;
index = i;
}
}
return index;
}
For testing purpose I have created below class and main function . Itis working as per requirements
public class Element {
/**
* posOfElementClosestTo returns the position of the element in the array
* that is closest to the theVal parameter, in the absolute value sense. In
* the event of a tie, return the position of the first value found
* (starting from 0)
*
* You can assume the array is nonempty and all values are unique. Your
* solution must go through the array exactly once and use Math.abs. Here
* are some examples (using "==" informally):
*
* <pre>
* 0 == posOfElementClosestTo(3, new double[] { -7 }) // -7 is closest to 3, it's in pos 0
* 5 == posOfElementClosestTo(3, new double[] { 11, -4, -7, 7, 8, 1 }), // 1 is closest to 3, it's in pos 5
* 2 == posOfElementClosestTo(-6, new double[] { 1, -4, -7, 7, 8, 11 }), // -7 is closest to -6, it's in pos 2
*
* The code below is a stub version, you should replace the line of code
* labeled TODO with code that achieves the above specification
* </pre>
*/
public static int posOfElementClosestTo(double theVal, double[] list) {
double diff = 0, minDif = Integer.MAX_VALUE;
int index = 0;
for (int i = 0; i < list.length; i++) {
diff = Math.abs(list[i] - theVal);
if (diff < minDif) {
minDif = diff;
index = i;
}
}
return index;
}
public static void main(String[] args) {
System.out.println("Case 1 : " + (0 == posOfElementClosestTo(3, new double[] { -7 })));
System.out.println("Case 1 : " + (5 == posOfElementClosestTo(3, new double[] { 11, -4, -7, 7, 8, 1 })));
System.out.println("Case 1 : " + (2 == posOfElementClosestTo(-6, new double[] { 1, -4, -7, 7, 8, 11 })));
}
}
Output
Case 1 : true
Case 1 : true
Case 1 : true
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.