15.1k views
1 vote
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.

1 Answer

1 vote

Answer:

The Answer is given in the explanation section, See detailed explanations as comments within the code.

Step-by-step 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<Integer> myList = new ArrayList<Integer>(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());

}

}

User Denis Barushev
by
5.3k points