A java question. Write a class LinkedListUtil that contains 2 static methods 1.
ID: 3830571 • Letter: A
Question
A java question. Write a class LinkedListUtil that contains 2 static methods
1. public static void shrink(LinkedList strings, int n) removes every nth node in the LinkedList of Strings. Start the removal with the nth not the 0th node
2. public static void reverse(LinkedList strings) reverses the order of the elements in the LinkedList
Please show the full code, Thank you.
------------------------------------------------------------------------------------------------------------------
The LinkedListUtilTester is given as follow:
LinkedListUtilTester.java
Explanation / Answer
LinkedListUtilTester.java
import java.util.LinkedList;
public class LinkedListUtilTester
{
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
list.add("5");
list.add("6");
list.add("7");
list.add("8");
list.add("9");
list.add("10");
list.add("11");
list.add("12");
list.add("13");
list.add("14");
list.add("15");
LinkedListUtil.shrink(list, 3);
System.out.println(list);
System.out.println("Expected: [1, 2, 4, 5, 7, 8, 10, 11, 13, 14]");
LinkedListUtil.reverse(list);
System.out.println(list);
System.out.println("Expected: [14, 13, 11, 10, 8, 7, 5, 4, 2, 1]");
list.clear();
list.add("uno");
list.add("dos");
list.add("tres");
list.add("cuatro");
list.add("cinco");
list.add("seis");
list.add("siete");
list.add("ocho");
list.add("nueve");
list.add("dies");
list.add("once");
list.add("doce");
list.add("trece");
list.add("catorce");
list.add("quince");
LinkedListUtil.shrink(list, 4);
System.out.println(list);
System.out.println("Expected: [uno, dos, tres, cinco, seis, siete, nueve, dies, once, trece, catorce, quince]");
LinkedListUtil.reverse(list);
System.out.println(list);
System.out.println("Expected: [quince, catorce, trece, once, dies, nueve, siete, seis, cinco, tres, dos, uno]");
}
}
LinkedListUtil.java
import java.util.LinkedList;
public class LinkedListUtil {
public static void shrink(LinkedList strings, int n){
for(int i=n-1;i<strings.size();i+=n){
strings.remove(i);
i--;
}
}
public static void reverse(LinkedList strings) {
for(int i=0;i<strings.size()/2; i++){
String temp =(String) strings.get(i);
strings.set(i, strings.get(strings.size()-1-i));
strings.set(strings.size()-1-i,temp);
}
}
}
Output:
[1, 2, 4, 5, 7, 8, 10, 11, 13, 14]
Expected: [1, 2, 4, 5, 7, 8, 10, 11, 13, 14]
[14, 13, 11, 10, 8, 7, 5, 4, 2, 1]
Expected: [14, 13, 11, 10, 8, 7, 5, 4, 2, 1]
[uno, dos, tres, cinco, seis, siete, nueve, dies, once, trece, catorce, quince]
Expected: [uno, dos, tres, cinco, seis, siete, nueve, dies, once, trece, catorce, quince]
[quince, catorce, trece, once, dies, nueve, siete, seis, cinco, tres, dos, uno]
Expected: [quince, catorce, trece, once, dies, nueve, siete, seis, cinco, tres, dos, uno]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.