Question

In: Computer Science

In Java Please: * posOfLargestElementLtOeT returns the position of the largest element in the array that...

In Java Please:

* posOfLargestElementLtOeT returns the position of the largest element in the array that is

* less than or equal to the limit parameter

* if all values are greater than theVal, return -1;

*

* Precondition: the array is nonempty and all elements are unique.

* Your solution must go through the array exactly once.

*

* <pre>

* 0 == posOfLargestElementLtOeT(3, new double[] { -7 }) // value:-7 is in pos 0

* 5 == posOfLargestElementLtOeT(3, new double[] { 11, -4, -7, 7, 8, 1 }), // value:1 is in pos 5

* -1 == posOfLargestElementLtOeT(-7, new double[] { 1, -4, -5, 7, 8, 11 }), // all elements are > -7

*

* 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 posOfLargestElementLtOeT( double limit, double[] list)

// Solve here

Solutions

Expert Solution

public class PosLargest {

    /**
     * posOfLargestElementLtOeT returns the position of the largest element in the array that is
     * <p>
     * less than or equal to the limit parameter
     * <p>
     * if all values are greater than theVal, return -1;
     * <p>
     * <p>
     * <p>
     * Precondition: the array is nonempty and all elements are unique.
     * <p>
     * Your solution must go through the array exactly once.
     *
     *
     *
     * <pre>
     *
     * 0 == posOfLargestElementLtOeT(3, new double[] { -7 }) // value:-7 is in pos 0
     *
     * 5 == posOfLargestElementLtOeT(3, new double[] { 11, -4, -7, 7, 8, 1 }), // value:1 is in pos 5
     *
     * -1 == posOfLargestElementLtOeT(-7, new double[] { 1, -4, -5, 7, 8, 11 }), // all elements are > -7
     *
     *
     *
     * 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 posOfLargestElementLtOeT(double limit, double[] list) {
        int maxIndex = -1;
        for (int i = 0; i < list.length; i++) {
            if (list[i] <= limit) {
                if (maxIndex == -1 || list[i] > list[maxIndex]) {
                    maxIndex = i;
                }
            }
        }
        return maxIndex;
    }

    public static void main(String[] args) {
        System.out.println(posOfLargestElementLtOeT(3, new double[]{-7}));
        System.out.println(posOfLargestElementLtOeT(3, new double[]{11, -4, -7, 7, 8, 1}));
        System.out.println(posOfLargestElementLtOeT(-7, new double[]{1, -4, -5, 7, 8, 11}));
    }
}


Related Solutions

Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
In Java Find the second largest and second smallest element in a given array. You can...
In Java Find the second largest and second smallest element in a given array. You can hardcode/declare the array in your program.
Write a program in MIPS to find the largest element of an array, the array size...
Write a program in MIPS to find the largest element of an array, the array size should be less than or equal to 10. Has to be extremely basic, cannot use stuff like move. Very basic. Here is what I already have and I am stuck. .data myarray: .word 0,0,0,0,0,0,0,0,0,0 invalid: .asciiz "Number is invalid, store a number in the array that is from 0-10.\n" large: .asciiz "The largest element is " colon: .asciiz " :\t" enter: .asciiz "Store a...
Question 6 Which of the following for loops will find the largest element in the array...
Question 6 Which of the following for loops will find the largest element in the array numbers, assuming numbers has already been assigned a collection of numeric values? Question 6 options: largest = None for i in range(len(numbers)): if largest is None and numbers[i] > largest: largest = numbers[i] largest = None for i in range(numbers): if largest is None and numbers[i] > largest: largest = numbers[i] largest = None for i in range(len(numbers)): if largest is None or numbers[i]...
Develop a recursive algorithm to find the smallest and largest element in an array and trace...
Develop a recursive algorithm to find the smallest and largest element in an array and trace the recursive function with appropriate message. using c++ add comment to the code
Write a program to remove an element from an array at the given position k and...
Write a program to remove an element from an array at the given position k and push the rest of the array elements one position back. Then insert the removed element at the beginning. Position k is entered through keyboard. For example, if the original array x is {'r', 'c', 'm', '7', 'w', '3', 'q'} and k = 3, the array will be changed to {'7', 'r', 'c', 'm', 'w', '3', 'q'}. Hint: Sequence of moving the element is important....
JAVA /**    * dataAtPosition returns the int data at the described position.    * if...
JAVA /**    * dataAtPosition returns the int data at the described position.    * if the position is an invalid position it throws an Exception.    *    * Examples:    * pos : 0 and LinkedList : 2 --> 3 --> null ==> return 2    * pos : 1 and LinkedList : 1 --> -3 --> null ==> return -3    * pos : 2 and LinkedList : -2 --> 3 --> -2 --> null ==> return...
Write a method called mode that returns the most frequently occurring element of an array of...
Write a method called mode that returns the most frequently occurring element of an array of integers. Assume that the array has at least one element and that every element in the array has a value between 0 and 100 inclusive. Break ties by choosing the lower value. For example, if the array passed contains the values [27, 15, 15, 11, 27], your method should return 15. write a version of this method that does not rely on the values...
Write the methods that insert and remove an element at the kth position in Java using...
Write the methods that insert and remove an element at the kth position in Java using recursion (NOT iteration) (Hint for the remove method: we have to recursive position -1 times, and each time we do the recursion, we have to create a method to move the head to the right) public void insertRecursive(int position, int data) { //enter code here } public int removeAtRecursive(int position) { //enter code here } Here is the given class for the Node: public...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT