Question

In: Computer Science

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.

Solutions

Expert Solution

All the explanation is provided in the comments of the code itself.

CODE--

public class CompareGeneric
{
   public static Comparable find_Largest(Comparable arr[])
   {
       //declare and initialize a variable to store the largest value
       Comparable largest=arr[0];
       //iterate over the array and find the largest
       for(int i=1;i<arr.length;i++)
       {
           if(arr[i].compareTo(largest)>0)
           {
               largest=arr[i];
           }
       }
       //finally return largest
       return largest;
   }
   public static void main(String[] args)
   {
       Double[] d= {2.0,5.7,22.4};
       System.out.println("Largest Double: "+find_Largest(d));
       Integer[] i= {1,2,3,4,5,6,7,8,9};
       System.out.println("Largest Integer: "+find_Largest(i));
   }
}

OUTPUT SCREENSHOT--

NOTE--

Please upvote if you like the effort.


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.
write a java code Write a generic program to compute the sum of 30 terms of...
write a java code Write a generic program to compute the sum of 30 terms of the following series. (181 - 5)/31 + (186 + 9)/34 - (191 - 13)/37 + (196 + 17)/40 + . . . . . 1 5 11 Note: the 3rd, 6th, 9th term etc, has a negative sign in front of parenthesis. User Input: None Expected output: Term Ratio Sum 1 5.677 5.677 2 5.735 11.413 3 -4.811 6.602
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...
Please write a java code. Write a generic program for New Home Construction Pricing with the...
Please write a java code. Write a generic program for New Home Construction Pricing with the following specifications. Note, each one of the 2, 3 or 4 bedroom homes are priced with standard type bathrooms. Update to deluxe or premium choice of bathrooms can be ordered by paying the difference in prices.    Types of homes Price 2 bedroom, 2 bathroom (standard type) and 1 car garage home = $350,000 3 bedroom, 2 bathroom (standard type) and 2 car garage...
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...
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.
Create a generic method to print objects in java. Include a tester class.
Create a generic method to print objects in java. Include a tester class.
Find the K'th smallest element in an unsorted array of integers. Find the K'th largest element...
Find the K'th smallest element in an unsorted array of integers. Find the K'th largest element in an unsorted array of integers. please make two separate methods in java
Write Java code to generate 100 random integers ranging from 0..9, inserting each element into an...
Write Java code to generate 100 random integers ranging from 0..9, inserting each element into an ArrayList. Then search for the first instance of the number 3, print the position, and then remove it from the list.
Write an algorithm in pseudo code to find one element and delete it in a doubly...
Write an algorithm in pseudo code to find one element and delete it in a doubly linked list. Your algorithm will print the original list, request the user to put in an element to be deleted, then print the final list after the deletion is done. If the element doesn’t exist in the list, print "XXX is not in the list" where "XXX" should be the one you received from the user. Use the following as your test cases to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT