Ask Question
26 February, 10:16

Add the function min as an abstract function to the class arrayListType to return the smallest element of the list. Also, write the definition of the function min in the class unorderedArrayListType and write a program to test this function.

+3
Answers (1)
  1. 26 February, 12:31
    0
    The Answer is given in the explanation section, See detailed explanations as comments within the code.

    Explanation:

    import java. util. ArrayList;

    import java. util. Collections;

    import java. util. List;

    / / The abstract class with the abstract method

    public abstract class arrayListType {

    public abstract int smallestElement ();

    }

    //Class to implement the the abstract method

    class unorderedArrayListType{

    public int smallestElement () {

    //Create the list of Intitial capacity of 5

    List myList = new ArrayList (5);

    //Add Elements to the list

    myList. add (3);

    myList. add (4);

    myList. add (1);

    myList. add (6);

    myList. add (7);

    //FInd the smallest integer and return it

    int smallestInt = Collections. min (myList);

    return smallestInt;

    }

    }

    //Testing the class with a main method

    class Testing {

    public static void main (String[] args) {

    / / Creating an instance of the class unorderedArrayListType

    unorderedArrayListType list1 = new unorderedArrayListType ();

    //Calling the method to print the smallest Integer in the List

    System. out. println (list1. smallestElement ());

    }

    }
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Add the function min as an abstract function to the class arrayListType to return the smallest element of the list. Also, write the ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers