Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In the attached code, you can see that we can use linked lists to create the ”St

ID: 3681436 • Letter: I

Question

In the attached code, you can see that we can use linked lists to create the ”Stack” data structure. There is also a Stack class that Java library provides. To understand the structure of the ”Stacks”, please take a good look into the given code. Change the items in the ”Main” class to understand how stacks are working. In this lab we will use the Stack class in order to reverse the word-order of a sentence. Stack class is very similar to the NodeList class that we used in the previous lab, but the modified version of that linked list class, that works as a Stack. In this lab, we will write a method that takes in a String, reverses the order of the words in that String, and then returns the result. Using stacks, the solution is relatively simple. All we need to do is push words onto the stack from left to right, so that we end up with a stack with the first word on the bottom, and the last word on the top. Now, when we pop them off, they will come off in reverse order (remember, stacks are LIFO).

For example:

• If we input ’hello world’ it would output ’world hello’

• If we input ’Where are my shoes?’ it would output ’shoes? my are Where’

1. Our first step is to take the input string, and split it into words. Since words are separated by whitespaces, we can use the Java String split() method, with a whitespace as the parameter. This will split the string around the whitespaces and store the result in an array for us. We can do that like shown in the code(in Main class, reversedSentence method). Now the first word in the sentence is words[0], the second is words[1] and so on. Loop through the array using a for loop, and push each word onto our stack.

2. Now the bottom of the stack is the first word, and the top of the stack is the last word. Use another loop to pop each item off the stack and print it. Since the top item of the stack is the last word in the original sentence, it will now be the first word printed.

3. Test it by pushing the words of the sentence into the Stack in the main method and using the iterate method, check if they’re printing the same results with the previous item.

Then, in your main method, test the reverseSentence method with a few different sentences and see how it works. A basic test outline and Stack code is provided for you.

MAIN.JAVA

package com.list;

public class Main
{
public static void main(String[] args)
{
Main mainClass = new Main();
Stack list = new Stack();
  
list.push("Book");
list.push("Lappy");
System.out.println("Length : "+list.size());

  
System.out.println("Pop element : "+list.pop());
  
System.out.println("Pop element : "+list.pop());
//step 3: change the words in here to match your sentence, see how they're printing it in the iterate method.
list.push("Book");
list.push("Lappy");
list.push("Glass");
System.out.println("Length : "+list.size());
  
list.iterate();
//TO-DO enter your sentence here
mainClass.reversedSentence("Write your sentence here");
}
  
//TO-DO
  
public String reversedSentence(String sentence){
String reverseSentence = "";
//split the sentence into words, and make each word the element in an array(Done)
String[] words = sentence.split(" ");
//TO-DO
//step1: push each element of the array onto the stack

  
//step 2: pop each element off the stack from top to botton and print it in here
  

return reverseSentence;
}
}

NODE.JAVA
package com.list;


public class Node {
private String data;
private Node next;

public Node(String data) {
this.data = data;
this.next = null;
}
  
public Node(String data, Node next){
this.data = data;
this.next = next;
}

/**
* @return data
*/
public String getData() {
return data;
}

/**
* @param data :the data to set
*/
public void setData(String data) {
this.data = data;
}

/**
* @return String of the data
*/
public String toString() {
return "Data: "+ data.toString();
}

/**
* @return the next
*/
public Node getNext() {
return next;
}

/**
* @param next :the next to set
*/
public void setNext(Node next) {
this.next = next;
}
}

STACK.JAVA

package com.list;

public class Stack {

private int size = 0;
private Node root = null;

  

/*
* It has to return the size of the NodeList
*
* @return size
*/
public int size() {
return size;
}

// add an element to the stack
public void push(String item) {
Node oldfirst = root;
root = new Node(item, oldfirst);
size++;
}

// delete and return the most recently added element
public Object pop() {
if (isEmpty()) throw new RuntimeException("Stack underflow");
String item = root.getData(); // save item to return
root = root.getNext(); // delete first node
size--;
return item; // return the saved item
}

// is the stack empty?
public boolean isEmpty() { return root == null; }
  
/**
* Start with the head and traverse till you reach null.
*/
public void iterate(){
System.out.println("Iterate forward...");
Node current = root;
while(current!=null){
System.out.println(current.toString());
current = current.getNext();
}
}
  
  
}

I BELIEVE ONLY CHANGES SHOULD BE MADE UNDER THE COMMENTS OF STEP1 & STEP2 OF MAIN.JAVA (AS WELL AS SWITCHING THE WORDS FOR A SPECIFIC SENTENCE)

Explanation / Answer

Hi I have implemented required functionality in Main.java

import java.util.Scanner;

public class Main
{
   public static void main(String[] args)
   {
       Main mainClass = new Main();
       Stack list = new Stack();

       list.push("Book");
       list.push("Lappy");
       System.out.println("Length : "+list.size());

       System.out.println("Pop element : "+list.pop());

       System.out.println("Pop element : "+list.pop());
       //step 3: change the words in here to match your sentence, see how they're printing it in the iterate method.
       list.push("Book");
       list.push("Lappy");
       list.push("Glass");
       System.out.println("Length : "+list.size());

       list.iterate();
       //TO-DO enter your sentence here
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter your sentence");
       String sentence = sc.nextLine();
       System.out.println(mainClass.reversedSentence(sentence));
   }

   //TO-DO

   public String reversedSentence(String sentence){
       String reverseSentence = "";
       //split the sentence into words, and make each word the element in an array(Done)
       String[] words = sentence.split("\s+");
       Stack list = new Stack();
       //TO-DO
       for(String s: words){
           list.push(s); // pusing each words
       }


       //step 2: pop each element off the stack from top to botton and print it in here
       while(!list.isEmpty())
           reverseSentence = reverseSentence+list.pop()+" ";

       return reverseSentence;
   }
}

/*

Output:

Length : 2
Pop element : Lappy
Pop element : Book
Length : 3
Iterate forward...
Data: Glass
Data: Lappy
Data: Book
Enter your sentence
Hi this is the sentatence that I want to reverse word by word
word by word reverse to want I that sentatence the is this Hi

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote