Skip to content

Instantly share code, notes, and snippets.

@rshepherd
rshepherd / bit_intrepretations.cpp
Last active December 23, 2015 15:29
Interpreting bit patterns as different types
#include<iostream>
using namespace std;
int main() {
//long x = 1;
long x = -1;
// A pointer to the bit pattern, interpreted as a 32-bit signed int
int* i = (int*) &x;
@rshepherd
rshepherd / BadConditionals.java
Last active December 23, 2015 16:19
What does this print?
boolean a = false;
boolean b = true;
// Example 1
if(b);
a = true;
System.out.println(a)
// Example 2
@rshepherd
rshepherd / Expressions.java
Last active December 23, 2015 16:19
Expressions versus statements
public class Expressions {
public static void main(String[] args) {
// Expressions evaluate to a value...
int z = 1 + 1;
// ^^^^^
// Statements do not.
System.out.println("This is a statement.");
public class ForLoops {
public static void main(String[] args) {
// for ( init ; test ; increment ) {
// body
// }
// 1) The 'init' code runs once to set things up at the very start of the loop.
// 2) The boolean 'test' is evaluated.
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
@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[]
@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 / 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 / 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 / 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