Skip to content

Instantly share code, notes, and snippets.

@bhnascar
bhnascar / Seats.txt
Created May 1, 2016 04:40
Free response #4
Do you like classical music? I like classical music. Anyway...let's
say we're going to see a concert with a bunch of friends. We want
to sit together in a row and there's 5 of us. Furthermore we like to
sit close to the front. Cause you know, better view and everything.
So given a seating chart, I want you to write a method that will
tell us the best row where we can sit.
The seating chart is going to be a 2D array like so...
@bhnascar
bhnascar / Quilt.txt
Last active April 30, 2016 03:05
Free response #3
One thing Stanford students get too much of is free T-shirts. There's
free T-shirts from career fairs, from your academic department, from
dorm events, from campus events, from student groups, etc. etc. etc.
So one thing we like to do with our old T-shirts is make quilts out of
them!
Imagine we had 9 T-shirts. We could stitch them together a 3x3 quilt,
where each square unit is a T-shirt. So something like:
__________________________________
@bhnascar
bhnascar / Bikes.txt
Last active April 25, 2016 14:22
Free response #2
Imagine we're a bike shop. We sell bikes. Bikes have a manufacturer,
a model name, and a price. So we will represent bikes with the
following class:
public class Bike
{
private int price;
private String modelName;
private String manufacturer;
@bhnascar
bhnascar / Dice.txt
Created April 25, 2016 14:11
Free response #1
Let's write a dice simulator. I want you to write a class that can
create an n-sided dice and simulate a dice-roll. In other words, given
n, you will create a "dice" with n faces numbered 1...n. To "roll" the
dice, you will basically pick a random integer between 1 and n.
The class should be able work like this:
Dice d1 = new Dice(6);
d1.roll(); // Returns random number between 1 and 6
d1.roll(); // Returns another random number between 1 and 6
...etc...
@bhnascar
bhnascar / MethodPractice.java
Created April 8, 2016 19:58
Trick questions involving Java methods...
public class MethodPractice
{
public static int question1(int a, int b) {
return a * b;
}
public static void question2(int a) {
a = 2 * a;
}
@bhnascar
bhnascar / Practice.java
Created April 4, 2016 01:02
More loop practice
public class Practice
{
public static void main(String[] args) {
// How can you replace the hard coded numbers
// with a variable and print out the same thing
// as the code below?
//
// There's more than one way to do this.
System.out.println(1);
@bhnascar
bhnascar / Practice.java
Created April 3, 2016 22:26
For loop practice
public class Practice
{
public static void main(String[] args) {
// How can you replace the hard coded numbers
// with a variable?
System.out.println(1);
System.out.println(2);
System.out.println(3);
System.out.println(4);
@bhnascar
bhnascar / Practice.java
Created March 23, 2016 05:44
Practice problems from today
import java.util.ArrayList;
public class Practice
{
public static void main(String[] args) {
// An example list of words to test against.
ArrayList<String> words = new ArrayList<String>(10);
words.add("foo");
words.add("bar");
words.add("baz");
@bhnascar
bhnascar / Solutions.java
Last active March 23, 2016 11:24
Solutions to last two problems
/* Returns all books in this library that are
* written by the given author.
*
* @param author - the author.
*/
public ArrayList<Book> getBooksByAuthor(String author) {
// Two comments:
//
// (1) Your solution is pretty close but it returns
// a SINGLE book by the given author, not all
@bhnascar
bhnascar / Practice.java
Created March 23, 2016 02:13
More AP-style questions
public class Book
{
private String title;
private String author;
private String genre;
private int numberOfPages;
/* Other methods not shown. */
public String getTitle() {