In: Computer Science
Test Memory Usage on the Text File Reversing Problem (1
mark)
Test the performance of your Stack implementation on file reversing
task. The code below is to reverse a text file using the stack. It
has three lines missing, and you need to complete it. Then you
submit it on our marking site here. You need to change the stack
capacity according to input data size. On average each line has
about 11 words. Our site prints out the total computer memory usage
by your program when it runs on 5 data sets of different
sizes.
The total memory usage from the submission site is
static String[] reverse(String filename) throws Exception{
Scanner scanner = new Scanner(new
File(filename)).useDelimiter("[^a-zA-Z]+");
Stack2540Array stack = new
Stack2540Array();
while (scanner.hasNext())
stack.push(scanner.next().toLowerCase());
String[] rev = new
String[stack.size()];
/* for (int i = 0; i < stack.size(); i++) {
rev[i] =
stack.pop();
} */
return rev;
}
The code for Stack2540Array cannot be modified:
import java.io.*;
import java.util.*;
public class Stack2540Array {
int CAPACITY = 128;
int top;
String[] stack;
public Stack2540Array() {
stack = new String[CAPACITY];
top = -1;
}
public int size() {
return top + 1; }
public boolean isEmpty() {
return (top == -1); }
public String top() {
if (top == -1)
return
null;
return stack[top];
}
public void push(String element) {
top++;
stack[top] = element;
}
public String pop() {
if (top == -1) {
return null;
}
else {
return stack[top--];
}
}
}
The lines are added as comments, but the output shows: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 128 out of bounds for length 128. Your Stack and reverse() can run, but the result is not correct. Can you please fix the error in the code?
* The complete program is not available as we are supposed to work with code fragments.
Here to fix this error I am adding a new variable called "count" which keeps tracking when every new element is pushed into the stack.
I implemented the driver code to test it.
CODE:
import java.io.*;
import java.util.*;
class Stack2540Array {
int CAPACITY = 128;
int top;
String[] stack;
public Stack2540Array() {
stack = new String[CAPACITY];
top = -1;
}
public int size() { return top + 1; }
public boolean isEmpty() { return (top == -1); }
public String top() {
if (top == -1)
return null;
return stack[top];
}
public void push(String element) {
top++;
stack[top] = element;
}
public String pop() {
if (top == -1) {
return null;
}
else {
return stack[top--];
}
}
}
public class Main{
static String[] reverse(String filename) throws Exception{
Scanner scanner = new Scanner(new File(filename)).useDelimiter("[^a-zA-Z]+");
Stack2540Array stack = new Stack2540Array();
int count=0;//Introducing a new variable
while (scanner.hasNext()){
stack.push(scanner.next().toLowerCase());
count+=1;//Keeping a track when every new element is pushed
}
String[] rev = new String[stack.size()];
for (int i = 0; i < count; i++) {//using the count as the size
rev[i] = stack.pop();;
}
return rev;
}
//Driver code to test for output
public static void main(String[] args) throws Exception{
String a[]=reverse("test.txt");
for(String i:a){
System.out.println(i);}
}
}
Screenshots of the Code for your reference:
The text file and the output of the code:
Thank you! if you have any queries post it below in the
comment section I will try my best to resolve your queries and I
will add it to my answer if required. Please give upvote if you
like my work.