2023-09-22 16:25:19 -05:00

94 lines
4.3 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>GenericStack.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.literal {color: #cc7832}
.number {color: #6897bb}
.string {color: #6a8759}
.comment {color: #808080}
.whitespace {color: #505050}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/lab6_generics_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/lab6_generics_calebfontenot/GenericStack.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license</span>
<span class="comment"> */</span>
<span class="literal">package</span> edu.slcc.asdv.caleb.lab6_generics_calebfontenot;
<span class="literal">import</span> java.util.ArrayList;
<span class="literal">import</span> java.util.EmptyStackException;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> GenericStack&lt;T&gt; {
<span class="literal">private</span> T[] elements;
ArrayList&lt;T&gt; elementsList = <span class="literal">new</span> ArrayList&lt;T&gt;();
<span class="literal">private</span> <span class="literal">int</span> top;
<span class="literal">static</span> <span class="literal">int</span> size = <span class="number">4</span>;
<span class="literal">public</span> GenericStack(<span class="literal">int</span> size) {
elements = (T[]) <span class="literal">new</span> Object[size];
}
<span class="literal">public</span> GenericStack()
{
elements = (T[]) <span class="literal">new</span> Object[size];
}
<span class="literal">public</span> <span class="literal">boolean</span> push (T element) {
<span class="literal">if</span> (top == size) {
<span class="literal">throw</span> <span class="literal">new</span> StackOverflowError();
}
elements[top++] = element;
elementsList.add(element);
<span class="literal">return</span> <span class="literal">true</span>;
}
<span class="literal">public</span> T pop() {
<span class="literal">if</span> (top == <span class="number">0</span>) {
<span class="literal">throw</span> <span class="literal">new</span> EmptyStackException();
}
--top;
elementsList.remove(top);
<span class="literal">return</span> elements[top];
}
<span class="literal">public</span> T peek() {
<span class="literal">if</span> (top == <span class="number">0</span>) {
<span class="literal">throw</span> <span class="literal">new</span> EmptyStackException();
}
<span class="literal">return</span> elements[top - <span class="number">1</span>];
}
<span class="literal">public</span> <span class="literal">boolean</span> isEmpty() {
<span class="literal">boolean</span> returnBoolean = <span class="literal">false</span>;
<span class="literal">if</span> (top == <span class="number">0</span>) {
<span class="literal">return</span> <span class="literal">true</span>;
}
<span class="literal">return</span> returnBoolean;
}
@Override
<span class="literal">public</span> String toString()
{
String returnString = <span class="string">&quot;&quot;</span>;
<span class="literal">for</span> (<span class="literal">int</span> i = top -<span class="number">1</span>; i &gt;= <span class="number">0</span>; --i) {
returnString += elements[i].toString() + <span class="string">&quot;</span><span class="string">, </span><span class="string">&quot;</span>;
}
<span class="literal">return</span> returnString;
}
}
</pre></body>
</html>