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.
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]...
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 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...
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 a method in JAVA, called binarySearch, that returns the index of the key element if...
Write a method in JAVA, called binarySearch, that returns the index of the key element if found in the array, otherwise it will return the value of minus(insertion point +1). In main, use an initializer list create an array of ints called nums holding the following values: 1, 4, 13, 43, -25, 17, 22, -37, 29. Test your binarySearch method on nums with a key value number in it (e.g. 4) and one that is not (e.g. 100). Ex. Given...
Code with Java and no imports, Method sandwiched returns true if num is in the element...
Code with Java and no imports, Method sandwiched returns true if num is in the element before and after an element that is not equal to num sandwiched([4,5,4,6,7,3], 4) returns true sandwiched([2,1,2], 2) returns true sandwiched([3,3,3], 3) returns false sandwiched([4,5,6,4], 4) returns false sandwiched([1,1,2,3,1,4,1], 1) returns true @param nums Integer ArrayList @param num integer @return true if a single number is between elements equal to num */ public static boolean sandwiched(ArrayList nums, int num) { return false; }//end sandwiched
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT