import java.util.Scanner; Okay so the question is like this: Exercise 2: Design
ID: 3592929 • Letter: I
Question
import java.util.Scanner;
Okay so the question is like this:
Exercise 2: Design a class called ListUtility.java that contains a list of static methods that manipulate unordered lists.
public class ListUtility{
public static Lis<T> findUnion(List<T> list1, List<T> list2){ //Create and return a third list that contains
//the items that are either in list1 or in list2 or both. //Do not add duplicates.
}
public static List<T> findIntersection(List<T> list1, List<T> list2){
//Create and return a third list that contains the items //that are common to both list1 and list2.
//Do not add duplicates.
}
public static List<T> interleave(List<T> list1, List<T> list2){
//Create and return a third list that contains the items //in list1 interleaved with the items in list2.
//For example, list1={A, C}, list2={B, P, M, N, Z} //list3 = {A, B, C, P, M, N, Z}
}
public static List<T> chopSkip(List<T> list1){
} }
//Create and return a list that has the items in list1 //with every second item removed.
//For example, if list1={A, B, C, D, E}
//the list returned is {A, C, E}
Write a test program that creates two unordered lists from input data supplied by the user (you may assume that each list contains just Strings such as “A”, “C”, etc.) Make sure you test all the methods for at least three cases.
>>>>My code
public class ListUtility{
public static <T> List<T> findUnion(List<T> list1, List<T> list2){
//Create and return a third list that contains
//the items that are in list1, list2 or both. //Do not add duplicates.
List <T> list3 = new List<T>();
T curr = list1.first();
T curr2 = list2.first();
while(curr != null)
{
if((!list3.contains(curr)))
{
list3.add(curr);
}
}
while(curr2 != null)
{
if((!list3.contains(curr2)))
{
list3.add(curr2);
}
}
return list3;
}
public static <T> List<T> findIntersection(List<T> list1, List<T> list2)
{
//Create and return a third list that contains the items //that are common to both list1 and list2.
//Do not add duplicates.
List <T> list3 = new List<T>();
T curr = list1.first();
while(curr != null)
{
if(list2.contains(curr) && (!list3.contains(curr)))
{
list3.add(curr);
}
curr = list1.next();
}
return list3;
}
public static <T> List<T> interleave(List<T> list1, List<T> list2){
//Create and return a third list that contains the items //in list1 interleaved with the items in list2.
//For example, list1={A, C}, list2={B, P, M, N, Z} //list3 = {A, B, C, P, M, N, Z}
List <T> list3 = new List<T>();
T curr = list1.first();
T curr2 = list2.first();
while (curr != null && curr2 != null)
{
if(curr != null)
{
list3.add(curr);
curr = list1.next();
}
if(curr2 != null)
{
list3.add(curr2);
curr2 = list2.next();
}
}
return list3;
}
public static <T> List<T> chopSkip(List<T> list1){
{
//Create and return a list that has the items in list1 //with every second item removed.
//For example, if list1={A, B, C, D, E}
//the list returned is {A, C, E}
List <T> list2 = new List();
T curr = list1.first();
int counter = 0;
while (curr != null)
{
if(counter % 2 != 0)
{
list2.add(curr);
}
counter ++;
curr = list1.next();
}
return list1;
}
}
public static void main (String [] args)
{
List<String> strings = new List<String>();
List<String> strings2 = new List<String>();
Scanner kb = new Scanner (System.in);
boolean stop = true;
String temp;
while (stop == true)
{
System.out.println("Enter chars and type stop to move to the next list");
temp = kb.nextLine();
if(temp.equals("stop"))
{
stop = false;
break;
}
strings.add(temp);
}
stop = true;
while (stop == true)
{
System.out.println("Enter chars and type stop to finish");
temp = kb.nextLine();
if(temp.equals("stop"))
{
stop = false;
break;
}
strings2.add(temp);
}
System.out.print("List 1: "); strings.enumerate(); System.out.println(" ");
System.out.print("List 2: "); strings2.enumerate(); System.out.println(" ");
System.out.print("Union: "); findUnion(strings, strings2).enumerate();
}
---- So everything works except when i try to call the findUnion method? This is really confusing to me because with other questions we would make another list class that uses the actual list.java, however this is sort of implementing it I'mnot sure how type <string> can be implemented in type <T> methods?
Explanation / Answer
The solution for your problem is to use an ArrayList instead of List. List is just an interface. ArrayList is a class that implements List and several other interfaces. So in my answer, I’ve used ArrayList. An Iterator is used to iterate through the elements of an ArrayList. So instead of using first() and next() functions of the List, you can use hasNext() function of the iterator to check if there’s a next element in the list which is not traversed/iterated. And if the hasNext() returns true, you can use the next() to fetch the next element. I’ve also used the foreach looping statements in this answer to quickly go through an ArrayList. You can find more information on comments included in the program. A screenshot of the ouput is also included.
//ListUtility.java file
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class ListUtility {
public static <T> ArrayList<T> findUnion(ArrayList<T> list1,
ArrayList<T> list2) {
/* Create and return a third list that contains
the items that are in list1, list2 or both. Do not add duplicates.*/
ArrayList<T> list3 = new ArrayList<T>();
Iterator<T> list1Iterator=list1.iterator(); /*Iterator can be used to iterate through the list items */
Iterator<T> list2Iterator=list2.iterator();
while(list1Iterator.hasNext()){ /* hasNext() function of iterator will return true if there's any next elements left in the list */
T curr=list1Iterator.next(); /*the next() function will fetch the next item in the list,if any*/
if ((!list3.contains(curr))) {
list3.add(curr); /*adding list1 elements to list3*/
}
}
while(list2Iterator.hasNext()){
T curr=list2Iterator.next();
if ((!list3.contains(curr))) {
list3.add(curr); /* adding list2 elements in list3*/
}
}
return list3;
}
public static <T> ArrayList<T> findIntersection(ArrayList<T> list1,
ArrayList<T> list2) {
/* Create and return a third list that contains the items that are
common to both list1 and list2.
Do not add duplicates.*/
ArrayList<T> list3 = new ArrayList<T>();
for (T curr : list1) { /*
* using a foreach loop to iterate through the
* list items, it's simple and quick
*/
if (list2.contains(curr) && (!list3.contains(curr))) { /*if the item is present in list1 and list2;and not yet
added to list3; add it to list3*/
list3.add(curr);
}
}
return list3;
}
public static <T> ArrayList<T> interleave(ArrayList<T> list1,
ArrayList<T> list2) {
/* Create and return a third list that contains the items in list1 interleaved with the items in list2.
* For example, list1={A, C}, list2={B, P, M, N, Z} so list3 = {A, B, C, P, M, N, Z}*/
ArrayList<T> list3 = new ArrayList<T>();
Iterator<T> list1iterator = list1.iterator();
Iterator<T> list2iterator = list2.iterator();
T curr=null;
T curr2=null;
if(list1iterator.hasNext()){
curr=list1iterator.next();
}
if(list2iterator.hasNext()){
curr2=list2iterator.next();
}
while (curr != null || curr2 != null) { /* loops until both list elements are fully iterated */
if (curr != null) {
list3.add(curr);
if(list1iterator.hasNext()){
curr = list1iterator.next();
}else{
curr=null; /* if all the items are iterated, a null value is given , so that it'll not consider for the next loop*/
}
}
if (curr2 != null) {
list3.add(curr2);
if(list2iterator.hasNext()){
curr2 = list2iterator.next();
}else{
curr2=null;
}
}
}
return list3;
}
public static <T> ArrayList<T> chopSkip(ArrayList<T> list1) {
{
/* Create and return a list that has the items in list1 with every second item removed.
* For example, if list1={A, B, C, D, E}
* the list returned is {A, C, E} */
ArrayList<T> list3 = new ArrayList<T>();
Iterator<T> listiterator = list1.iterator();
T curr = null;
if(listiterator.hasNext()){
curr = listiterator.next();
}
int counter = 0;
while (curr != null) {
if (counter % 2 == 0) {
list3.add(curr);
}
counter++;
if(listiterator.hasNext()){
curr = listiterator.next(); /* fetching the next item from the list */
}else{
break;
}
}
return list3;
}
}
public static void main(String[] args)
{
ArrayList<String> strings = new ArrayList<String>();
ArrayList<String> strings2 = new ArrayList<String>();
Scanner kb = new Scanner(System.in);
boolean stop = true;
String temp;
System.out.println("Enter chars and type stop to move to the next list");
while (stop == true) {
temp = kb.nextLine();
if (temp.equals("stop")) {
stop = false;
break;
}
strings.add(temp);
}
stop = true;
System.out.println("Enter chars and type stop to finish");
while (stop == true) {
temp = kb.nextLine();
if (temp.equals("stop")) {
stop = false;
break;
}
strings2.add(temp);
}
kb.close();
System.out.print("List 1:");
for (String tmp : strings) {
System.out.print(tmp+" ");
}
System.out.println(" ");
System.out.print("List 2:");
for (String tmp : strings2) {
System.out.print(tmp+" ");
}
System.out.println(" ");
System.out.print("Union:");
for (String tmp : findUnion(strings, strings2)) {
System.out.print(tmp+" ");
}
System.out.println(" ");
System.out.print("Intersection:");
for (String tmp : findIntersection(strings, strings2)) {
System.out.print(tmp+" ");
}
System.out.println(" ");
System.out.print("Interleave:");
for (String tmp : interleave(strings, strings2)) {
System.out.print(tmp+" ");
}
System.out.println(" ");
System.out.print("List1 chopskip:");
for (String tmp : chopSkip(strings)){
System.out.print(tmp+" ");
}
System.out.println(" ");
System.out.print("List2 chopskip:");
for (String tmp : chopSkip(strings2)){
System.out.print(tmp+" ");
}
System.out.println(" ");
}
}
// Output
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.