Q:

(Implement MyStack using inheritance) In Listing 11.10, MyStack is implemented using composition. Define a new stack class that extends ArrayList

0

(Implement MyStack using inheritance) In Listing 11.10, MyStack is implemented using composition. Define a new stack class that extends ArrayList. Draw the UML diagram for the classes and then implement MyStack. Write a test program that prompts the user to enter five strings and displays them in reverse order.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

/*********************************************************************************
* (Implement MyStack using inheritance) In Listing 11.10, MyStack is implemented *
* using composition. Define a new stack class that extends ArrayList.            *
* Draw the UML diagram for the classes and then implement MyStack. Write a test  *
* program that prompts the user to enter five strings and displays them in       *
* reverse order.                                                                 *
*********************************************************************************/
import java.util.Scanner;

public class Exercise_11_10 {
	/** Main method */
	public static void main(String[] args) {
		// Create a Scanner
		Scanner input = new Scanner(System.in);

		// Create a MyStack
		MyStack stack = new MyStack();

		// Prompt the user to enter five strings
		System.out.print("Enter five strings: ");
		for (int i = 0; i < 5; i++) {
			stack.push(input.next());	
		}

		// Display in reverse order
		System.out.println("Stack: " + stack.toString());
	}
}

MyStack.java

/*****************************
*           MyStack          *
*----------------------------*
* -list: ArrayList<Object>   *
* +MyStack()                 *
* +peek(): Object            *
* +pop(): Object             *
* +push(o: Object): void     *
*****************************/
// Implement MyStack class
public class MyStack 
		extends java.util.ArrayList {
	// Data fields
	private java.util.ArrayList<Object> list;

	// Constructor
	/** Construct a default MyStack object */
	public MyStack() {
		list = new java.util.ArrayList<Object>();
	}

	/** Add a new element to 
	  * the top of this stack */
	public void push(Object o) {
		list.add(0, o);
	}

	/** Return the top element in this  
	  * stack without removing it */
	public Object peek() {
		return list.get(0);
	}

	/** Return an remove the 
	  * top element in this stack */
	public Object pop() {
		Object o = list.get(0);
		list.remove(0);
		return o;
	}

	/** Return a string of all elements in MyStack */
	@Override
	public String toString() {
		return "stack: " + list.toString();
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now