Question

In: Computer Science

JAVA Add static methods largest and smallest to the Measurable interface. The methods should return the...

JAVA

Add static methods largest and smallest to the Measurable interface. The methods should return the object with the largest or smallest measure from an array of Measurable objects.

Solutions

Expert Solution

Answer-

Your code is here -

public static Measurable largest(Measurable[] objects)
{
   if (objects.length == 0)
   {
   return null;
   }
   Measurable largestData = objects[0];
   for (int i = 1; i < objects.length; i++)
   {
   Measurable current = objects[i];
   if (current.getMeasure() > largestData.getMeasure())
   {
       largestData = current;
   }
   }
   return largestData;

   public static Measurable smallest(Measurable[] objects)
   {
       if (objects.length == 0)
       {
           return null;
    }
       Measurable smallestData = objects[0];
       for (int i = 1; i < objects.length; i++)
       {
           Measurable current = objects[i];
           if (current.getMeasure() < smallestData.getMeasure())
           {
               smallestData = current;
           }
       }
       return smallestData;
   }
   }
  

Note- Please do upvote, if any problem then comment in box sure I will help.


Related Solutions

Using the ListNode file. Write methods called min and max that return the smallest and largest...
Using the ListNode file. Write methods called min and max that return the smallest and largest values in the linked list. These methods will be added to your ListNode class. For example if a variable called list stores {11, -7, 3, 42, 0, 14], the call of list.min() should return -7 and the call of list.max() should return 42. If the list is empty, return -1. Print the returned value. Write a method called insertNode that inserts a new node...
Using the ListNode file. Write methods called min and max that return the smallest and largest...
Using the ListNode file. Write methods called min and max that return the smallest and largest values in the linked list. These methods will be added to your ListNode class. For example if a variable called list stores {11, -7, 3, 42, 0, 14], the call of list.min() should return -7 and the call of list.max() should return 42. If the list is empty, return -1. Print the returned value. Write a method called insertNode that inserts a new node...
Make the Measurable interface from Chapter 10 into a generic class. Provide a static method that...
Make the Measurable interface from Chapter 10 into a generic class. Provide a static method that returns the largest element of an ArrayList, provided that the elements are instances of Measurable. Be sure to return a value of type T. Measurable interface: public interface Measurable { double getMeasure(); } Using JAVA please
Using the Comparable Interface Write a class Compare3 that provides a static method largest. Method largest...
Using the Comparable Interface Write a class Compare3 that provides a static method largest. Method largest should take three Comparable parameters and return the largest of the three (so its return type will also be Comparable). Recall that method compareTo is part of the Comparable interface, so largest can use the compareTo method of its parameters to compare them. Write a class Comparisons whose main method tests your largest method above. First prompt the user for and read in three...
Write a Java program to get the difference between the largest and smallest values in a...
Write a Java program to get the difference between the largest and smallest values in a user inputed array of integers. The length of the array must be 1 and above
JAVA programming- answer prompts as apart of one java assignment Static static variables static methods constants...
JAVA programming- answer prompts as apart of one java assignment Static static variables static methods constants Create a class Die representing a die to roll randomly. ☑ Give Die a public final static int FACES, representing how many faces all dice will have for this run of the program. ☑ In a static block, choose a value for FACES that is a randomly chosen from the options 4, 6, 8, 10, 12, and 20. ☑ Give Die an instance variable...
A java program with classes and methods to manage employment. Program should be able to store/add...
A java program with classes and methods to manage employment. Program should be able to store/add employees, calculate payroll, display or schedule shifts. All classes should have at least a null constructor and copy constructor. Other constructors are up to your discretion. Include all necessary accessors and modifiers. To test the classes, create a container class with simply a main() method. The container class will have attributes that are class objects of each of the classes you create. The main()...
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.
IN JAVA: Repeat Exercise 28, but add the methods to the LinkedStack class. Add the following...
IN JAVA: Repeat Exercise 28, but add the methods to the LinkedStack class. Add the following methods to the LinkedStacked class, and create a test driver for each to show that they work correctly. In order to practice your array related coding skills, code each of these methods by accessing the internal variables of the LinkedStacked, not by calling the previously defined public methods of the class. - String toString()—creates and returns a string that correctly represents the current stack....
IN JAVA Step 1 Develop the following interface: Interface Name: ImprovedStackInterface Access Modifier: public Methods Name:...
IN JAVA Step 1 Develop the following interface: Interface Name: ImprovedStackInterface Access Modifier: public Methods Name: push Access modifier: public Parameters: item (data type T, parameterized type) Return type: void Throws: StackFullException Name: push Access modifier: public Parameters: item1 (data type T, parameterized type), item2 (data type T, parameterized type) Return type: void Throws: StackFullException Name: pop Access modifier: public Parameters: none Return type: void Throws: StackEmptyException Name: doublePop Access modifier: public Parameters: none Return type: void Throws: StackEmptyException Name:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT