BigIntegerFib lol
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
<!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<T> {
|
||||
|
||||
<span class="literal">private</span> T[] elements;
|
||||
ArrayList<T> elementsList = <span class="literal">new</span> ArrayList<T>();
|
||||
<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">""</span>;
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = top -<span class="number">1</span>; i >= <span class="number">0</span>; --i) {
|
||||
returnString += elements[i].toString() + <span class="string">"</span><span class="string">, </span><span class="string">"</span>;
|
||||
}
|
||||
<span class="literal">return</span> returnString;
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@@ -0,0 +1,70 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>Max.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}
|
||||
.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/Max.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"> * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template</span>
|
||||
<span class="comment"> */</span>
|
||||
<span class="literal">package</span> edu.slcc.asdv.caleb.lab6_generics_calebfontenot;
|
||||
|
||||
<span class="comment">/**</span>
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class Max {
|
||||
|
||||
public static Comparable max(Comparable o1, Comparable o2)
|
||||
{
|
||||
if (o1.compareTo(o2) > 0) {
|
||||
return o1;
|
||||
} else {
|
||||
return o2;
|
||||
}
|
||||
}
|
||||
public static <E extends Comparable<E>> E maxSafe(E e1, E e2) {
|
||||
if(e1.compareTo(e2) > 0) {
|
||||
return e1;
|
||||
} else {
|
||||
return e2;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.out.println(max(1, 2));
|
||||
try {
|
||||
System.out.println(maxSafe(1, 2));
|
||||
System.out.println(maxSafe("abc", "ABC"));
|
||||
System.out.println();
|
||||
//System.out.println(maxSafe(1, "two"));
|
||||
|
||||
GenericStack stackUnsafe = new GenericStack();
|
||||
GenericStack<Integer> stackSafe = new GenericStack();
|
||||
stackSafe.push(1); stackSafe.push(2);
|
||||
System.out.println(stackSafe);
|
||||
stackUnsafe.push(1); stackUnsafe.push("two");
|
||||
System.out.println("This line compiles but crashes the program " + max(1, "two"));
|
||||
} catch (ClassCastException e) {
|
||||
System.err.println("RAW TYPES ARE UNSAFE " + e.getMessage()) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@@ -0,0 +1,56 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>NoWildCard.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/NoWildCard.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"> * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template</span>
|
||||
<span class="comment"> */</span>
|
||||
<span class="literal">package</span> edu.slcc.asdv.caleb.lab6_generics_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">public</span> <span class="literal">class</span> NoWildCard {
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">double</span> max(GenericStack<Integer> stack) {
|
||||
<span class="literal">double</span> max = Double.MIN_VALUE;
|
||||
|
||||
<span class="literal">while</span> (!stack.isEmpty()) {
|
||||
<span class="literal">double</span> value = stack.pop().doubleValue();
|
||||
<span class="literal">if</span> (value > max) {
|
||||
max = value;
|
||||
}
|
||||
}
|
||||
<span class="literal">return</span> max;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> main(String[] args) {
|
||||
GenericStack<Integer> intStack = <span class="literal">new</span> GenericStack<>();
|
||||
intStack.push(<span class="number">1</span>);
|
||||
intStack.push(<span class="number">2</span>);
|
||||
intStack.push(-<span class="number">2</span>);
|
||||
System.out.print(<span class="string">"</span><span class="string">The max number is </span><span class="string">"</span> + max(intStack));
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@@ -0,0 +1,60 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>WildCard.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}
|
||||
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
|
||||
.ST0 {color: #287bde}
|
||||
.number {color: #6897bb}
|
||||
.string {color: #6a8759}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
.ST1 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
|
||||
.ST2 {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 3/Assignments/lab6_generics_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/lab6_generics_calebfontenot/WildCard.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> edu.slcc.asdv.caleb.lab6_generics_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">public</span> <span class="literal">class</span> WildCard {
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">double</span> <span class="ST1">max</span>(GenericStack<? <span class="literal">extends</span> Number> stack) {
|
||||
<span class="literal">double</span> max = Double.<span class="ST2">MIN_VALUE</span>;
|
||||
|
||||
<span class="literal">while</span> (!stack.isEmpty()) {
|
||||
<span class="literal">double</span> value = stack.pop().doubleValue();
|
||||
<span class="literal">if</span> (value > max) {
|
||||
max = value;
|
||||
}
|
||||
}
|
||||
<span class="literal">return</span> max;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">main</span>(String[] args) {
|
||||
GenericStack<Integer> intStack = <span class="literal">new</span> GenericStack<>();
|
||||
intStack.push(<span class="number">1</span>);
|
||||
intStack.push(<span class="number">2</span>);
|
||||
intStack.push(-<span class="number">2</span>);
|
||||
System.<span class="ST2">out</span>.print(<span class="string">"</span><span class="string">The max number is </span><span class="string">"</span> + <span class="ST3">max</span>(intStack));
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@@ -0,0 +1,49 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>WildCard2.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}
|
||||
.string {color: #6a8759}
|
||||
.number {color: #6897bb}
|
||||
.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/WildCard2.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"> * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template</span>
|
||||
<span class="comment"> */</span>
|
||||
<span class="literal">package</span> edu.slcc.asdv.caleb.lab6_generics_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">public</span> <span class="literal">class</span> WildCard2 {
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> print(GenericStack<?> stack) {
|
||||
<span class="literal">while</span> (!stack.isEmpty()) {
|
||||
System.out.print(stack.pop() + <span class="string">"</span> <span class="string">"</span>);
|
||||
}
|
||||
}
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> main(String[] args) {
|
||||
GenericStack<Integer> intStack = <span class="literal">new</span> GenericStack<>();
|
||||
intStack.push(<span class="number">1</span>);
|
||||
intStack.push(<span class="number">2</span>);
|
||||
intStack.push(-<span class="number">2</span>);
|
||||
|
||||
print(intStack);
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@@ -0,0 +1,52 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>WildCardWithSuper.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}
|
||||
.string {color: #6a8759}
|
||||
.number {color: #6897bb}
|
||||
.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/WildCardWithSuper.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"> * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template</span>
|
||||
<span class="comment"> */</span>
|
||||
<span class="literal">package</span> edu.slcc.asdv.caleb.lab6_generics_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">public</span> <span class="literal">class</span> WildCardWithSuper {
|
||||
<span class="literal">public</span> <span class="literal">static</span> <T> <span class="literal">void</span> add(GenericStack<T> stack1, GenericStack<? <span class="literal">super</span> T> stack2) {
|
||||
<span class="literal">while</span>(!stack1.isEmpty()) {
|
||||
stack2.push(stack1.pop());
|
||||
}
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> main(String[] args) {
|
||||
GenericStack<String> stack1 = <span class="literal">new</span> GenericStack<>();
|
||||
GenericStack<Object> stack2 = <span class="literal">new</span> GenericStack<>();
|
||||
stack2.push(<span class="string">"</span><span class="string">one</span><span class="string">"</span>);
|
||||
stack2.push(<span class="number">2</span>);
|
||||
stack1.push(<span class="string">"</span><span class="string">one</span><span class="string">"</span>);
|
||||
|
||||
add(stack1, stack2);
|
||||
WildCard2.print(stack2);
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
*/
|
||||
package edu.slcc.asdv.caleb.lab6_generics_calebfontenot;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.EmptyStackException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class GenericStack<T> {
|
||||
|
||||
private T[] elements;
|
||||
ArrayList<T> elementsList = new ArrayList<T>();
|
||||
private int top;
|
||||
static int size = 4;
|
||||
|
||||
public GenericStack(int size) {
|
||||
elements = (T[]) new Object[size];
|
||||
}
|
||||
public GenericStack()
|
||||
{
|
||||
elements = (T[]) new Object[size];
|
||||
}
|
||||
public boolean push (T element) {
|
||||
if (top == size) {
|
||||
throw new StackOverflowError();
|
||||
}
|
||||
elements[top++] = element;
|
||||
elementsList.add(element);
|
||||
return true;
|
||||
}
|
||||
|
||||
public T pop() {
|
||||
if (top == 0) {
|
||||
throw new EmptyStackException();
|
||||
}
|
||||
--top;
|
||||
elementsList.remove(top);
|
||||
return elements[top];
|
||||
}
|
||||
|
||||
public T peek() {
|
||||
if (top == 0) {
|
||||
throw new EmptyStackException();
|
||||
}
|
||||
return elements[top - 1];
|
||||
}
|
||||
public boolean isEmpty() {
|
||||
boolean returnBoolean = false;
|
||||
if (top == 0) {
|
||||
return true;
|
||||
}
|
||||
return returnBoolean;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
String returnString = "";
|
||||
for (int i = top -1; i >= 0; --i) {
|
||||
returnString += elements[i].toString() + ", ";
|
||||
}
|
||||
return returnString;
|
||||
}
|
||||
}
|
@@ -34,6 +34,12 @@ public class Max {
|
||||
System.out.println(maxSafe("abc", "ABC"));
|
||||
System.out.println();
|
||||
//System.out.println(maxSafe(1, "two"));
|
||||
|
||||
GenericStack stackUnsafe = new GenericStack();
|
||||
GenericStack<Integer> stackSafe = new GenericStack();
|
||||
stackSafe.push(1); stackSafe.push(2);
|
||||
System.out.println(stackSafe);
|
||||
stackUnsafe.push(1); stackUnsafe.push("two");
|
||||
System.out.println("This line compiles but crashes the program " + max(1, "two"));
|
||||
} catch (ClassCastException e) {
|
||||
System.err.println("RAW TYPES ARE UNSAFE " + e.getMessage()) ;
|
||||
|
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package edu.slcc.asdv.caleb.lab6_generics_calebfontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class NoWildCard {
|
||||
|
||||
public static double max(GenericStack<Integer> stack) {
|
||||
double max = Double.MIN_VALUE;
|
||||
|
||||
while (!stack.isEmpty()) {
|
||||
double value = stack.pop().doubleValue();
|
||||
if (value > max) {
|
||||
max = value;
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
GenericStack<Integer> intStack = new GenericStack<>();
|
||||
intStack.push(1);
|
||||
intStack.push(2);
|
||||
intStack.push(-2);
|
||||
System.out.print("The max number is " + max(intStack));
|
||||
}
|
||||
}
|
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package edu.slcc.asdv.caleb.lab6_generics_calebfontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class WildCard {
|
||||
|
||||
public static double max(GenericStack<? extends Number> stack) {
|
||||
double max = Double.MIN_VALUE;
|
||||
|
||||
while (!stack.isEmpty()) {
|
||||
double value = stack.pop().doubleValue();
|
||||
if (value > max) {
|
||||
max = value;
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
GenericStack<Integer> intStack = new GenericStack<>();
|
||||
intStack.push(1);
|
||||
intStack.push(2);
|
||||
intStack.push(-2);
|
||||
System.out.print("The max number is " + max(intStack));
|
||||
}
|
||||
}
|
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package edu.slcc.asdv.caleb.lab6_generics_calebfontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class WildCard2 {
|
||||
public static void print(GenericStack<?> stack) {
|
||||
while (!stack.isEmpty()) {
|
||||
System.out.print(stack.pop() + " ");
|
||||
}
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
GenericStack<Integer> intStack = new GenericStack<>();
|
||||
intStack.push(1);
|
||||
intStack.push(2);
|
||||
intStack.push(-2);
|
||||
|
||||
print(intStack);
|
||||
}
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package edu.slcc.asdv.caleb.lab6_generics_calebfontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class WildCardWithSuper {
|
||||
public static <T> void add(GenericStack<T> stack1, GenericStack<? super T> stack2) {
|
||||
while(!stack1.isEmpty()) {
|
||||
stack2.push(stack1.pop());
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
GenericStack<String> stack1 = new GenericStack<>();
|
||||
GenericStack<Object> stack2 = new GenericStack<>();
|
||||
stack2.push("one");
|
||||
stack2.push(2);
|
||||
stack1.push("one");
|
||||
|
||||
add(stack1, stack2);
|
||||
WildCard2.print(stack2);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user