Recursion hell

This commit is contained in:
2023-09-20 14:39:20 -05:00
parent c375732331
commit 2f7fa46bdf
16 changed files with 337 additions and 37 deletions

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.slcc.asdv.caleb</groupId>
<artifactId>GenericStack</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<exec.mainClass>edu.slcc.asdv.caleb.genericstack.GenericStack</exec.mainClass>
</properties>
</project>

View File

@@ -0,0 +1,63 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package edu.slcc.asdv.caleb.genericstack;
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];
}
@Override
public String toString()
{
String returnString = "";
for (int i = top -1; i >= 0; --i) {
returnString += elements[i].toString() + ", ";
}
return returnString;
}
}

View File

@@ -0,0 +1,23 @@
/*
* 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.genericstack;
/**
*
* @author caleb
*/
public class Max {
public static <E extends Comparable> E max(E e1, E e2) {
return e1.compareTo(e2) >= 0 ? e1 : e2;
}
public static void main(String[] args)
{
Object o1 = new Object();
Object o2 = new Object();
System.out.println(Max.max(o1, o2));
System.out.println(Max.max(2, 4));
System.out.println(Max.max("abc", "defg"));
}
}

View File

@@ -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.genericstack;
/**
*
* @author caleb
*/
public class TestStack {
public static void main(String[] args)
{
GenericStack<Integer> gsi = new GenericStack(4);
gsi.push(10);
gsi.push(20);
// gsi.push(30);
//gsi.push(40);
//gsi.pop();
//gsi.push(50);
System.out.println(gsi.toString());
}
}