Question
Implement a GENERIC Class named MagicBox Write a generic method to exchange the positions of two different elements in a list Write a generic method
Implement a GENERIC Class named MagicBox
Write a generic method to exchange the positions of two different elements in a list
Write a generic method that will count the occurrences of an item in the list
YOU MUST USE GENERICS!
Test your program with the following code
public class CountTest {
public static void main(String[] args) {
MagicBox occurances = new MagicBox();
String[] helloWorld = {"h", "e", "l", "l", "o", "w", "o", "r", "l", "d"};
int char_count = occurences.count(helloWorld, "l");
System.out.println("#occurrences of l: " + char_count);
Integer[] myints = {1, 9, 8, 8, 0, 0, 2, 0, 1, 7, 1, 0, 1, 0, 1, 0, 0};
int int_count = occurences.count(myints, 0);
System.out.println("#occurrences of zeros: " + int_count);
//before swap
System.out.println(helloWorld);
occurances.swap(helloWorld, 4,5);
//after swap
System.out.println(helloWorld);
//before int swap
System.out.println(myints);
occurances.swap(myints,7,8);
//after int swap
System.out.println(myints);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started