Generics7.java - \/Purpose Practicing with Generics Recursion and Interfaces\/Author Misty Van Dyke Miguel Vazquez\/Course CS 1302\/Date\/Program

Generics7.java - /Purpose Practicing with Generics...

This preview shows page 1 - 2 out of 3 pages.

//******************************************************************************* //Purpose: Practicing with Generics, Recursion, and Interfaces // //Author: Misty Van Dyke, Miguel Vazquez // //Course: CS 1302 //Date: 3/20/2019 //Program: Generics7.java //******************************************************************************* import java.util.ArrayList; public class Generics7{ //No-argument constructor: public Generics7(){ } //max: Receives a generic one-dimensional array and returns the maximum value public <E extends Comparable<E>> E max(E[] list){ E max = list[0]; for (int i = 1; i < list.length; i++) { E element = list[i]; if (element.compareTo(max) > 0) { max = element; } } return max; } //largest: Receives a generic arrayList and returns the maximum value public <E extends Comparable<E>> E largest(ArrayList<E> list) { E max = list.get(0); for (int i = 1; i < list.size(); i++) { if (list.get(i).compareTo(max) > 0) { max = list.get(i); } } return max; } //binarySearch: Receives a generic one-dimensional array and a generic key // and returns the location of the key (positive) or negative if not found public <E extends Comparable<E>> int binarySearch(E[] list, E key) { int low = 0; int high = list.length - 1; return binarySearch (list, key, low, high); } public <E extends Comparable<E>> int binarySearch(E[] list, E key, int low, int high) { int mid; while(low <= high){
Image of page 1
Image of page 2

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture