Question

[Java] Please test your code in the link I provide before you post your answer.

The output should be looked like exact same as the tester.

http://www.codecheck.it/files/17033122188mcxvjz8n8qbk0k9fyfrd3w95

Write a class LinkedListutil that contains 2 static methods public statie void shrink (LinkedListkstring> strings, int n) removes every nth node in the LinkedList of Strings. Start the removal with the nth not the oth node public static LinkedList<string> reverse (LinkedList<string> strings) return a LinkedList in which the the order of the elements is reversed.

Use the following file:

LinkedListUtilTester.java

import java.util.LinkedList;

public class LinkedListUtilTester
{

   public static void main(String[] args)
   {
      LinkedList<String> 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]");
      
      System.out.println(LinkedListUtil.reverse(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]");

      System.out.println(LinkedListUtil.reverse(list));
      System.out.println("Expected: [quince, catorce, trece, once, dies, nueve, siete, seis, cinco, tres, dos, uno]");
      

   }

}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

LinkedListUtil.java

import java.util.LinkedList;

public class LinkedListUtil
{
   //function that takes a linked list as parameter and an integer n
   //function removes every nth element in the list
   public static void shrink(LinkedList strings, int n)
   {
       //loop through the linked list
       for(int i=0;i<strings.size();i++)
       {
           //if i is not 0 and i % n-1 is 0
           if(i!=0 && i%(n-1)==0)
               strings.remove(i); //remove the ith element
       }  
   }
  

   //function that takes a linked list as parameter and returns its reverse
   public static LinkedList reverse(LinkedList strings)
   {
       //loop through the linked list until half of the list is traversed
       for (int i = 0; i < strings.size()/2; i++)
       {
           //swap the ith element from first and ith element from last
           String temp = (String) strings.get(i);
           strings.set(i, strings.get(strings.size()-1-i));
           strings.set(strings.size()-1-i,temp);
       }
       //return the reversed linked list
       return strings;
   }

}

LinkedListUtilTester.java

import java.util.LinkedList;

public class LinkedListUtilTester
{

public static void main(String[] args)
{
LinkedList list = new LinkedList<>();
//add elements into the list
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");
  
//call the shrink function to remove every 3rd element
LinkedListUtil.shrink(list, 3);
//print the new list
System.out.println(list);
System.out.println("Expected: [1, 2, 4, 5, 7, 8, 10, 11, 13, 14]");
  
//call the reverse function and print the reverse of the list
System.out.println(LinkedListUtil.reverse(list));
System.out.println("Expected: [14, 13, 11, 10, 8, 7, 5, 4, 2, 1]");
  
//clear the linked list
list.clear();
//add elements into the list
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");
  
//call the shrink function to remove every 4th element
LinkedListUtil.shrink(list, 4);
//print the new list
System.out.println(list);
System.out.println("Expected: [uno, dos, tres, cinco, seis, siete, nueve, dies, once, trece, catorce, quince]");
//call the reverse function and print the reverse of the list
System.out.println(LinkedListUtil.reverse(list));
System.out.println("Expected: [quince, catorce, trece, once, dies, nueve, siete, seis, cinco, tres, dos, uno]");
  

}

}

Output:

Console X sterminated> LinkedListUtilTester [Java Application] C:lProgram Files Java ljrel.8.0 731bin javaw.exe (May 2, 2017,

Add a comment
Know the answer?
Add Answer to:
[Java] Please test your code in the link I provide before you post your answer. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • [Java] Please test your code in the link I provide before you post your answer. The...

    [Java] Please test your code in the link I provide before you post your answer. The output should be looked like exact same as the tester. http://www.codecheck.it/files/17050415451csldwjahxt2kwn73ahd6vukt Thank you. Write a simplified application to illustrate the use of an undo button for a word processor. It keeps a history of all items and allows the user to undo the last. Write a class UndoStack. Implement using a Stack of Strings. The constructor will create an empty Stack to keep the...

  • Need Help Filling in Areas: /** Based on a Big Java problem You have to write...

    Need Help Filling in Areas: /** Based on a Big Java problem You have to write a static method that removes every other element from a linked list. Expected output: [Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday] [Monday, Wednesday, Friday] [Wednesday] [] */ public class DownSizeTester { public static void main(String[] args) { LinkedList list = new LinkedList() ; list.add("Sunday") ; list.add("Monday") ; list.add("Tuesday") ; list.add("Wednesday") ; list.add("Thursday") ; list.add("Friday") ; list.add("Saturday") ; System.out.println(list) ; downsize(list) ; System.out.println(list) ; downsize(list)...

  • NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT...

    NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT WOULD CAUSE THE HW1.java TO PRINT THE RIGHT DATA.!!! The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. None of your methods...

  • Activity 2. Complete the code inside the Java file below, ListUtil.java, such that this class supplies...

    Activity 2. Complete the code inside the Java file below, ListUtil.java, such that this class supplies a utility method to reverse the entries in a linked list. Then, test your code using the tester class, Reverse Tester.java, given below. ListUtil.java import java.util.LinkedList; /** This class supplies a utility method to reverse the entries in a linked list. */ public class ListUtil { /** Reverses the elements in a linked list @param strings the linked list to reverse public static void...

  • Consider java for fixing this code please: what i need is to insert method to be...

    Consider java for fixing this code please: what i need is to insert method to be added ( please don't change the test class and any giving value in the first class ) here is the correct out put: ------------------testAddLast()---- {A} {A->B} {A->B->null} {A->B->null->C} ----------------------------- --------testSubListOfSmallerValues()---------- {} {B->B->B->A} {F->B->B->B->A->D} {F->B->B->G->B->A->M->D} ----------------------------- ------------Test lastIndexOf()----- -1 3 -1 -1 0 5 2 ----------------------------- ---------testRetainAll()--------- {} {6:Tony->6:Tony} {null->bad->null} ----------------------------- ---------------Test removeStartingAtBack--- false true {apple->null->bad->null} true {apple->null->bad} {2:Morning->3:Abby->4:Tim->5:Tom->6:Tony} ----------------------------- ---------test insertionSort()--------- {} {D} {D->E->E->F->G}...

  • JAVA LANG PLEASE: I have follwed these below guidelines but when i run my queue test...

    JAVA LANG PLEASE: I have follwed these below guidelines but when i run my queue test it is not executing but my stack is working fine, can you fix it please! MyQueue.java Implement a queue using the MyStack.java implementation as your data structure.  In other words, your instance variable to hold the queue items will be a MyStack class. enqueue(String item): inserts item into the queue dequeue(): returns and deletes the first element in the queue isEmpty(): returns true or false...

  • please help!!!! JAVA I done the project expect one part but I still give you all...

    please help!!!! JAVA I done the project expect one part but I still give you all the detail that you needed... and I will post my code please help me fix the CreateGrid() part in main and make GUI works    List Type Data Structures Overview : You will be implementing my version of a linked list. This is a linked list which has possible sublists descending from each node. These sublists are used to group together all nodes which...

  • starter code To write a program using the starter code which is TestLinkedList to see if...

    starter code To write a program using the starter code which is TestLinkedList to see if the LinkedList program has bugs. It will produce ether a pass or fail.More information is in the first two pictures. LinkedList.java /** * @author someone * * Implements a double-linked list with four errors */ public class LinkedList<E> { // The first and last nodes in the list private Node<E> head, tail; // Number of items stored in the list private int size; //...

  • Currently working on a Java Assignment. I have written most codes for swap, reverse and insert....

    Currently working on a Java Assignment. I have written most codes for swap, reverse and insert. Just need a. itemCount receives a value and returns a count of the number of times this item is found in the list. c. sublist receives two indexes and returns an ArrayList of node values from the first index to the second index, provided the indexes are valid. d. select receives a variable number of indexes, and returns an ArrayList of node values corresponding...

  • Prelab Exercises Your task is to write a Java program that will print out the following...

    Prelab Exercises Your task is to write a Java program that will print out the following message (including the row of equal marks): Computer Science, Yes!!!! ========================= An outline of the program is below. Complete it as follows: a. In the documentation at the top, fill in the name of the file the program would be saved in and a brief description of what the program does. b. Add the code for the main method to do the printing. //...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT