Question

In: Computer Science

JAVA /**    * posOfLargestElementLtOeT returns the position of the largest element in the    *...

JAVA

/**
   * 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 limit, 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) {
       return -2; // TODO 2: fix this code
   }

Solutions

Expert Solution

Program in java:

//class definition
public class LargestElement {
    //main method definition
    public static void main(String[] args) {
        //calling funtion to find the position of largest number
        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}));
    }
    //declaring the function to calculate largest number
    static int posOfLargestElementLtOeT(int val, double[] array){
        //declare varibale
        int pos=-1;
        double largest=0;
        boolean flag=true;
        //iterate through the array to find the largest number within limit
        for(int i=0;i<array.length;i++){
            //if the value of the element is less than the specified limit
            if(array[i]<val){
                //this condition will be executed only once to store the largest value
                if(flag==true){
                    //storing the first element less than the specified limit as the largest
                    largest=array[i];
                    //marking this position as the largest element position
                    pos=i;
                }
                //checking to find the largest number subsequent from the array less than specified limit
                if(largest<array[i]){
                    //storing the first element less than the specified limit as the largest
                    largest=array[i];
                    //marking this position as the largest element position
                    pos=i;
                }              
            }
        }
        //returning the position of the largest element
        return pos;
    }
}

Output:


Related Solutions

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,...
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.
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...
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)
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
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 out a generic java code to find a largest element from n comparable objects (default)...
Write out a generic java code to find a largest element from n comparable objects (default) and use a main method to test.
Using the trend in the periodic table, and position of an element in it, the smallest element is
Using the trend in the periodic table, and position of an element in it, the smallest element is a. hydrogen b. Helium c. Oxygen d. Carbon
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT