Question

In Java please. 2. Create a stack class MyStack2 that extends ArrayList. Write a test program...

In Java please.

2. Create a stack class MyStack2 that extends ArrayList. Write a test program that reads a number of strings from the user then displays them in reverse order.

3. Modify your code in (2) so that MyStack become a generic class (i.e. MyStack).

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

Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change.

If you are satisfied with the solution, please rate the answer. Let me know for any help with any other questions.

Thank You !
===========================================================================

import java.util.ArrayList;

public class MyStack2 extends ArrayList {

public void push(String word) {
this.add(word);
}

public String pop() {
if (size() != 0) {
String lastElement = (String) get(size() - 1);
remove(size() - 1);
return lastElement;
} else {
return null;
}
}

public boolean isEmpty() {
return size() == 0;
}

}

======================================================================
// TestProgram.java
public class TestProgram {

public static void main(String[] args) {

MyStack2 myStack2 = new MyStack2();

myStack2.push("Apple");
myStack2.push("Banana");
myStack2.push("Cherry");
myStack2.push("Daisy");

while (!myStack2.isEmpty()){
System.out.println(myStack2.pop());
}
}
}
======================================================================
// Part 3
import java.util.ArrayList;

public class MyStack2<T> extends ArrayList<T> {

public void push(T word) {
this.add(word);
}

public T pop() {
if (size() != 0) {
T lastElement = (T) get(size() - 1);
remove(size() - 1);
return lastElement;
} else {
return null;
}
}

public boolean isEmpty() {
return size() == 0;
}

}


// TestProgram for Part 3
public class TestProgram {

public static void main(String[] args) {

MyStack2<Integer> myStack2 = new MyStack2();

myStack2.push(10);
myStack2.push(20);
myStack2.push(30);
myStack2.push(40);
myStack2.push(50);

while (!myStack2.isEmpty()){
System.out.println(myStack2.pop());
}
}
}
===================================================================

Add a comment
Know the answer?
Add Answer to:
In Java please. 2. Create a stack class MyStack2 that extends ArrayList. Write a test program...
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
  • Using java Create a simple queue class and a simple stack class. The queue and stack...

    Using java Create a simple queue class and a simple stack class. The queue and stack should be implemented as a linked list. Create three functions that utilize these data structures Write a function that opens a text file and reads its contents into a stack of characters. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse...

  • JAVA QUESTION 2.One use of a Stack is to reverse the order of input. Write a...

    JAVA QUESTION 2.One use of a Stack is to reverse the order of input. Write a complete method that reads a series of Strings from the user. The user enters "end" to stop inputting words. Then, output the Strings in reverse order of how they were entered. Do not output the String “end”. Use a stack to accomplish this task. Invoke only the methods push, pop, peek, and isEmpty on the stack object. Here is an example of how the...

  • Writing in Java ,using Arraylist and collection please. 3. Write a simple phonebook program that reads...

    Writing in Java ,using Arraylist and collection please. 3. Write a simple phonebook program that reads in a series of name-number pairs from the user (that is, name and number on one line separated by whitespace) and stores them in a Map from Strings to Integers. Then ask the user for a name and return the matching number, or tell the user that the name wasn't found.

  • Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(),...

    Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(), peek(), and isEmpy() methods. Create a new Java Application that has the following methods. Write a method reverseChar() to print a sentence in reverse order. Use a Stack to reverse each character. Example: if the user enters a sentence “ABC DEFG”, the program will display “GFED CBA” Write a method reverseWord() to print a sentence reverse order. Use a Stack to reverse each word....

  • Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(),...

    Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(), peek(), and isEmpy() methods. Create a new Java Application that has the following methods. Write a method reverseChar() to print a sentence in reverse order. Use a Stack to reverse each character. Example: if the user enters a sentence “ABC DEFG”, the program will display “GFED CBA” Write a method reverseWord() to print a sentence reverse order. Use a Stack to reverse each word....

  • Write a program that uses a stack to reverse its inputs. Your stack must be generic...

    Write a program that uses a stack to reverse its inputs. Your stack must be generic and you must demonstrate that it accepts both String and Integer types. Your stack must implement the following methods: push, pop, isEmpty (returns true if the stack is empty and false otherwise), and size (returns an integer value for the number of items in the stack). You may use either an ArrayList or a LinkedList to implement your stack. Also, your pop method must...

  • 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...

  • Write a Java program that will create an array of Strings with 25 elements. Input Strings...

    Write a Java program that will create an array of Strings with 25 elements. Input Strings from the user and store them in the array until either the user enters “quit” or the array is full. HINT: the sentinel value is “quit” all lowercase. “Quit” or “QUIT” should not stop the loop. HINT: you have to use the equals method (not ==) when you are comparing two Strings. After the input is complete, print the Strings that the user entered...

  • Create a new Java Application that test MyStack by having the following methods in main class:...

    Create a new Java Application that test MyStack by having the following methods in main class: 1. A method to generate a number of element between two given values and save them in a stack 2. A method to print a stack (10 elements per line). The original stack should remain as is after the print 3. A method to exchange the first element and the last element in a stack 4. A Boolean method to search for a value...

  • Please Do It With Java. Make it copy paste friendly so i can run and test...

    Please Do It With Java. Make it copy paste friendly so i can run and test it. Thnak You. Write programs for challenges 1 and 2 and 6 at the end of the chapter on Generics. 1) Write a generic class named MyList, with a type parameter T. The type parameter T should be constrained to an upper bound: the Number class. The class should have as a field an ArrayList of T. Write a public method named add, which...

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