Reset author name to chosen name ✨
This commit is contained in:
@@ -1,69 +0,0 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
*/
|
||||
package edu.slcc.asdv.chloe.lab6_generics_chloefontenot;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.EmptyStackException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
*/
|
||||
|
||||
package edu.slcc.asdv.chloe.lab6_generics_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
public class Lab6_generics_ChloeFontenot {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello World!");
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
/*
|
||||
* 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.chloe.lab6_generics_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
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()) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* 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.chloe.lab6_generics_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
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));
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* 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.chloe.lab6_generics_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
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));
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
/*
|
||||
* 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.chloe.lab6_generics_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
/*
|
||||
* 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.chloe.lab6_generics_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
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