public class LinkedPlayListTest { static Song s1; static Song s2;…
Questionpublic class LinkedPlayListTest { static Song s1; static Song s2;…Image transcription textPart 2: Implement the insertAfter method Implement the public boolean insertAfter (Song prevSong, SongsongToAdd. To do this look for the node in the list that contains the prevSong. new node that contains songToAdd and insert it immediately after the prevSong node. For example if I want to insert the song … Show more public class LinkedPlayListTest {static Song s1;static Song s2;static Song s3;static Song s4;static Song s5;LinkedPlayList empty = new LinkedPlayList();LinkedPlayList one = new LinkedPlayList();LinkedPlayList songs = new LinkedPlayList();@BeforeClasspublic static void setUpBeforeClass() throws Exception { s1 = new Song(“Bad Habits”,”Ed Sheeran”); s2 = new Song(“Tides”, “Ed Sheeran”); s3 = new Song(“Overpass Graffiti”,”Ed Sheeran”); s4 = new Song(“Heat Waves”,”Glass Animals”); s5 = new Song(“Ophelia”,”The Lumineers”);}@Beforepublic void setUp() throws Exception { one.insertAtHead(s1); songs.insertAtHead(s4); songs.insertAtHead(s3); songs.insertAtHead(s2); songs.insertAtHead(s1); }@Afterpublic void tearDown() throws Exception {}@Testpublic void containsTrue() { assertTrue(songs.contains(s1)); assertTrue(songs.contains(s2)); assertTrue(songs.contains(s3)); assertTrue(songs.contains(s4));}@Testpublic void containsFalse() { assertFalse(songs.contains(s5));}@Testpublic void append() { LinkedPlayList added = new LinkedPlayList(); added.insertAtHead(s5); added.insertAtHead(s4); added.insertAtHead(s3); added.insertAtHead(s2); added.insertAtHead(s1); songs.append(s5); assertTrue(songs.equals(added));}@Testpublic void appendEmptyList() { LinkedPlayList added = new LinkedPlayList(); added.insertAtHead(s5); empty.append(s5); assertTrue(empty.equals(added));}@Testpublic void getHeadTest() { assertTrue(s1.equals(songs.get(0).getSong()));}@Test public void getRandomTest() { assertTrue(s2.equals(songs.get(1).getSong()));}@Test public void getLastTest() {..;@Test(expected=IndexOutOfBoundsException.class) public void getNegativeIndex() { &..;@Test(expected=IndexOutOfBoundsException.class) public void getLargeIndex() { songs.get(4);}@Test public void addHeadTest() { LinkedPlayList added = new LinkedPlayList(); added.insertAtHead(s4); added.insertAtHead(s3); added.insertAtHead(s2); added.insertAtHead(s1); added.insertAtHead(s5); songs.add(0, s5); assertTrue(songs.equals(added));}@Test public void addInMiddle() { LinkedPlayList added = new LinkedPlayList(); added.insertAtHead(s4); added.insertAtHead(s3); added.insertAtHead(s5); added.insertAtHead(s2); added.insertAtHead(s1); songs.add(2, s5); assertTrue(songs.equals(added));}@Test public void removeHead() { LinkedPlayList removed = new LinkedPlayList(); removed.insertAtHead(s4); removed.insertAtHead(s3); removed.insertAtHead(s2); boolean r = songs.removeOne(s1); Logger.getGlobal().info(“head=” + songs.head().toString()); String contents = “”; Node current = songs.head(); while(current!=null) { contents += “next=” + current.getNext() + “n”; current = current.getNext(); } Logger.getGlobal().info(contents); assertTrue(songs.equals(removed)); assertTrue(r); }@Test public void removeMiddle() { LinkedPlayList removed = new LinkedPlayList(); removed.insertAtHead(s4); removed.insertAtHead(s2); removed.insertAtHead(s1); boolean r = songs.removeOne(s3); assertTrue(songs.equals(removed)); assertTrue(r);}@Test public void removeAllMiddle() { LinkedPlayList removed = new LinkedPlayList(); removed.insertAtHead(s4); removed.insertAtHead(s3); removed.insertAtHead(s2); removed.insertAtHead(s1); songs.append(s4); songs.append(s4); songs.append(s4); songs.append(s5); boolean r = songs.removeAll(s2); assertTrue(removed.equals(removed)); assertTrue(r);}@Test public void reverseEmpty() { LinkedPlayList reversed = new LinkedPlayList(); empty.reverse(); assertTrue(empty.equals(reversed));}@Test public void reverseOne() { LinkedPlayList reversed = new LinkedPlayList(); reversed.insertAtHead(s1); one.reverse(); assertTrue(one.equals(reversed));}@Test public void reverseMany() { LinkedPlayList reversed = new LinkedPlayList(); reversed.insertAtHead(s1); reversed.insertAtHead(s2); reversed.insertAtHead(s3); reversed.insertAtHead(s4); songs.reverse(); assertTrue(songs.equals(reversed));}@Test public void swapApart() { LinkedPlayList swapped = new LinkedPlayList(); swapped.append(s1); swapped.append(s4); swapped.append(s3); swapped.append(s2); swapped.append(s5); songs.append(s5); songs.swap(s2,s4); assertTrue(songs.equals(swapped));} } package playlist;import java.util.logging.*;public class LinkedPlayList { static class Node { private Song song; private Node next; public Node(Song song,Node next) { this.song = song; this.next = next; } public Song getSong() { return song; } public Node getNext() { return next; } public String toString() { return song.toString(); } } private Node head; public Node head() { return head; } public boolean isEmpty() { return head==null; } public int size() { int size=0; Node current = head; while(current!=null) { size++; current = current.next; } return size; } public void insertAtHead(Song song) { Node node = new Node(song,null);//new head points to old head node.next = head; head = node; } public boolean equals(Object o) { LinkedPlayList pl = (LinkedPlayList)o; if(pl.size() != size()) {return false;} Node current1 = pl.head; Node current2 = head; while(current1!=null) { if(!current1.song.equals(current2.song)) return false; current1 = current1.next; current2 = current2.next; } return true; } public int hashCode() { int hc = 0; Node current = head; while(current!=null) { hc = hc + (current.getSong()).hashCode(); } return hc; } //****IMPLEMENT THIS*******// //Returns true if the LinkedPlayList contains a Node with Song s //Returns false otherwise public boolean contains(Song s) { Node current = head; while(current !=null) { if(s.equals(current.song)) { return true; } current = current.next; } return false; } //****IMPLEMENT THIS*******// //Note that there is no tail instance variable //You should add a new Node containing Song, song //to the end of the PlayList public void append(Song song) { Node node = new Node(song,null); //will be the end if(head==null) { head=node; } else { Node current = head; while(current.next!=null) { current = current.next; } current.next = node; } } //****IMPLEMENT THIS*******// //Returns that Node at the specified index in the List. //Remember the first element in the list has an index of 0 //Throws an indexOutOfBoundsException if the index is less than 0 // or greater than or equal to the size of the list public Node get(int index) { if(index < 0) { throw new IndexOutOfBoundsException(index); } else { int currentIndex = 0; Node current = head; while(current!=null) { if(currentIndex == index) { return current; } currentIndex++; current = current.next; } throw new IndexOutOfBoundsException(index); } } //****IMPLEMENT THIS*******// //Adds a new Node containing the Song song to the LinkedPlayList //at the current location. If I want to add a Song at the index 1, //the node at index 0 should be updated to point at the new Node and //the remaining nodes should shift down one. This is very similar to //insertAt method we completed in class //If the index is invalid, throw an IndexOutOfBoundsException //Think about how to use the get method to implement the add method public void add(int loc,Song song) { if(loc<0) { throw new IndexOutOfBoundsException(loc); } if(loc==0) { Node n = new Node(song,null); n.next = head; head = n; } else { Node prevSong = get(loc-1); Node nextSong = get(loc); Node n = new Node(song,null); prevSong.next = n; n.next = nextSong; } } //****IMPLEMENT THIS*******// //remove the first node that contains the specified song and return true //if the list is empty throw an illegal argument exception //if the list doesn't contain the song, return false public boolean removeOne(Song song) { boolean removedOne = false; if(head==null) throw new IllegalArgumentException("can't remove from an empty list"); if(song.equals(head.song)) { head = head.next; removedOne = true; } else { Node current = head; while(current.next!=null && (!song.equals(current.next.song))) current = current.next; if(current.next!=null) { current.next = current.next.next; removedOne = true; } else removedOne = false; } return removedOne; } //****IMPLEMENT THIS*******// //remove the all the nodes that contain the specified song and return true; //if the list is empty throw an illegal argument exception //if the list doesn't contain the song, //then print a useful message to the console public boolean removeAll(Song song) { if(head ==null) { throw new IllegalArgumentException("empty list"); } Node temp = head; boolean flag =false; while(temp!=null) { if(temp.song.equals(song)) { flag = true; temp = temp.next; removeOne(song); }else { temp = temp.next; } } if (!flag) { System.out.println("playlist with no song"); } else { System.out.println("Song Removed"); } return false; } //****IMPLEMENT THIS*******// //Look for the node in the list that contains dataBefore //Create a new node that contains data and insert it immediately after the dataBefore node //For example if I call insertAfter(2,7) with the list [1,2,3] //the new list would be [1,2,7,3] //Returns true if the method successfully inserts the song and false otherwise public boolean insertAfter(Song prevSong, Song songToAdd) { Node current = head; while(current != null) { if(current.song.equals(prevSong)) { Node nextnext = current.next; current.next = new Node(songToAdd,nextnext); return true; } current= current.next; } return false; } //****IMPLEMENT THIS*******// //Reverse the linked list //You can only use one while loop public void reverse() { Node prev = null; Node current = head; Node next = null; while(current!= null) { next = current.next; current.next=prev; prev= current; current = next; } } //****IMPLEMENT THIS*******// //Swap two nodes in a linked list //Don't just swap the data in the nodes, swap the actual nodes //If either i or j don't exist in the list, print a useful message to the console public void swap(Song i, Song j) { Node temp1 = head; while (temp1 != null) { if (temp1.song.equals(i)) { break; } temp1 = temp1.next; } if (temp1 == null) { System.out.println("Song " + i + " does not exist"); return; } Node temp2 = head; while (temp2 != null) { if (temp2.song.equals(j)) { break; } temp2 = temp2.next; } if (temp2 == null) { System.out.println("Song"+j+"doesnt exist"); } temp1.song= j; temp2.song= i; } public String toString() { String s = ""; Node current=head; while(current!=null) { s = s + current.song; current = current.next; s = s+"->“; } return s; } }This is what I have so far I am not sure if I am doing it correctlyComputer ScienceEngineering & TechnologyJava ProgrammingCS 46BShare Question
What are two key traits (not states) of Harlen in Thomas Kings’s…
Question Answered step-by-step What are two key traits (not states) of Harlen in Thomas Kings’s… What are two key traits (not states) of Harlen in Thomas Kings’s Medicine River? Provide two Quotations for each trait from the novel Arts & Humanities English English Literature ENGL 101 Share QuestionEmailCopy link Comments (0)

