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

1. Modify the Stack class provided below so that it can be used for different ty

ID: 3817296 • Letter: 1

Question

1. Modify the Stack class provided below so that it can be used for different types.

2. Construct a class 'Main.java' from scratch to test your Stack class, using methods and constructors shown below. This following class contains only the constructors and methods templates. that contains all your test statements. Need to print the output to output file 'out2b.txt'.

---- Add required code in Main.java (Sample program)

Need to implement this step by step as shown below.

String myName = "(Name) ";

  
   char chA = 'A';
   char chB = 'B';
   char chC = 'C';
   char chD = 'D';
  
   double dA = 93.7;
   double dB = 7;
   double dC = 10;
   double dD = 6;
  

// Need to make methods for these
cStack();   // a Stack for Character Class
dStack();   // a Stack for Double Class
vStack();   // a Stack for Variable Class
  
   //Print name to the first line of output file
  
   cStack.push('A');
   cStack.push('B');
   cStack.push('C');
   // Display the contents of cStack to the output file
  
   cStack.push('D');
     // Display the contents of cStack to the output file

// Print to ouput file ("Character popped: " + cStack.pop());

// Print the contents of cStack to output file

// Print to ouput file ("Character popped: " + cStack.pop());

// Print to ouput file ("Character popped: " + cStack.pop());

// Print to ouput file ("Character popped: " + cStack.pop());

// Print to ouput file ("Character popped: " + cStack.pop());

// Print to ouput file ("Character popped: " + cStack.pop());


   dStack.push(dA);
   dStack.push(dC);
   // Print the contents of dStack to output file
  
vStack.push(new Variable(chA, dA));

  vStack.push(new Variable(chB, dB));  

vStack.push(new Variable(chC, dC));

// print the contents of vSatck to output file

Sample output to output file 'out2b.txt': (Step-by-Step shown)

(Name)

Stack Pointer   Value
97 C
98 B
99 A


Stack Pointer   Value
96 D
97 C
98 B
99 A

Character Popped: D

Stack Pointer   Value
97 C
98 B
99 A

Character Popped: C
Character Popped: B
Character Popped: A

Stack is Empty. Nothing to pop!

Character Popped: null

Stack is Empty. Nothing to pop!

Character Popped: null

Stack Pointer   Value

98 10.0
99 93.7

Stack Pointer   Value
97 C 10.0
98 B 7.0
99 A 93.7

Required Programs: (Modify code as required)

Stack.java:

import java.util.NoSuchElementException;

public class Stack {
   private int size; // size of the stack
private int array[], first, sp; // top of the stack, stack, and stack pointerz
  
// constructor for declaring values for variables
public Stack(int n)
{
size = 5;   // constant size of the stack
array = new int[size];
first = -1;
sp = 0;
}

/**
* Returns true if this stack is empty.
*
* @return true if this stack is empty; false otherwise
*/
public boolean isEmpty() {
return first == -1;
}

/* Function to check if stack is full */
public boolean isFull()
{
return first == size -1 ;
}
  
/**
* Returns the number of items in this stack.
*
* @return the number of items in this stack
*/
public int size() {
return size;
}
  
/**
* Adds the item to this stack.
*
* @param item the item to add
*/
public void push(int i)
{
if(first + 1 >= size)
throw new IndexOutOfBoundsException("Overflow Exception");
if(first + 1 < size )
array[++first] = i;
sp++ ;
}
/**
* Removes and returns the item most recently added to this stack.
*
* @return the item most recently added
* @throws NoSuchElementException if this stack is empty
*/
public int pop()
{
if( isEmpty() )
throw new NoSuchElementException("Underflow Exception");
sp-- ;
return array[first--];
}
  
/* Function to check the top element of the stack */
public int peek()
{
if( isEmpty() )
throw new NoSuchElementException("Underflow Exception");
return array[first];
}

/**
* Returns a string representation of this stack.
*/
public void Display()
{
   System.out.print(" Stack : ");
if (sp == 0)
{
System.out.print("Empty ");

}
for (int i = first; i >= 0; i--)
System.out.print(array[i]+" ");
System.out.println();
}
}

Explanation / Answer

This answer contains 3 files. Compile with 1.5 jre in Eclipse. Logic is to use Generics to enable different stack types.

Sample output generated(out2b.txt) file.

(Name)

Stack Pointer Value
1 C
2 B
3 A
Stack Pointer Value
1 D
2 C
3 B
4 A
Character popped: D
Stack Pointer Value
1 C
2 B
3 A
Character popped: C
Character popped: B
Character popped: A
Stack is Empty. Nothing to pop
Character popped: null
Stack is Empty. Nothing to pop
Character popped: null
Stack Pointer Value
1 10.0
2 93.7
Stack Pointer Value
1 C 10.0
2 B 7.0
3 A 93.7

Variable.java //contains class Variable


public class Variable {
   public Variable(Character character, Double doubleVal) {
       this.character = character;
       this.doubleVal = doubleVal;
   }
   private Character character;
   private Double doubleVal;
  
   public String toString()
   {
       return character + " " + doubleVal;
   }
        
}

Stack.java //Modified Stack

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.NoSuchElementException;

public class Stack<T> {
private int size,first, sp; // size of the stack, top of the stack, stack pointer
private T array[] ; // stack
  
// constructor for declaring values for variables
@SuppressWarnings("unchecked")
   public Stack(int n)
{
size = n; // constant size of the stack
array = (T[])new Object[size];
first = -1;
sp = 0;
}

/**
* Returns true if this stack is empty.
*
* @return true if this stack is empty; false otherwise
*/
public boolean isEmpty() {
return first == -1;
}
/* Function to check if stack is full */
public boolean isFull()
{
return first == size -1 ;
}
  
/**
* Returns the number of items in this stack.
*
* @return the number of items in this stack
*/
public int size() {
return size;
}
  
/**
* Adds the item to this stack.
*
* @param item the item to add
*/
public void push(T i)
{
if(first + 1 >= size)
System.out.println("Stack is Full.");
if(first + 1 < size )
array[++first] = i;
sp++ ;
}
/**
* Removes and returns the item most recently added to this stack.
*
* @return the item most recently added
* @throws NoSuchElementException if this stack is empty
*/
public T pop()
{
if( isEmpty() )
{
System.out.println("Stack is Empty. Nothing to pop");
return null;
}
sp-- ;
return array[first--];
}
  
/* Function to check the top element of the stack */
public T peek()
{
if( isEmpty() )
{
System.out.println("Stack is Empty. Nothing to pop");
return null;
}
return array[first];
}
/**
* Returns a string representation of this stack.
* @throws IOException
*/
public void Display() {
System.out.println("Stack Pointer Value");
if (sp == 0)
{
   System.out.print("Empty ");

}
for (int i = first; i >= 0; i--)
   System.out.println((sp-i) + " " + array[i]);
}
}

Main.Java //Main class

import java.io.*;
import java.io.IOException;

public class Main {

   public static void main(String[] args) {
       String myName = "(Name) ";
      
   char chA = 'A';
   char chB = 'B';
   char chC = 'C';
   char chD = 'D';
  
   double dA = 93.7;
   double dB = 7;
   double dC = 10;
   double dD = 6;
  
   Stack<Character> cStack = new Stack<Character>(10);
   Stack<Double> dStack = new Stack<Double>(10);
   Stack<Variable> vStack = new Stack<Variable>(10);
  
   try {
           File file = new File("out2b.txt");
           FileOutputStream fos = new FileOutputStream(file);
           PrintStream ps = new PrintStream(fos);
           System.setOut(ps);;
          
           System.out.println(myName); //Print name to the first line of output file

       cStack.push('A');
       cStack.push('B');
       cStack.push('C');
      
       cStack.Display();
       cStack.push('D');
       cStack.Display();

       System.out.println("Character popped: " + cStack.pop());
       cStack.Display();
       System.out.println("Character popped: " + cStack.pop());
       System.out.println("Character popped: " + cStack.pop());
       System.out.println("Character popped: " + cStack.pop());
       System.out.println("Character popped: " + cStack.pop());
       System.out.println("Character popped: " + cStack.pop());

       dStack.push(dA);
       dStack.push(dC);
       dStack.Display();
      
           vStack.push(new Variable(chA, dA));
           vStack.push(new Variable(chB, dB));
           vStack.push(new Variable(chC, dC));
           vStack.Display();

       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
   }

}