Skip to content

Instantly share code, notes, and snippets.

@rshepherd
rshepherd / DigitalWallet.java
Last active August 29, 2015 14:10
Solution to practice midterm
public class DigitalWallet {
private static double maxWithdrawalPct = .5;
private long accountId;
private long balance = 0;
private TransactionHistory transactions = new TransactionHistory();
public DigitalWallet(long accountId) {
this.accountId = accountId;
@rshepherd
rshepherd / Constructors.java
Last active August 29, 2015 14:09
Constructors
public class Constructors {
// Constructors initializes a class for use
// They must have the same name as the name of the class
public static class Cube {
// You can provide initial values for your instance variables like so
// (You don't necessarily need to do this in a constructor)
private int length = 1;
private int width = 1;
@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
@rshepherd
rshepherd / PracticeMidterm.java
Created October 25, 2014 18:54
Answers to PAC Midterm Pt 1 from 2013
// Question 1
public static boolean isVowell(char letter) {
switch (letter) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
return true;
case 'y':
import java.util.Arrays;
public class Methods {
public static void main(String[] args) {
// Methods are just functions that belong to a particular class
// Methods are sub-routines. Reusable code.
// Method have parameters, are called with arguments
@rshepherd
rshepherd / FizzBuzz.java
Last active August 29, 2015 14:07
A solution to the FizzBuzz faux-quiz.
public class FizzBuzz {
public static void main(String[] args) {
for(int i = 0 ; i < 100 ; ++i) {
boolean fizz = i % 3 == 0;
boolean buzz = i % 5 == 0;
if (fizz && buzz) {
System.out.println("FizzBuzz");
} else if (fizz) {
System.out.println("Fizz");
} else if (buzz) {
@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();
@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 / 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()