Skip to content

Instantly share code, notes, and snippets.

@bhnascar
bhnascar / InheritancePractice.java
Created March 25, 2017 21:12
Inheritance practice questions
class InheritancePractice
{
public static abstract class Vehicle
{
public int maxSpeed; // In MPH
public int acceleration; // In 0-60MPH secs
public Vehicle(int maxSpeed, int acceleration) {
this.maxSpeed = maxSpeed;
this.acceleration = acceleration;
@bhnascar
bhnascar / ReadingPractice.java
Created March 12, 2017 22:02
Code reading practice
import java.util.*;
class ReadingPractice
{
/* What is the return value of |mystery("hi")|?
*
* (A) hi
* (B) hi5
* (C) hi55
* (D) mn
@bhnascar
bhnascar / Errors.java
Created March 12, 2017 22:02
Error spotting practice
import java.util.*;
class Errors
{
/* Adds the given value to everything in the ArrayList.
*
* This AddToAll method does not work correctly. Explain
* why.
*
* Which of the following replacements for line 19 will fix the method?
@bhnascar
bhnascar / MoreArrayListTest.java
Created February 12, 2017 22:02
Tests for MoreArrayList
import java.util.*;
class MoreArrayListTest
{
public static void main(String[] args)
{
// Run all the tests.
testNormalize();
testShuffle();
testRemoveDuplicates();
@bhnascar
bhnascar / MoreArrayList.java
Last active February 13, 2017 06:32
More arraylist practice problems
import java.util.*;
public class MoreArrayList
{
// Think!
//
// What are the repeatable units of work?
// This is what goes in the for loop.
//
// If you see the keyword "every" or "for every X do Y"
@bhnascar
bhnascar / ArrayListTest.java
Created November 27, 2016 07:05
ArrayList exercise
public class ArrayListTest
{
public static void main(String[] args)
{
MyArrayList list = new MyArrayList(5);
for (int i = 0; i < 1000; i++) {
list.add(i);
}
@bhnascar
bhnascar / Solution.java
Last active October 30, 2016 18:52
TicTacToe: 2D array practice
import java.util.*;
public class Solution
{
public static class TicTacToe
{
/*
* This board will represent all the plays made in
* the game so far.
*
@bhnascar
bhnascar / Solution.java
Created May 3, 2016 05:00
2015 #3 part b
public void removeCol(int col) {
for (int i = entries.size() - 1; i >= 0; i--) {
SparseArrayEntry entry = entries.get(i);
if (entry.getCol() > col) {
// Fix the column number for this item because it's in a column
// to the right of the column we're removing
SparseArrayEntry fixedEntry = new SparseArrayEntry(entry.getRow(),
entry.getCol() - 1,
entry.getValue());
entries.set(i, fixedEntry);
@bhnascar
bhnascar / Tic-tac-toe.txt
Created May 1, 2016 04:46
Free response #6
Let's write (part of) a tic-tac-toe game!
Let's suppose the game board is represented using a 3x3 2D array of
integers like this:
______________________
| | | |
| 1 | O | O |
|______|______|______|
| | | |
@bhnascar
bhnascar / Todo.txt
Last active May 1, 2016 21:55
Free response #5
Let's imagine you're a really forgetful person, so you decide to make
a to-do list program to help you remember things.
Everything that you want to do has a description and a time. For
example, uh..."buy eggs at Costco on Wednesday". So let's represent
things that you want to do with the following class:
public class ThingToDo
{
private String description; // Ex. "Buy eggs at Costco"