For this assignment you are given the following Java source code files: IStack.java (This file is complete – make no changes to this file) MyStack.java (You must complete this file) Main.java (You may use this file to write code to test your MyStack)

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter11: Advanced Inheritance Concepts
Section: Chapter Questions
Problem 14RQ
icon
Related questions
Question
100%

Problem Description and Given Info

For this assignment you are given the following Java source code files:

  • IStack.java (This file is complete – make no changes to this file)
  • MyStack.java (You must complete this file)
  • Main.java (You may use this file to write code to test your MyStack)

You must complete the public class named MyStack.java with fields and methods as defined below. Your MyStack.java will implement the IStack interface that is provided in the IStack.java file. You must implement your MyStack class as either a linked list or an array list (refer to your MyArrayList and MyLinkedList work). Your MyStack must not be arbitrarily limited to any fixed size at run-time.

UML


UML CLass Diagram: MyStack

Structure of the Fields

While there are no required fields for your MyStack class, you will need to decide what fields to implement. This decision will be largely based on your choice to implement this MyStack as either an array list or a linked list.

Structure of the Methods

As described by the UML Class Diagram above, your MyStack class must implement the following methods:

  • a public method named push that takes an Object argument and returns nothing
  • a public method named pop that takes no arguments and returns an Object
  • a public method named peek that takes no arguments and returns an Object
  • a public method named indexOf that takes an Object argument and returns an int
  • a public method named getSize that takes no arguments and returns an int
  • a public method named isEmpty that takes no arguments and returns an boolean

Note that:

  • these methods are declared in the IStack interface. You will be implementing these methods in this MyStack concrete class.

Additional Information

MyStack

  1. This concrete class will store its elements in either an internal array list or linked list. All such implementation details must be contained in your myStack.java file. You may add any additional fields, methods, and inner classes that you will need to achieve this.
  2. push method
    • Add a new item to top of the stack. For example: given the stack {1, 2, 3} (where the value 1 is on the top) and an instruction to push(99), the result would be this {99, 1, 2, 3}, with the value 99 now on the top of the stack.
  3. pop method
    • Remove and return the item currently on the top of the stack. For example: given the stack {1, 2, 3} (where the value 1 is on the top) and an instruction to pop(), the stack would now look like this {2, 3}, and the value 1 would be returned.
    • Throws a NoSuchElementException if the stack is currently empty when this method is called.
  4. peek method
    • Return (but do not remove) the item currently on the top of the stack. For example: given the stack {1, 2, 3} (where the value 1 is on the top) and an instruction to peek(), the stack would still look like this {1, 2, 3}, and the value 1 would be returned.
    • Throws a NoSuchElementException if the stack is currently empty when this method is called.
  5. indexOf method
    • Return the (zero-based) number of elements from the front or top of the collection where the specified item is first found. Returns -1 if the item is not found in the collection. For example: given the stack {1, 2, 3} (where the value 1 is on the top) and the instruction indexOf(2), the value 1 would be returned (because the value 2 was found at index 1 (1 element below the top) in the stack. For another example: given the stack {1, 2, 3} (where the value 1 is on the top) and the instruction indexOf(99), the value -1 would be returned (because the value 99 is not found in the stack).
  6. getSize method
    • Returns the number of elements currently stored in the stack.
  7. isEmpty method
    • Returns true if there are currently no items stored in this stack, otherwise returns false.

 

UML
IStack <<interface>>
+ push(item : Object) : void
+ pop(): Object
+ peek(): Object
+ indexOf(item : Object): int
+ getSize(): int
+ isEmpty(): boolean
implements
MyStack
UML CLass Diagram: MyStack
Transcribed Image Text:UML IStack <<interface>> + push(item : Object) : void + pop(): Object + peek(): Object + indexOf(item : Object): int + getSize(): int + isEmpty(): boolean implements MyStack UML CLass Diagram: MyStack
1
Current file: MyStack.java
MyStack.java
IStack.java
Main.java
Load default template...
Transcribed Image Text:1 Current file: MyStack.java MyStack.java IStack.java Main.java Load default template...
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Unreferenced Objects
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,