Ask Question

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.

+2
Answers (1)
  1. 18 May, 09:41
    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?
Find 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 the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers