1 // you may use this file to write and run code to test your code 2 3 public class Main ( 6 7) 7 8 9 1 import java.util.NoSuch ElementException; 3 public class MyStack implements IStack ( 4 // add any necessary variables here 5 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 public static void main(String[] args) { } 35 36 37 ) @Override public void push(Object item) ( } Current file: Main.java @Override public Object pop() { } @Override public Object peek() ( } Current file: MyStack.java @Override public int size() ( } @Override public int indexOf(Object item) { } @Override public boolean isEmpty() { } // add any necessary methods or classes below File is marked as read only 2 A stack is a last in first out (LIFO) data structure. 3 New items are pushed onto the top of the list. 4 Items are popped from the top of the list. 5%/ 6 public interface IStack ( 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43) 44 Pushes an item onto the top of this stack. */ public void push(Object item); Removes the object at the top of this stack and returns that object as the value of this function. @return the item at the top of the stack or throws a NoSuchElementException is the stack is empty "/ public Object pop(); Current file: IStack.java Looks at the object at the top of this stack without removing it from the stack. @return the item at the top of the stack or throws a NoSuchElementException is the stack is empty public Object peek(); searches the stack for an item. @return the zero-based index of the item in the stack; returns -1 if the item is not in the stack. "/ public int indexOf(object item); A count of the number of items in the stack. @return A count of the number of items in the stack. "/ public int size(); Tests if this stack is empty. @return true if the stack is empty "/ public boolean isEmpty();

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

Hi, I am a bit lost on how to do this coding assignment. It would be great if someone could help me fill in the MyStack.java file with the given information. All the information is in the pictures. Thank you!

1 // you may use this file to write and run code to test your code
2
3 public class Main {
4
5
6
7 }
7
8
9
10
11
12
13
14
15
16
1 import java.util. NoSuch Element Exception;
2
3 public class MyStack implements IStack {
4 // add any necessary variables here.
5
6
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public static void main(String[] args) {
}
35
36
37 }
38
@Override
public void push(Object item) {
}
@Override
public Object pop() {
}
@Override
public Object peek() {
}
Current file: Main.java ▾
}
@Override
public int indexOf(Object item) {
@Override
public int size() {
}
Current file: MyStack.java
@Override
public boolean isEmpty() {
}
// add any necessary methods or classes below
File is marked as read only
1/**
2 A stack is a last in first out (LIFO) data structure.
3 New items are pushed onto the top of the list.
4 Items are popped from the top of the list.
5 */
6 public interface IStack {
7
/**
8
9
10
11
12
13
14
15
16
17
7 18 19 28 21 22 23 24 25 26 27 28 29 28 2132334353678 39 40 41 42 3 44
20
30
43 }
Pushes an item onto the top of this stack.
*/
public void push(Object item);
/**
Removes the object at the top of this stack and returns that object as the value of this function.
@return the item at the top of the stack or throws a NoSuch Element Exception is the stack is empty
public Object pop();
*/
/**
Looks at the object at the top of this stack without removing it from the stack.
@return the item at the top of the stack or throws a NoSuchElementException is the stack is empty
public Object peek();
*/
/**
searches the stack for an item.
@return the zero-based index of the item in the stack; returns -1 if the item is not in the stack.
public int indexOf(Object item);
*/
/**
Current file: IStack.java
*/
public int size();
A count of the number of items in the stack.
@return A count of the number of items in the stack.
/**
*/
Tests if this stack is empty.
@return true if the stack is empty
public boolean isEmpty();
Transcribed Image Text:1 // you may use this file to write and run code to test your code 2 3 public class Main { 4 5 6 7 } 7 8 9 10 11 12 13 14 15 16 1 import java.util. NoSuch Element Exception; 2 3 public class MyStack implements IStack { 4 // add any necessary variables here. 5 6 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 public static void main(String[] args) { } 35 36 37 } 38 @Override public void push(Object item) { } @Override public Object pop() { } @Override public Object peek() { } Current file: Main.java ▾ } @Override public int indexOf(Object item) { @Override public int size() { } Current file: MyStack.java @Override public boolean isEmpty() { } // add any necessary methods or classes below File is marked as read only 1/** 2 A stack is a last in first out (LIFO) data structure. 3 New items are pushed onto the top of the list. 4 Items are popped from the top of the list. 5 */ 6 public interface IStack { 7 /** 8 9 10 11 12 13 14 15 16 17 7 18 19 28 21 22 23 24 25 26 27 28 29 28 2132334353678 39 40 41 42 3 44 20 30 43 } Pushes an item onto the top of this stack. */ public void push(Object item); /** Removes the object at the top of this stack and returns that object as the value of this function. @return the item at the top of the stack or throws a NoSuch Element Exception is the stack is empty public Object pop(); */ /** Looks at the object at the top of this stack without removing it from the stack. @return the item at the top of the stack or throws a NoSuchElementException is the stack is empty public Object peek(); */ /** searches the stack for an item. @return the zero-based index of the item in the stack; returns -1 if the item is not in the stack. public int indexOf(Object item); */ /** Current file: IStack.java */ public int size(); A count of the number of items in the stack. @return A count of the number of items in the stack. /** */ Tests if this stack is empty. @return true if the stack is empty public boolean isEmpty();
Required Skills Inventory
• Write concrete classes that implement Java Interfaces according to specifications given in UML.
Implement the functionality of a Stack data structure.
You are not allowed to use any of the standard Java collection types (like ArrayList) for this assignment. Do not import any Java
standard library features from java.util, except java.util.NoSuchElementException. You may use simple arrays. You may also use
any code from your MyArrayList or MyLinkedList submissions.
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
IStack <<interface>>
+ push(item : Object) : void
+ pop(): Object
+ peek(): Object
+ indexOf(item : Object): int
+ size(): int
+ isEmpty(): boolean
implements
MyStack
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 size that takes no arguments and returns an int
• a public method named isEmpty that takes no arguments and returns an boolean
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 size 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
o 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
o 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.
o Throws a NoSuch ElementException 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.
o Throws a NoSuch ElementException if the stack is currently empty when this method is called.
5. indexOf method
o 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. size method
o Returns the number of elements currently stored in the stack.
7. isEmpty method
o Returns true if there are currently no items stored in this stack, otherwise returns false.
Need Help?
Additional help resources are available by clicking on the words "Need Help?" at the bottom of this page, and search for help or ask a
question!
Copyright 2021 Arizona State University - This content is protected and may not be shared, uploaded, sold, or distributed in whole or part. Copying any part of these instructions or any part of
a solution and sharing online or otherwise in any form is a violation of copyright laws and the ASU Academic Integrity Policy. All violations will be prosecuted.
460134.2737500.qx3zqy7
LAB
19.1.1: MyStack (Individual Assignment)
0/100
Transcribed Image Text:Required Skills Inventory • Write concrete classes that implement Java Interfaces according to specifications given in UML. Implement the functionality of a Stack data structure. You are not allowed to use any of the standard Java collection types (like ArrayList) for this assignment. Do not import any Java standard library features from java.util, except java.util.NoSuchElementException. You may use simple arrays. You may also use any code from your MyArrayList or MyLinkedList submissions. 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 IStack <<interface>> + push(item : Object) : void + pop(): Object + peek(): Object + indexOf(item : Object): int + size(): int + isEmpty(): boolean implements MyStack 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 size that takes no arguments and returns an int • a public method named isEmpty that takes no arguments and returns an boolean 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 size 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 o 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 o 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. o Throws a NoSuch ElementException 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. o Throws a NoSuch ElementException if the stack is currently empty when this method is called. 5. indexOf method o 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. size method o Returns the number of elements currently stored in the stack. 7. isEmpty method o Returns true if there are currently no items stored in this stack, otherwise returns false. Need Help? Additional help resources are available by clicking on the words "Need Help?" at the bottom of this page, and search for help or ask a question! Copyright 2021 Arizona State University - This content is protected and may not be shared, uploaded, sold, or distributed in whole or part. Copying any part of these instructions or any part of a solution and sharing online or otherwise in any form is a violation of copyright laws and the ASU Academic Integrity Policy. All violations will be prosecuted. 460134.2737500.qx3zqy7 LAB 19.1.1: MyStack (Individual Assignment) 0/100
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 6 images

Blurred answer
Knowledge Booster
File Input and Output Operations
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.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education