Skip to content

Instantly share code, notes, and snippets.

@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 / Scoping.java
Last active August 28, 2020 04:56
An example of the implications of Java's lexical scoping rules in the context of a class.
public class Scoping { // class scope begins
// Because 'a' is declared just inside the *class curly braces* it is in 'class scope'
// therefore its visible everywhere inside the class. This is the 'widest' scope in this file.
private int a = 0;
// Class variables have class scope!
private static String classVariable = "I am a class variable!";
public void methodOne() { // methodOne scope begins
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
class DoublePointer {
String s;
public DoublePointer(String s){
this.s = s;
}
}
public class IdentitySwitch {
public static void main(String[] args) {
DoublePointer dp = new DoublePointer("unchanged");
@rshepherd
rshepherd / ForEach.java
Last active January 26, 2017 08:24
Examples of using Java's 'for each' loop structure
import java.util.ArrayList;
import java.util.List;
public class ForEach {
public static void main(String[] args) {
// Let's say we have a plain old array, like this one.
char[] array = "This is a character array.".toCharArray();
public class IdentitySwitch {
public static void main(String[] args) {
String s = "unchanged";
System.out.println(s);
foo(s);
System.out.println(s);
}
public void foo(String s) {
s = "changed"
@rshepherd
rshepherd / ListBasedQueue.java
Last active December 30, 2015 02:29
A linked list-based implementation of a queue.
public class ListBasedQueue<T> implements Queue<T>
{
private Node<T> first; // beginning of queue
private Node<T> last; // For efficiency, maintain a reference to the least-recently added Node on the queue.
public ListBasedQueue() {
first = null;
last = null;
}
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 / 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):