this is what I have so far. Need help with the methods import java.util.ArrayLis
ID: 3855283 • Letter: T
Question
this is what I have so far. Need help with the methods
import java.util.ArrayList;
public class MyGenerics6{
//Declarations
//****************************************************************************
//No-argument constructor:
//****************************************************************************
public MyGenerics6 (){
}//end of constructor
//****************************************************************************
//max: Receives a generic one-dimensional array and returns the maximum
// value in the array.
//****************************************************************************
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;
}//end of max
//****************************************************************************
//max: Receives a generic two-dimensional array and returns the maximum
// value in the array.
//****************************************************************************
public <E extends Comparable<E>> E max(E[][] list) {
}
//****************************************************************************
//largest: Receives a generic arrayList and returns the maximum
// value in the array.
//****************************************************************************
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
// a negative location 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) {
return -99; // update
}
//****************************************************************************
//sort: Receives a generic arrayList and returns nothing.
//****************************************************************************
public <E extends Comparable<E>> void sort(ArrayList<E> list) {
}
//****************************************************************************
//sort: Receives a generic one-dimensional array and returns nothing.
//****************************************************************************
public <E extends Comparable<E>> void sort(E[] list) {
}
//****************************************************************************
//displayOneDList: Receives a generic one-dimensional array and displays its contents
//****************************************************************************
public <E> void displayOneDList(E[] list, String listName){
}
//****************************************************************************
//displayTwoDList: Receives a generic two-dimensional array & a string name
// and displays its contents
//****************************************************************************
public <E> void displayTwoDList(E[][] list, String listName){
}
//****************************************************************************
//displayArrayList: Receives a generic arraylist & a string name
// and displays its contents
//****************************************************************************
public <E> void displayArrayList(ArrayList <E> list, String listName){
}
}//end of class
Explanation / Answer
Given below is the completed code. Please rate the answer if it helped. Thank you.
import java.util.ArrayList;
public class MyGenerics6{
//Declarations
//****************************************************************************
//No-argument constructor:
//****************************************************************************
public MyGenerics6 (){
}//end of constructor
//****************************************************************************
//max: Receives a generic one-dimensional array and returns the maximum
// value in the array.
//****************************************************************************
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;
}//end of max
//****************************************************************************
//max: Receives a generic two-dimensional array and returns the maximum
// value in the array.
//****************************************************************************
public <E extends Comparable<E>> E max(E[][] list) {
E max = list[0][0];
for (int i = 0; i < list.length; i++) {
for(int j = 0; j < list[0].length; j++)
{
E element = list[i][j];
if (element.compareTo(max) > 0) {
max = element;
}
}
}
return max;
}
//****************************************************************************
//largest: Receives a generic arrayList and returns the maximum
// value in the array.
//****************************************************************************
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
// a negative location 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)
{
mid = (low + high) /2;
if(list[mid].compareTo(key) == 0) // found, return mid
return mid;
else if(list[mid].compareTo(key) < 0)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1; //when not found
}
//****************************************************************************
//sort: Receives a generic arrayList and returns nothing.
//****************************************************************************
public <E extends Comparable<E>> void sort(ArrayList<E> list) {
//selection sort
int minIdx ;
for(int i = 0 ; i < list.size(); i++)
{
minIdx = i;
for(int j = i + 1; j < list.size(); j++)
{
if(list.get(j).compareTo(list.get(minIdx)) < 0)
minIdx = j;
}
if(minIdx != i)
{
E temp = list.get(i);
list.set(i, list.get(minIdx));
list.set(minIdx, temp);
}
}
}
//****************************************************************************
//sort: Receives a generic one-dimensional array and returns nothing.
//****************************************************************************
public <E extends Comparable<E>> void sort(E[] list) {
//selection sort
int minIdx ;
for(int i = 0 ; i < list.length; i++)
{
minIdx = i;
for(int j = i + 1; j < list.length; j++)
{
if(list[j].compareTo(list[minIdx]) < 0)
minIdx = j;
}
if(minIdx != i)
{
E temp = list[i];
list[i] = list[minIdx];
list[minIdx] = temp;
}
}
}
//****************************************************************************
//displayOneDList: Receives a generic one-dimensional array and displays its contents
//****************************************************************************
public <E> void displayOneDList(E[] list, String listName){
System.out.println("The contents of the one dimensional list " + listName + " is ");
for(int i = 0; i < list.length; i++)
System.out.println(list[i].toString());
}
//****************************************************************************
//displayTwoDList: Receives a generic two-dimensional array & a string name
// and displays its contents
//****************************************************************************
public <E> void displayTwoDList(E[][] list, String listName){
System.out.println("The contents of the 2D list " + listName + " is ");
for(int i = 0; i < list.length; i++)
{
System.out.println(" ");
for(int j = 0; i < list[0].length; j++)
{
System.out.print(list[i][j].toString() + " ");
}
}
}
//****************************************************************************
//displayArrayList: Receives a generic arraylist & a string name
// and displays its contents
//****************************************************************************
public <E> void displayArrayList(ArrayList <E> list, String listName){
System.out.println("The contents of the ArrayList " + listName + " is ");
for(int i = 0; i < list.size(); i++)
System.out.println(list.get(i).toString());
}
}//end of class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.