Question

In: Computer Science

JAVA I need to make it that I can expand the stack for everytime a push...

JAVA

I need to make it that I can expand the stack for everytime a push operation is performed that would normally cause an overflow, returning a false value. Should accompany 3 nodes initially.

DriverStack.java

public class DriverStack {
  
public static void main (String[] args)
{
  
Stack s = new Stack(3);
Listing l;
Listing l1 = new Listing ("Bill", "1st Avenue", "123 4567");
Listing l2 = new Listing ("Alec", "2nd Avenue", "456 4567");
Listing l3 = new Listing ("James", "3rd Avenue", "367 8896");
Listing l4 = new Listing ("Nico", "4th Avenue", "322 1156");
  
System.out.println(s.pop());
System.out.println(s.push(l1));
System.out.println(s.push(l2));
System.out.println(s.push(l3));
System.out.println(s.push(l4));
  
System.out.println("\n");

System.out.println("----------------All listings with one over flow----------------" + "\n");
  
s.showAll(); //shows listings
  
System.out.println("----------------Tries to pop the Listing '1'----------------" + "\n");

//Three pop operations
l = s.pop();
System.out.println(l.toString());
  
l = s.pop();
System.out.println(l.toString());
  
l = s.pop();
System.out.println(l.toString());


l = s.pop(); //will try to pop an already empty stack
  
System.out.println(l);
System.exit(0);
  
}
  
}

Stack.java

public class Stack
{
private Listing[] data;
private int top;
private int size;
  
public Stack()
{
top = -1;
size = 3;
data = new Listing[3];
}
public Stack (int n)
{
top = -1;
size = n;
data = new Listing[n];
}
  
public void settop()
  
{
top=-1;
}
  
  
public boolean push(Listing newNode)
{
if(top == size - 1)
return false; //overflow
else
{

top = top + 1;
data[top] = newNode.deepCopy();
return true;
}
}
  
public Listing pop()
{
int topLocation;
if(top == -1)
  
return null; //underflow error
else
{
topLocation = top;
top = top - 1;
return data[topLocation];
  
}
}
  
public void showAll()
{
for (int i = top; i >= 0; i--)
System.out.println(data[i].toString());
  
}
  
public void initialStack()
{
top = -1;
}
  
public boolean StackEmpty()
  
{
if(top == -1)
return true;
else
return false;
  
}
  
public boolean StackFull()
  
{
if(top == size-1)
return true;
else
return false;
  
}
  
public Listing peek()

{

if(StackEmpty())

{

System.out.println("Stack is underflow");

return null;

}

else

{

return data[top];

}

}
  
  

  
}

There is one more class called Listing but I dont think anything needs done with that.

Please comment next to the new code that was added, Thank you!

Solutions

Expert Solution

If you have any doubts, please give me comment...

public class Stack {

    private Listing[] data;

    private int top;

    private int size;

    public Stack() {

        top = -1;

        size = 3;

        data = new Listing[3];

    }

    public Stack(int n) {

        top = -1;

        size = n;

        data = new Listing[n];

    }

    public void settop()

    {

        top = -1;

    }

    public boolean push(Listing newNode) {

       if (top == size - 1){

            Listing[] temp = new Listing[size+1];

            for(int i=0; i<size; i++){

                temp[i] = data[i];

            }

            data = temp;

            size++;

        }

       top = top + 1;

        data[top] = newNode.deepCopy();

        return true;

    }

    public Listing pop() {

        int topLocation;

        if (top == -1)

            return null; // underflow error

        else {

            topLocation = top;

            top = top - 1;

            return data[topLocation];

        }

    }

    public void showAll() {

        for (int i = top; i >= 0; i--)

            System.out.println(data[i].toString());

    }

    public void initialStack() {

        top = -1;

    }

    public boolean StackEmpty()

    {

        if (top == -1)

            return true;

        else

            return false;

    }

    public boolean StackFull()

    {

        if (top == size - 1)

            return true;

        else

            return false;

    }

    public Listing peek() {

        if (StackEmpty()) {

            System.out.println("Stack is underflow");

            return null;

        } else {

            return data[top];

        }

    }

}


Related Solutions

JAVA Stack - Implementation. You will be able to use the push, pop and peek of...
JAVA Stack - Implementation. You will be able to use the push, pop and peek of Stack concept. Post-Fix calculator - When an arithmetic expression is presented in the postfix form, you can use a stack to evaluate the expression to get the final value. For example: the expression 3 + 5 * 9 (which is in the usual infix form) can be written as 3 5 9 * + in the postfix. More interestingly, post form removes all parentheses...
(In Java) Design a stack that supports getMin(), pop() and push() in O(1) time. Must use...
(In Java) Design a stack that supports getMin(), pop() and push() in O(1) time. Must use the iterator and comparator, does not need to be a linked list, although it's what I've tried using. I'm using another tester class to test this class. import java.util.Comparator; import java.util.List; import java.util.LinkedList; import java.util.Iterator; import java.util.Stack; import java.util.ListIterator; public class FMinStack<T> implements MinStack<T> { protected Comparator<? super T> comp; T min; protected List<T> ds; public FMinStack() { this(new DefaultComparator<T>()); } public FMinStack(Comparator<? super...
I need to make JAVA program that calculates the area of a brick of a specific...
I need to make JAVA program that calculates the area of a brick of a specific color. I have class Brick with the following UML: String color int number int length int width I made methods: getColor(), getNumber(), getLength(), getWidth() in the class Brick So it starts like this: ##### public Brick(String color, int number, int length, int width) { this.color = color; this.number = number; this.length = length; this.width = width;    }   public String toString() {   return number...
I need to make this into a Java Code: Write a program that prompts the user...
I need to make this into a Java Code: Write a program that prompts the user for a double value representing a radius. You will use the radius to calculate: circleCircumference = 2πr circleArea = πr2 sphereArea = 4πr2 sphereVolume = 43πr3 You must use π as defined in the java.lang.Math class. Your prompt to the user to enter the number of days must be: Enter radius: Your output must be of the format: Circle Circumference = circleCircumference Circle Area...
Make a java program of Mickey I have the starter program but I need to add...
Make a java program of Mickey I have the starter program but I need to add eyes and a smile to it. import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; import javax.swing.JFrame; public class Mickey extends Canvas { public static void main(String[] args) { JFrame frame = new JFrame("Mickey Mouse"); Canvas canvas = new Mickey(); canvas.setSize(400, 400); canvas.setBackground(Color.white); frame.add(canvas); frame.pack(); frame.setVisible(true); } public void paint(Graphics g) { Rectangle bb = new Rectangle(100, 100, 200, 200); mickey(g, bb); } public void...
Reverse the contents of a stack using only stack operations [ push() and pop()  ]. Using the...
Reverse the contents of a stack using only stack operations [ push() and pop()  ]. Using the java and give the explanation
Please I need this to be done in Java, can I have it answered by anonymous...
Please I need this to be done in Java, can I have it answered by anonymous who answered my last question regarding java? Thank you You will need to implement a specific algorithm implementation to match the class definition AND implement a unit test using JUnit that conforms the specific naming convention. Algorithm: Inputs: integers m and n , assumes m and n are >= 1 Order is not important for this algorithm Local variables integers: remainder Initialize: No initialization...
Please Why do we need a dynamic stack? How to implement a dynamic array stack?(JAVA)
Please Why do we need a dynamic stack? How to implement a dynamic array stack?(JAVA)
Using java, I need to make a program that reverses a file. The program will read...
Using java, I need to make a program that reverses a file. The program will read the text file character by character, push the characters into a stack, then pop the characters into a new text file (with each character in revers order) EX: Hello World                       eyB       Bye            becomes    dlroW olleH I have the Stack and the Linked List part of this taken care of, I'm just having trouble connecting the stack and the text file together and...
So I need to make a java program that reverse or replace first or lastchar or...
So I need to make a java program that reverse or replace first or lastchar or remove char from user's string input. length, concat, charAt, substring, and equals (or equalsIgnoreCase) thses are the string method that only I can use for example if user put asdf asdf and chose reverse input should be fdsa fdsa if user put asdf asdf and chose replace first a with b input should be bsdf asdf if user put asdf asdf and chose remove...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT