In: Computer Science
1. create a class called ArrayStack that is a generic class. Create a main program to read in one input file and print out the file in reverse order by pushing each item on the stack and popping each item off to print it. The two input files are: tinyTale.txt and numbers.txt. Rules: You cannot inherit the StackofStrings class.
2. Using your new ArrayStack, create a new class called RArrayStack. To do this, you need
a) remove the capacity parameter from the constructor and create the array with 8 as the starting size
b) create a new method called resize that takes a parameter (capacity)
c) change push() to check for length of array. If it is at the limit then call resize to increase it to twice the size Use the same main program from 1 and the two input files to test out your program. RULE: You cannot use ArrayList – you must use a primitive Java arrays.
import java.util.Iterator;
import java.util.NoSuchElementException;
public class StackOfStrings implements
Iterable<String> {
private String[] a; // holds the items
private int N; // number of items in stack
// create an empty stack with given capacity
public StackOfStrings(int capacity) {
a = new String[capacity];
N = 0;
}
public boolean isEmpty() {
return N == 0;
}
public boolean isFull() {
return N == a.length;
}
public void push(String item) {
a[N++] = item;
}
public String pop() {
return a[--N];
}
public String peek() {
return a[N-1];
}
public Iterator<String> iterator() {
return new ReverseArrayIterator();
}
public class ReverseArrayIterator implements
Iterator<String> {
private int i = N-1;
public boolean hasNext() {
return i >= 0;
}
public String next() {
if (!hasNext()) throw new NoSuchElementException();
return a[i--];
}
public void remove() {
throw new UnsupportedOperationException();
}
}
}
Numbers.txt
20
7
99
88
1
2
3
4
30
16
19
50
55
60
61
6
68
28
32
--------------------------------------------------------------
tinyTale.txt
it was the best of times it was the worst of times
it was the age of wisdom it was the age of foolishness
it was the epoch of belief it was the epoch of incredulity
it was the season of light it was the season of darkness
it was the spring of hope it was the winter of despair
Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks.
ArrayStack.java
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ArrayStack<T> implements Iterable<T>
{
private T[] a; // holds the items
private int N; // number of items in stack
// create an empty stack with given capacity
public ArrayStack(int capacity) {
a = (T[]) new
Object[capacity];
N = 0;
}
public boolean isEmpty() {
return N == 0;
}
public boolean isFull() {
return N == a.length;
}
public void push(T item) {
a[N++] = item;
}
public T pop() {
return a[--N];
}
public T peek() {
return a[N - 1];
}
public Iterator<T> iterator() {
return new
ReverseArrayIterator();
}
public class ReverseArrayIterator implements
Iterator<T> {
private int i = N - 1;
public boolean hasNext() {
return i >=
0;
}
public T next() {
if
(!hasNext())
throw new NoSuchElementException();
return
a[i--];
}
public void remove() {
throw new
UnsupportedOperationException();
}
}
}
RArrayStack.java
import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class RArrayStack<T> implements Iterable<T>
{
private T[] a; // holds the items
private int N; // number of items in stack
// create an empty stack with given capacity
public RArrayStack() {
a = (T[]) new Object[8];
N = 0;
}
public void resize(int capacity){
T[] largerArray = Arrays.copyOf(a,
2*capacity);
a=largerArray;
}
public boolean isEmpty() {
return N == 0;
}
public boolean isFull() {
return N == a.length;
}
public void push(T item) {
if(isFull()){
resize(a.length);
//System.out.println("New length"+a.length);
}
a[N++] = item;
}
public T pop() {
return a[--N];
}
public T peek() {
return a[N - 1];
}
public Iterator<T> iterator() {
return new
ReverseArrayIterator();
}
public class ReverseArrayIterator implements
Iterator<T> {
private int i = N - 1;
public boolean hasNext() {
return i >=
0;
}
public T next() {
if
(!hasNext())
throw new NoSuchElementException();
return
a[i--];
}
public void remove() {
throw new
UnsupportedOperationException();
}
}
}
Main.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void arrayStack(){
BufferedReader reader;
try {
ArrayStack<Integer> number=new
ArrayStack<Integer>(30);
ArrayStack<String> text=new
ArrayStack<String>(30);
reader = new
BufferedReader(new FileReader(
"Numbers.txt"));
String line =
reader.readLine();
while (line !=
null) {
// read next line
number.push(Integer.parseInt(line));
line = reader.readLine();
}
reader.close();
for(Object
item : number){
System.out.println(item.toString());
}
reader = new
BufferedReader(new FileReader(
"tinyTale.txt"));
line =
reader.readLine();
while (line !=
null) {
// read next line
text.push(line);
line = reader.readLine();
}
reader.close();
for(Object
item : text){
System.out.println(item.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void rArrayStack(){
BufferedReader reader;
try {
RArrayStack<Integer> number=new
RArrayStack<Integer>();
RArrayStack<String> text=new
RArrayStack<String>();
reader = new
BufferedReader(new FileReader(
"Numbers.txt"));
String line =
reader.readLine();
while (line !=
null) {
// read next line
number.push(Integer.parseInt(line));
line = reader.readLine();
}
reader.close();
for(Object
item : number){
System.out.println(item.toString());
}
reader = new
BufferedReader(new FileReader(
"tinyTale.txt"));
line =
reader.readLine();
while (line !=
null) {
// read next line
text.push(line);
line = reader.readLine();
}
reader.close();
for(Object
item : text){
System.out.println(item.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println("Array Stack
:");
arrayStack();
System.out.println("RArray Stack
:");
rArrayStack();
}
}
Output: