Skip to content

Instantly share code, notes, and snippets.

View rshepherd's full-sized avatar
💭
Building something…

Randy Shepherd rshepherd

💭
Building something…
View GitHub Profile
import java.util.ArrayList;
import java.util.List;
public class ExpressionParser {
public static void main(String[] arg) {
// Assume the entered expression has only the expected characters otherwise they are ignored
String input = "(400+8) * (6-5)/((311-2) *(2+2)) xxx";
// Parse input
@rshepherd
rshepherd / ListBasedStack.java
Last active December 29, 2015 12:19
A linked list-based implementation of a stack.
public class ListBasedStack<T> implements Stack<T>
{
private Node<T> top = null;
public boolean isEmpty()
{
return top == null;
}
public void clear()
@rshepherd
rshepherd / CommandObject.java
Last active December 29, 2015 12:19
An example of using a command object pattern.
import java.util.Scanner;
public class CommandObject {
private static Scanner input = new Scanner(System.in);
// a stubbed out example of how you can use OO to clean up
// how you take user input from the user.
public static void main(String[] args) {
Command c = getCommand();
@rshepherd
rshepherd / ParametricPolymorphism.java
Last active December 29, 2015 12:19
Introduction to Java Generics
import java.util.ArrayList;
public class ParametricPolymorphism {
public static void main(String[] args) {
// Java Generics are an implementation of parametric polymorphism. They can be complicated subject.
// We will only focus on their simple use case with collections (lists, sets, stacks, etc)
// Old Java (1.4 and older):
@rshepherd
rshepherd / Exceptions.java
Last active October 17, 2022 07:23
Introduction to Java exceptions
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
public class Exceptions {
// An exception is an event that occurs during the execution of a program
@rshepherd
rshepherd / TwoDimensionalArrays.java
Last active December 29, 2015 00:19
Two dimensional arrays
public class TwoDimensionalArrays {
public static void main(String[] args) {
// An array keeps track of multiple pieces of information in linear order.
// A one-dimensional list.
// The data associated with certain programs (a digital image, a board game, etc.)
// lives in two dimensions. To model this data, we need a multi-dimensional data
// structure, that is, a multi-dimensional array.
@rshepherd
rshepherd / Node.java
Created November 12, 2013 23:39
A node illustrating the basic property of a linked list.
public class Node {
private int value;
private Node next;
public Node(int value) {
this.value = value;
}
public Node getNext() {
@rshepherd
rshepherd / SubtypePolymorphism.java
Last active December 28, 2015 04:09
Demonstration of subtype polymorphism
import java.util.Random;
public class SubtypePolymorphism {
public static abstract class Animal {
public abstract String talk();
public void write() {
System.out.println(this.talk());
}
@rshepherd
rshepherd / AdHocPolymorphism.java
Last active December 28, 2015 04:09
Demonstration of adhoc polymorphism via function overloading
public class AdHocPolymorphism {
// ad hoc polymorphism to refers to
// polymorphic functions which can be
// applied to arguments of different types,
public static class Adder {
// Note: int[] is not coercible to double[]
import java.util.Random;
public class Arrays {
public static void main(String[] args) {
// Declaration style.
int arrayTwo[] = new int[3];
int[] arrayOne = new int[3]; // This is better! []'s are part of the type!!
// Indexing: We always refer to the first element of an array [0].
// ex. subway example