Find Nth Occurrence

The third question on the 2017 AP Computer Science Exam had you go through a phrase and replace the Nth occurrence of a string and find the last occurrence. For both of these you were required to use a method called findNthOccurrence , which did not have an implementation shown. Like many AP FRQs you were given an arbitrary method to use, but didn't need to know or care how that method worked.

But let's say you're interested. How might that method be implemented?

While writing a JUnit test for this question I needed an implementation of this method. Here is what I came up with.

public int findNthOccurrence( String str, int n ) {
    int idx = currentPhrase.indexOf( str );
    if ( idx == -1 ) {
        return -1;

    for ( int i = 1; i < n; i++ ) {
        idx = currentPhrase.indexOf( str, idx + 1 );
        if ( idx == -1 ) {
            return -1;
    return idx;

First step is to decide if the substring str is in currentPhrase at all. If it's not the first indexOf will return -1 and we'll go ahead and bail then.

Then we'll start looping at 1 up to n to see if we find that occurrence of str. If we don't find it, again we return -1. If we do find it we return the location as idx.

AP Test Tips

No, it’s really not that bad.

Some of this is specific to the computer science AP-A test since that’s what I have the most experience with. But some of it is applicable to any AP test, or any high stakes test for that matter.

About the test

The Computer Science AP-A exam is split into two sections; a 90 minute, 40 question multiple choice test; and a 90 minute, 4 question free response section.

General Test Strategies

Let’s start with some basic strategies that apply to any test.

Get Your Sleep

This is not a test that you should be cramming for the night before. You shouldn’t stay up late studying. You’ve been working towards this test for almost an entire school year. The night before anything you might pick up from cramming will probably be negated by your lack of sleep.

And Eat Breakfast

Continue reading

Download Student Submissions

Just in time for the spring semester we’ve pushed out a new feature that will allow you to download student submissions from programming problems.

You’ve always been able to view your students’ submissions when you embed a ClassCube problem into an LTI consumer like Canvas or Moodle. Now with a couple of clicks you’ll be able to download a zip file with all of their submissions.

How to Download

First step is to go to the assignment. We’re using Canvas for these screen shots, but the process is the same in Moodle even though it might look a little different.

Continue reading

How to test private instance variables in Java

Why test private instance variables?

Need to test private instance variables in Java? Well, if you ask how to do that on a discussion forum you’ll probably get the response that you shouldn’t test anything private. Instead, call the public method that’s calling using the instance variable and make sure it works the way it should. If the public method tests correctly then the private variable must be working.

But what if you’re teaching coders, especially new coders? It’s a lot easier to test each small piece individually and let them know what needs fixing and not waiting until the end.

Fortunately, all it takes is a little bit of Reflection.

Testing Methods

In all of the test cases that I build I have the following two methods.

private <T> T getField( Object instance, String fieldName ) throws Exception {
    Field fld = instance.getClass().getDeclaredField( fieldName );
    fld.setAccessible( true );
    return (T) fld.get( instance );

private void setField( Object instance, String fieldName, Object value ) throws Exception {

    Field fld = instance.getClass().getDeclaredField( fieldName );
    fld.setAccessible( true );
    if ( value instanceof Integer ) {
        fld.setInt( instance, (int) value );
    else {
        fld.set( instance, value );


Getting and setting values

Now that you’ve got the methods, it’s just a matter of using them.

Let’s say that you’ve got a student that is writing a constructor that should set the someVar instance variable to 3.

@test ( timeout = 250 )
public void test() throws Exception {
    SomeClass c = new SomeClass();
    int expected = 3;
    int actual = getField( c, "someVar" );
    assertEquals( expected, actual ); 

This goes out and checks that someVar was set to the right value.

But what if we want to go the other way. Instead of testing that the value is set correctly, we want to check that getSomeVar returns the correct value. This time we need to set someVar and then check the method to make sure it returns the right value.

@test ( timeout = 250 )
public void test() throws Exception {
    SomeClass c = new SomeClass();
    setField( c, "someVar", 3 );
    int actual = c.getSomeVar();
    assertEquals( 3, actual ); 

What’s about super classes?

Well, this falls apart a bit if you’re needing to test parent classes. The methods above will only look in the actual class and not upstream to the parents. There’s a way to make that work, but let’s save that for another post.

How to embed a Gist in Canvas

octocatIf you’re teaching computer science, odds are pretty good that you need to share code with your students. Maybe it’s starter code for a project. Maybe it’s a solution for lab. But it’s something that you probably do.

Canvas, and specifically the rich text editor, makes this a little more difficult than it should be. Web editors tend not to be friendly to the tab key when you’re trying to indent code.

Enter GitHub & Gists

GitHub is probably something that you’re familiar with, even if it’s just a word that you’ve heard in passing. If not, it’s a website where users publish their code so that it can be viewed, edited, and shared. It makes coding a bit more of a social process. And it’s really great for open source projects.

But GitHub has a smaller, but perfect service for what we’re trying to do and that’s a Gist. A gist is a piece of code that you want to share with other users. If you already have a GitHub account you can create gists under your account, but you can also create them anonymously. For this demo that’s what we’re going to do. But if this is something that you think you mind wind up doing often it’s probably worth setting up an account. They’re free.

Creating a Gist

Let’s head over to If you have an account you can go ahead and login so that the gists show up under your account.

You should see a page that looks similar to this, except it will be blank. I went ahead and typed in a bit of code that we’ll be using later.


If you give your code a filename GitHub will automatically syntax highlight it for you. And, the editor works just like you would expect it to. Tabs work, braces highlight, that sort of thing.

If you’ve got more than one file, there’s an Add File button on the bottom of the editor.

Once you’re done, click on one of the two Create buttons on the bottom right of the editor. The only difference is that secret gists don’t show in search results. But, anyone that knows the URL can get to it. There’s no real protection, so you probably want to be careful about what you put out there.

Viewing the Gist

After saving your gist you’ll be bounced to the URL of your newly created page. It’ll look a bit like this.


The important part is the URL. Go ahead and copy it. We’ll need it later, and it’s not something that you’d want to type in manually.

You may also notice that there’s a button that says embed. This link, the one that starts with <script, would allow you to embed this gist into a web page. Unfortunately, Canvas strips out HTML script tags out of any content entered so it won’t work.

Over to Canvas

Now it’s time to actually embed the gist, so head on over to your Canvas account. For the demo we’re going to embed the gist into a discussion page, but it could be anywhere that you enter text using the rich text editor.

Once you get to the page where you want to embed the gist click on the link above the editor to switch from rich text to html mode. This is important because if we try to type in the embed code directly into the rich text editor it will convert it in such a way that it won’t work.


Notice the text we’ve entered. It’s the HTML code for an iframe. You’re going to want to replace the URL next to src with the gist URL you copied earlier.

There’s one important change before you save though. If you just paste in the URL you’ll embed the entire GitHub site with all of the headers and stuff. If all you want is the files, and it probably is, you’re going to add .pibb to the end of the src URL. This tells GitHub that you want to get the iframe friendly version of the gist. That way there’s no headers. Just the files.

Click to save your page and you’ll see something along these lines.


Fixing the Size

Okay, so that’s not quite right. Yes, it’s embedded. But it’s tiny. There’s a really easy fix though.

What we need to do is specify the size of the iframe with a bit of inline CSS. Click to go back and edit your post and then click on the link to switch back to HTML mode.

What we want to do is specify how big the iframe should be. Odds are pretty good that Canvas has already guessed, but probably guessed wrong. You might see something like height="300" width="300" as part of your iframe code. Just leave that there. What we’re about to do will override that.

Here’s a screenshot of what we’re changing.


Notice that we added style="width:100%;height:500px;" to the embed code. This is a bit of inline CSS that tells the web page how big to make your frame. It’s going to be 100% of the available width which will take it edge to edge in your post. It’ll also be 500 pixels (px is the abbreviation for pixels) tall. We can’t use a percentage here because of the way HTML does percentage heights. So we’re going to specify exactly how tall to make it.


And that’s much better. The gist is now across the entire width of the post.

But there’s a big gap between the bottom of the gist and the bottom of the post. That’s because 500 pixels was too much. But it’s not something you want to worry about too much. Even if you get the number exactly right, some browsers will still be a bit off and may leave the gap or not show the entire gist with scrollbars. I typically will either leave a pretty good gap if it’s a short bit of code or leave it short enough for longer code so that it’s obvious that they need to scroll.

Welcome to ClassCube

Hi. Welcome to ClassCube.

We’re working on getting everything up and running, but aren’t quite there yet. When we go live you’ll be able to create an account and practice programming. Feel free to look around or ask questions in the forum. There’s just not much here yet.

But since you’re here, here’s a video of what ClassCube looks like. It might change a bit before we go fully live, but this should give you an idea.