that's the "TwoEndedSLL.java" file
/ TwoEndedSll.java package main; public class TwoEndedSll{ // declear the TwoEndedSll private static class Node{ private T data; private Node next; Node(T data, Node next){ this.data = data; this.next = next; } } Node head, tail; TwoEndedSll(){ this.head = null; this.tail = null; } // add at Head public void addAtHead(T data){ if(head == null){ head = tail = new Node(data, null); } else{ head = new Node(data, head); } } // add At tail public void addAtTail(T data){ if(tail == null){ head = tail = new Node(data, null); } else{ tail.next = new Node(data, null); tail = tail.next; } } // print the content of liked list public void printContent(){ Node temp = head; System.out.print( "content: [" ); while(temp != null){ System.out.print(temp.data); if(temp != tail){ System.out.print(","); } temp = temp.next; } System.out.println("]"); } } // App.java package main; import java.util.Scanner; public class App{ public void run(){ displayInstructions(); TwoEndedSll list = new TwoEndedSll(); Scanner sc = new Scanner(System.in); System.out.print( "> " ); String str = sc.nextLine(); boolean addInHead = true; // we will read till an emtpy line is not entered while(!str.equals("")){ if(addInHead){ list.addAtHead(str.toLowerCase()); addInHead = false; } else{ list.addAtTail(str.toLowerCase()); addInHead = true; } list.printContent(); System.out.print( "> " ); str = sc.nextLine(); } System.out.println( "Done now! Thanks!" ); } private void displayInstructions(){ System.out.println( "Hi| I'm the Two-Ended List app!" ); System.out.println( "Every word you enter on a line will be added to me in alternating head/tail order." ); System.out.println( ); System.out.println( "I will show you my content after every word" ); System.out.println( "I will stop when you enter an emtpy line." ); System.out.println( "Ready? Start entering words!" ); } }
Here is an example run of a working solution (user input in bold): Hi! I'm the Chibi Linked Stack app! Every word you enter on a line will be added to me. I will turn each word to uppercase before it is added. After every two words pushed onto me, I'll pop one. I will stop when you enter an empty line. Ready? Start entering words! > sky SKY > why | WHY SKY That's 2 pushes! Popping [WHY | SKY > nigh NIGH SKY > try | TRY NIGH SKY That's 2 pushes! Popping ITRYI NIGH SKY > ply PLY NIGH SKY Done now! Thanks! 2. Create a ChibilinkedStack
class that implements the given PlainoldStack interface. The behaviour of ChibilinkedStack is described in the Preamble: i. Use your TwoEndedsil class to get this job done - you should be pleasantly surprised at how quickly this goes, because TwoEndeds11 does all the heavy lifting! ii. if all the ChibilinkedStack tests are passing, you can consider your ChibiLinkedStack to be working okay! 3. Complete the App.java run method so that when you run Main.java , the program behaves as expected by the MainTests. i. I've added some useful methods in App.java to make your life easier. It's best to start off small when dealing with linked structures. So we are. Hence the FE. Usually we reach for a linked structure because we want the flexibility of adding things without worrying about running over some sort of capacity (like we would with an array). But we're going to create a very small stack that only holds 3 things...and still force you to use linked structures. 'cause learning. Here are some details about the class - ChibilinkedStack - that you will be making. it will be generic...so you can toss whatever reference type you want in it. But... ...it can only hold 3 things; if you try and push something into ole chibi-chan when it's full, it will throw an unchecked Fullstackexception. You'll have to make this custom exception class by yourself: here's a reasonable how-to: https://stackabuse.com/how-to-make-custom- exceptions-in-java/#customuncheckedexception o Make sure you use the Custom Unchecked Exception section. Not the Checked stuff. if you try to pop or top from chibi when it's empty, it will throw a NoSuchElementException. it should use your TwoEndedsll under the hood (as its sole instance variable) package main; 1 2 import java.util.ArrayList; import java.util.List; public class App private List displayedWords-new ArrayList(); 3 9 public void run() { 1 // TODO: code this run so that it works as shown in the example // output in the instructions - and so that all the tests in MainTests pass as well > Use this when you want to add a word to the textual display of the stack and display it to a * console. . 10 21 * @param word the word going on top of the stack 23 24 private void addAndDisplay (String word) { System.out.println(); displayedWords.add(, String.format(" %s ", word)); System.out.printin(String-join(" ", displayedhords)); 1 2 23 30 * Use this when you want to remove the top word from the stack and display the result to console. 31 private void removeAndDisplay(){ System.out.println(); displayedWords.remove(); System.out.printin(String join(" ", displayedwords)); } 3 39 39 private void display Instructions() { System.out.println("Hi! I'm the Chibi Linked Stack app!"); System.out.println("Every word you enter on a line will be added to me."); System.out.printin("I will turn each word to uppercase before it is added."); (I ; System.out.println(); System.out.println("After every two words pushed onto me, I'll pop one."); System.out.println("I will stop when you enter an empty line."); System.out.println("Ready? Start entering words!"); > 5a } package main; public class Main public static void main(String[] args) { App app = new App(); app.run(); 1 package main; / "A generic Stack interface. Ho hum. author jpratt * @param the type of things in the Stack / public interface PlainoidStack { Returns true iff there is nothing in this PlainoldStack. 15 @return true iff there is nothing in this pleinoldStack * boolean IsEmpty(); Returns true iff the number of items in this PlainoldStack equals its capacity. @return true iff this PlainoldStack holds a number of items equal to its capacity . boolean isFull(); Pushes t onto the top of this PlainoldStack. 20 22 * @param t the thing to push onto the top of this plainolaStack * void push(Tt); 31 Removes the top element from this PlainoldStack. If the plainoidStack is empty, a NoSuchelementException is thrown. @return the element at the top of this PlainoldStack throws NoSuchelementException if this plainoidstack is empty . T pop(); Returns the top element of this PlainoldStack. If the PlainoldStack is empty, a NoSuchelementexception is thrown. 4 return the top element of this plainoldStack throws NoSuchelementException if this PlainoldStack is empty 45 T top(); ) Here is an example run of a working solution (user input in bold): Hi! I'm the Chibi Linked Stack app! Every word you enter on a line will be added to me. I will turn each word to uppercase before it is added. After every two words pushed onto me, I'll pop one. I will stop when you enter an empty line. Ready? Start entering words! > sky SKY > why | WHY SKY That's 2 pushes! Popping [WHY | SKY > nigh NIGH SKY > try | TRY NIGH SKY That's 2 pushes! Popping ITRYI NIGH SKY > ply PLY NIGH SKY Done now! Thanks! 2. Create a ChibilinkedStack class that implements the given PlainoldStack interface. The behaviour of ChibilinkedStack is described in the Preamble: i. Use your TwoEndedsil class to get this job done - you should be pleasantly surprised at how quickly this goes, because TwoEndeds11 does all the heavy lifting! ii. if all the ChibilinkedStack tests are passing, you can consider your ChibiLinkedStack to be working okay! 3. Complete the App.java run method so that when you run Main.java , the program behaves as expected by the MainTests. i. I've added some useful methods in App.java to make your life easier. It's best to start off small when dealing with linked structures. So we are. Hence the FE. Usually we reach for a linked structure because we want the flexibility of adding things without worrying about running over some sort of capacity (like we would with an array). But we're going to create a very small stack that only holds 3 things...and still force you to use linked structures. 'cause learning. Here are some details about the class - ChibilinkedStack - that you will be making. it will be generic...so you can toss whatever reference type you want in it. But... ...it can only hold 3 things; if you try and push something into ole chibi-chan when it's full, it will throw an unchecked Fullstackexception. You'll have to make this custom exception class by yourself: here's a reasonable how-to: https://stackabuse.com/how-to-make-custom- exceptions-in-java/#customuncheckedexception o Make sure you use the Custom Unchecked Exception section. Not the Checked stuff. if you try to pop or top from chibi when it's empty, it will throw a NoSuchElementException. it should use your TwoEndedsll under the hood (as its sole instance variable) package main; 1 2 import java.util.ArrayList; import java.util.List; public class App private List displayedWords-new ArrayList(); 3 9 public void run() { 1 // TODO: code this run so that it works as shown in the example // output in the instructions - and so that all the tests in MainTests pass as well > Use this when you want to add a word to the textual display of the stack and display it to a * console. . 10 21 * @param word the word going on top of the stack 23 24 private void addAndDisplay (String word) { System.out.println(); displayedWords.add(, String.format(" %s ", word)); System.out.printin(String-join(" ", displayedhords)); 1 2 23 30 * Use this when you want to remove the top word from the stack and display the result to console. 31 private void removeAndDisplay(){ System.out.println(); displayedWords.remove(); System.out.printin(String join(" ", displayedwords)); } 3 39 39 private void display Instructions() { System.out.println("Hi! I'm the Chibi Linked Stack app!"); System.out.println("Every word you enter on a line will be added to me."); System.out.printin("I will turn each word to uppercase before it is added."); (I ; System.out.println(); System.out.println("After every two words pushed onto me, I'll pop one."); System.out.println("I will stop when you enter an empty line."); System.out.println("Ready? Start entering words!"); > 5a } package main; public class Main public static void main(String[] args) { App app = new App(); app.run(); 1 package main; / "A generic Stack interface. Ho hum. author jpratt * @param the type of things in the Stack / public interface PlainoidStack { Returns true iff there is nothing in this PlainoldStack. 15 @return true iff there is nothing in this pleinoldStack * boolean IsEmpty(); Returns true iff the number of items in this PlainoldStack equals its capacity. @return true iff this PlainoldStack holds a number of items equal to its capacity . boolean isFull(); Pushes t onto the top of this PlainoldStack. 20 22 * @param t the thing to push onto the top of this plainolaStack * void push(Tt); 31 Removes the top element from this PlainoldStack. If the plainoidStack is empty, a NoSuchelementException is thrown. @return the element at the top of this PlainoldStack throws NoSuchelementException if this plainoidstack is empty . T pop(); Returns the top element of this PlainoldStack. If the PlainoldStack is empty, a NoSuchelementexception is thrown. 4 return the top element of this plainoldStack throws NoSuchelementException if this PlainoldStack is empty 45 T top(); )