Question: Please include a main to see the output. Question 3 : Write a method in the Demo class ( name it reverseString ) , call

Please include a main to see the output.
Question 3:
Write a method in the Demo class (name it reverseString), call the method from the main() and pass a string to it. This method will do the following:
Receives a string (sentence, not just one word).
Stores the received string in an linkedlist-based stack
Prints the sentence in reverse.
Example:
Call statement: reverseString("Testing my reverseString method");
output: dohtem gnirtSesrever ym gnitseT
Question 4:
Write a method in the Demo class (name it reverseNumber), call the method from the main() and pass an integer to it. This method will do the following:
Receives an integer.
Stores the received integer in an array-based stack
Prints the integer in reverse.
Example:
Call statement: reverseNumber(5467);
output: 7645
**Demo class below**
public class LinkedStack {
private Node top = null;
public boolean empty(){
return top == null;
}
public void push(String s){
top = new Node(s, top);
}
public String pop(){
String retValue;
if (empty()){
throw new IllegalStateException();
} else {
retValue = top.element;
top = top.next;
return retValue;
}
}
public String peek(){
if (empty()){
throw new IllegalStateException();
} else {
return top.element;
}
}
public String toString(){
Node ref = top;
String toPrint ="";
while (ref != null){
toPrint += ref.element;
ref = ref.next;
}
return toPrint;
}
// Node class used for linked list
private class Node {
String element;
Node next;
Node(String e, Node n){
element = e;
next = n;
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!