All code should be properly documented, commented and have proper unit testing i
ID: 3747190 • Letter: A
Question
All code should be properly documented, commented and have proper unit testing implemented in main(). Explain ALL lines of code. Assume that the data stored in each element is a character or integer (you may choose whichever you think is more convenient) All JAVA implementations should implement a method returning string representation of the list queue where each element is placed between brackets "[x]" and adjacent elements are separated by a comma" You are not allowed to use Java library implementations of the data structures (queues, lists etc.) You should implement the internals yourself! Question: In JAVA implement a recursive and an iterative version of a function which reads characters from stdin until a newline character is read and then prints them on stdout in reverse order. Hint: use getchar), putchar() (or getc(), putc)). For the iterative version you may assume a fixed max length of the input. Use one of these ADTs: BST, String, Graph, TrieST, TST, PQ, LinearProbingHashST, SeperateChainingHashST, String, UnionFindExplanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// Test.java
import java.util.Scanner;
public class Test {
/**
* method to read a String, print it in reverse order using iterative
* approach
*/
static void printStringReverseIterative() {
// scanner to read input
Scanner scanner = new Scanner(System.in);
// reading a line of text (terminated by newline character)
String line = scanner.nextLine();
// looping through the line of text in reverse order
for (int i = line.length() - 1; i >= 0; i--) {
// displaying the character at i'th index
System.out.print(line.charAt(i));
}
System.out.println();
}
/**
* method to read a String, print it in reverse order using recursive
* approach
*/
static void printStringReverseRecursive() {
Scanner scanner = new Scanner(System.in);
// reading a line of text (terminated by newline character)
String line = scanner.nextLine();
// calling the recursive method, passing the string as argument
printStringReverseRecursive(line);
System.out.println();
}
/**
* this is the main version of printStringReverseRecursive() method which
* actually performs the recursion
*
* @param str
* line of String
*/
static void printStringReverseRecursive(String str) {
// checking if string is not empty (base condition)
if (str.length() > 0) {
// displaying character at the last index
System.out.print(str.charAt(str.length() - 1));
// calling the method again, passing the string with one less
// character at the end
printStringReverseRecursive(str.substring(0, str.length() - 1));
}
}
public static void main(String[] args) {
//testing both methods
System.out.println("Testing iterative String reverse function.");
System.out.println("Enter a text and press enter:");
printStringReverseIterative();
System.out.println("Testing recursive String reverse function.");
System.out.println("Enter a text and press enter:");
printStringReverseRecursive();
}
}
/*OUTPUT*/
Testing iterative String reverse function.
Enter a text and press enter:
hello world
dlrow olleh
Testing recursive String reverse function.
Enter a text and press enter:
Java is amazing!
!gnizama si avaJ
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.