Question

Java has a class for a stack. It is java.util.Stack. Let us make use of this...

Java has a class for a stack. It is java.util.Stack. Let us make use of this class.

Step 1: Create a project. Name the project MyStk. This will result in the creation of the main class called MyStk if you are using Netbeans IDE with relevant default settings unchanged. If comments are excluded you should see the following in the code editor after creating the project.

package mystk;
public class MyStk {
public static void main(String[ ] args){
}
}
Step 2: Now, import the class java.util.Stack to your application by adding the statement for importing (import java.util.Stack;) as shown below. package mystk;

import java.util.Stack;//recently added public
class MyStk {
public static void main(String[ ] args){
}
}
Step 3: Create an object of java.util.Stack in public static void main(String[ ] args). Name the object stkObj. The code should appear as shown below.

package mystk;
import java.util.Stack;
public class MyStk {
public static void main(String[ ] args){
Stack stkObj = new Stack();//recently added
}
}
Step 4: Push objects onto the stack (stkObj) by making use of the void push(Object e) method found in java.util.Stack. Now, your code should appear as shown below.

import java.util.Stack;
public class MyStk {
public static void main(String[ ] args){
Stack stkObj = new Stack();
stkObj.push(“Dune”); //recently added
stkObj.push(“Jane”); //recently added
stkObj.push(“Pinky”); //recently added
}
}

Step 5: Write code to display the contents of stkObj (the stack object) after pushing objects onto it. Your code should appear as shown below.

import java.util.Stack;
public class MyStk {
public static void main(String[ ] args){
Stack stkObj = new Stack();
stkObj.push(“Dune”);
stkObj.push(“Jane”);
stkObj.push(“Pinky”);
System.out.println(“Stack after pushing: ”+stkObj);//recently added
}
}
Step 5a: Run your application and expect the following display. Stack after pushing: [Dune, Jane, Pinky]

Step 6: Pop an object off the stack (stkObj) by making use of the void pop( ) method found in java.util.Stack. Now, your code should appear as shown below.

package mystk;
import java.util.Stack;
public class MyStk {
public static void main(String[ ] args){
Stack stkObj = new Stack();
stkObj.push(“Dune”); //recently added
stkObj.push(“Jane”); //recently added
stkObj.push(“Pinky”); //recently added
System.out.println(“Stack after pushing: ”+stkObj);
stkObj.pop();//recently added
}
}

Step 7: Write code to display the contents of stkObj (the stack object) after popping an object off stkObj. Your code should appear as shown below.

package mystk;
import java.util.Stack;
public class MyStk {
public static void main(String[ ] args){
Stack stkObj = new Stack();
stkObj.push(“Dune”);
stkObj.push(“Jane”);
stkObj.push(“Pinky”);
System.out.println(“Stack after pushing: ”+stkObj);
stkObj.pop();
System.out.println(“Stack after popping: ”+stkObj); //recently added
}
}

Step 7a: Run your application and expect the following display.

Stack after pushing: [Dune, Jane, Pinky]

Stack after popping: [Dune, Jane]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

NOTE : FEEL FREE TO ASK ANY DOUBTS IN THE COMMENT SECTION

CODE

TILL STEP 5

import java.util.Stack;

public class MyStk {

   @SuppressWarnings("unchecked")
   public static void main(String[] args) {
       @SuppressWarnings("rawtypes")

       Stack stkObj = new Stack();
       stkObj.push("Dune");
       stkObj.push("Jane");
       stkObj.push("Pinky");
       System.out.println("Stack after pushing: "+stkObj);
   }

}

OUTPUT in CONSOLE

CODE in EDITOR

AFTER STEP 6

import java.util.Stack;

public class MyStk {

   @SuppressWarnings("unchecked")
   public static void main(String[] args) {
       @SuppressWarnings("rawtypes")

       Stack stkObj = new Stack();
       stkObj.push("Dune");
       stkObj.push("Jane");
       stkObj.push("Pinky");
       System.out.println("Stack after pushing: "+stkObj);

       stkObj.pop();
       System.out.println("Stack after popping: "+stkObj);
   }

}

OUTPUT in CONSOLE

CODE in EDITOR

Explanation

ENTIRE CODE

// importing stack class
import java.util.Stack;

// MyStk class
public class MyStk {

   // Main method
   @SuppressWarnings("unchecked")
   public static void main(String[] args) {
       @SuppressWarnings("rawtypes")

       // Creating stack class object
       Stack stkObj = new Stack();
       // stkObj = []

       // adding value to stack
       stkObj.push("Dune");
       // stkObj = ["Dune"]
       // top element = "Dune"

       // adding value to stack
       stkObj.push("Jane");
       // stkObj = ["Dune","Jane"]
       // top element = "Jane"

       // adding value to stack
       stkObj.push("Pinky");
       // stkObj = ["Dune","Jane","Pinky"]
       // top element = "Pinky"

       // Printing stack
       System.out.println("Stack after pushing: "+stkObj);

       // Popping top element from stack
       // This will remove top element from stack
       stkObj.pop();
       // stkObj = ["Dune","Jane"]
       // top element = "Jane"

       // printing stack after above operations
       System.out.println("Stack after popping: "+stkObj);
   }

}

OUTPUT in CONSOLE

CODE in EDITOR

DEAR SIR, PLEASE DON'T FORGET TO GIVE AN UP VOTE

Thank YOU :-)

Add a comment
Know the answer?
Add Answer to:
Java has a class for a stack. It is java.util.Stack. Let us make use of this...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Design a stack class by importing the available java.util.Stack to have the following features: push(x) --...

    Design a stack class by importing the available java.util.Stack to have the following features: push(x) -- push element x onto stack, where x is anywhere between Integer.MIN_VALUE and Integer.MAX_VALUE. pop() -- remove the element on top of the stack. top() -- get the top element. getMax() -- retrieve the max element in the stack in constant time (i.e., O(1)). Your code should have the following shape and form, all in one .java file. Note the styling and documentation API already...

  • import java.util.ArrayList; import java.util.List; import java.util.Stack; public class Main { public static void main(String[] args) {...

    import java.util.ArrayList; import java.util.List; import java.util.Stack; public class Main { public static void main(String[] args) { int programCounter = 0; List<String> program = new ArrayList<String>(); Stack<String> stack = new Stack<String>(); // TODO string probably not best program.add("GOTO start<<1>>"); program.add("LABEL Read"); program.add("LINE -1"); program.add("FUNCTION Read -1 -1"); program.add("READ"); program.add("RETURN "); program.add("LABEL Write"); program.add("LINE -1"); program.add("FUNCTION Write -1 -1"); program.add("FORMAL dummyFormal 0"); program.add("LOAD 0 dummyFormal"); program.add("WRITE"); program.add("RETURN "); program.add("LABEL start<<1>>"); program.add("LINE 1"); program.add("FUNCTION main 1 4"); program.add("LIT 0 i"); program.add("LIT 0 j");...

  • Here's the problem that I have to solve: Write a Java program that uses a Stack...

    Here's the problem that I have to solve: Write a Java program that uses a Stack data structure to evaluate postfix expressions. Your program takes a postfix expression as an input,for example:3 42.3+ 5.25* ,from the user and calculates/display the result of the expression. What I'm having trouble is getting it to reading and calculating it as postfix Here's my code: import java.util.Scanner; public class Assignment2 {     public static void main(String[] args)     {         Scanner scan = new...

  • Hey Guys , I need help with this assignment: This component of the final exam will...

    Hey Guys , I need help with this assignment: This component of the final exam will have two data structures. The two data structures will be a stack and the other will be a hashmap. The hashmap object will be added to the stack every time the user picks an answer. The hashmap will have a key and value. The key will be the timestamp (system time when the user selected a choice) and the value will be the choice...

  • Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source...

    Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source code from the next-line brace style to the end-of-line brace style. The program is invoked from the command line with the input Java source code file as args [0] and the name of the file to save the formatted code in as args [1]. The original file is left untouched. The program makes no other changes the source code, including whitespace. For example, the...

  • Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an ...

    Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an array. Implement all methods in ArrayStack class using resizable array strategy, i.e. usedoubleArray() Must throw StackException during exception events in methods:    peek(), pop(), ArrayStack(int initialCapacity) Do not change or add data fields Do not add new methods */ import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private static...

  • Need Help ASAP!! Below is my code and i am getting error in (public interface stack)...

    Need Help ASAP!! Below is my code and i am getting error in (public interface stack) and in StackImplementation class. Please help me fix it. Please provide a solution so i can fix the error. thank you.... package mazeGame; import java.io.*; import java.util.*; public class mazeGame {    static String[][]maze;    public static void main(String[] args)    {    maze=new String[30][30];    maze=fillArray("mazefile.txt");    }    public static String[][]fillArray(String file)    {    maze = new String[30][30];       try{...

  • Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored...

    Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an array. Implement all methods in ArrayStack class using resizable array strategy, i.e. usedoubleArray() Must throw StackException during exception events in methods:    peek(), pop(), ArrayStack(int initialCapacity) Do not change or add data fields Do not add new methods */ import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private...

  • What is output 1 in the code below? import java.util."; public class StackDemo { public static...

    What is output 1 in the code below? import java.util."; public class StackDemo { public static void main(String [] args) { Stack<Integer> intStack = new Stack<>(); Stack<Integer> tempStack = new Stack<>(); intStack.push(18); intStack.push(21); intStack.push(25); tempStack = intStack; while (!tempStack.empty() { System.out.print(tempStack.peek()+"");//output 1 tempStack.pop(); 3 System.out.println(); System.out.println(intStack.peek()); //output 2 25 18 21 • 25 21 18

  • Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB....

    Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB. This interface should specify this abstract method: public abstract Product get(String productCode); Modify the ProductDB class so it implements the IProductDB interface. Write the code for the new ‘get’ method. Then remove the getProductByCode method. In the Main class, modify the code so it works with the new ProductDB class. This code should create an instance of the IProductDB interface like this: IProductDB db...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT