Skip to content

Instantly share code, notes, and snippets.

@bhnascar
bhnascar / Practice.java
Created March 21, 2016 04:59
ArrayList practice questions
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
Created March 22, 2016 06:59
Solutions to ArrayList practice questions
import java.util.ArrayList;
public class Solutions
{
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 / 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() {
@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 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 / 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 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 / 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 / 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 / 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;