[Java] First of all, you need to check your code at the link I provided. If your
ID: 3830764 • Letter: #
Question
[Java] First of all, you need to check your code at the link I provided. If your code's not pass the test in the line, I have to give you a down-thumb.
Here's the link : http://www.codecheck.it/files/17033122188mcxvjz8n8qbk0k9fyfrd3w95
Write a class LinkedListUtil that contains 2 static methods
public static void shrink(LinkedList<String> strings, int n)removes every nth node in the LinkedList of Strings. Start the removal with the nth not the 0th node
public static LinkedList<String> reverse(LinkedList<String> strings) return a LinkedList in which the the order of the elements is reversed.
Provide Javadoc.
Use the following file:
LinkedListUtilTester.java
Explanation / Answer
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.