92 lines
5.2 KiB
HTML
92 lines
5.2 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
<html>
|
|
<head>
|
|
<title>MyStack.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}
|
|
.ST0 {color: #287bde}
|
|
.number {color: #6897bb}
|
|
.string {color: #6a8759}
|
|
.ST1 {color: #9876aa}
|
|
.ST2 {color: #ffc66d}
|
|
.comment {color: #808080}
|
|
.whitespace {color: #505050}
|
|
.ST3 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
|
|
.ST4 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
|
|
-->
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/MyStack.java</td></tr></table>
|
|
<pre>
|
|
<span class="comment">/*</span>
|
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
|
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
|
|
<span class="comment"> */</span>
|
|
<span class="literal">package</span> com.calebfontenot.mp5_calebfontenot;
|
|
|
|
<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">import</span> java.util.ArrayList;
|
|
|
|
<span class="literal">public</span> <span class="literal">class</span> MyStack <span class="literal">implements</span> Cloneable {
|
|
<span class="literal">private</span> ArrayList<Object> <span class="ST1">list</span> = <span class="literal">new</span> ArrayList<>();
|
|
|
|
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST2">isEmpty</span>() {
|
|
<span class="literal">return</span> <span class="ST1">list</span>.isEmpty();
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">int</span> <span class="ST2">getSize</span>() {
|
|
<span class="literal">return</span> <span class="ST1">list</span>.size();
|
|
}
|
|
|
|
<span class="literal">public</span> Object <span class="ST2">peek</span>() {
|
|
<span class="literal">return</span> <span class="ST1">list</span>.get(getSize() - <span class="number">1</span>);
|
|
}
|
|
|
|
<span class="literal">public</span> Object <span class="ST2">pop</span>() {
|
|
Object o = <span class="ST1">list</span>.get(getSize() - <span class="number">1</span>);
|
|
<span class="ST1">list</span>.remove(getSize() - <span class="number">1</span>);
|
|
<span class="literal">return</span> o;
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">push</span>(Object o) {
|
|
<span class="ST1">list</span>.add(o);
|
|
}
|
|
|
|
@Override <span class="comment">/**</span> <span class="comment">Override</span> <span class="comment">the</span> <span class="comment">toString</span> <span class="comment">in</span> <span class="comment">the</span> <span class="comment">Object</span> <span class="comment">class</span> <span class="comment">*/</span>
|
|
<span class="literal">public</span> String <span class="ST2">toString</span>() {
|
|
<span class="literal">return</span> <span class="string">"</span><span class="string">stack: </span><span class="string">"</span> + <span class="ST1">list</span>.toString();
|
|
}
|
|
|
|
@Override
|
|
<span class="literal">public</span> Object <span class="ST2">clone</span>() <span class="literal">throws</span> CloneNotSupportedException {
|
|
MyStack clonedStack = (MyStack) <span class="literal">super</span>.clone();
|
|
ArrayList<Object> newList = <span class="literal">new</span> ArrayList<>(<span class="ST1">l</span><span class="ST1">ist</span>);
|
|
clonedStack.<span class="ST1">list</span> = newList;
|
|
<span class="literal">return</span> clonedStack;
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST3">main</span>(String[] args) <span class="literal">throws</span> CloneNotSupportedException {
|
|
MyStack stack = <span class="literal">new</span> MyStack();
|
|
<span class="literal">final</span> <span class="literal">int</span> STACK_SIZE = <span class="number">50</span>;
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < STACK_SIZE; ++i) {
|
|
stack.push(i + <span class="number">1</span>); <span class="comment">// Fill Stack with sequential numbers.</span>
|
|
<span class="comment">//System.out.println(stack.peek());</span>
|
|
}
|
|
System.<span class="ST4">out</span>.println(stack.peek());
|
|
MyStack clonedStack = (MyStack) stack.clone();
|
|
System.<span class="ST4">out</span>.println(clonedStack.peek());
|
|
}
|
|
}
|
|
|
|
</pre></body>
|
|
</html>
|