torkell: (Default)
My latest distraction is Layton's Mystery Journey: Katrielle and the Millionaire's Conspiracy (because I clearly don't have enough in-progress games at the moment - how many Zelda games was I blogging again?), which is the latest incarnation in the Layton series. It's an interesting twist on the classic Layton gameplay - there's definitely a much more episodic style to it, in that there's a series of individual cases with an overall plot thread running through all of them. The puzzles seem to have less importance to the plot, though the game still throws no end of them at you to solve.

It puts me much more in mind of a Phoenix Wright game than a Professor Layton one.

That said, this game has an unusual twist in that there's a tie-in website with 50 real-world puzzles to find and solve. Some of those puzzles are surprisingly tricky!
torkell: (Default)
So here's a bit of a thought experiment, inspired by a problem I tackled at work recently...

Aren't "eventually consistent" databases fun? )

torkell: (Default)

Bonus extra post, to avoid [livejournal.com profile] talismancer mocking the lack of content today. That said, he hasn't updated yet today...

There's been a few guesses at monday's Java puzzler. [livejournal.com profile] talismancer is right in that the code won't work. [livejournal.com profile] olego and [livejournal.com profile] pteppic took this a bit further, and correctly guessed that it'd throw an exception of some sort. Specifically, you get the following output:

Adding java.lang.Object@addbf1 at index 0
Adding java.lang.Object@42e816 at index 1
Adding java.lang.Object@9304b1 at index 2
Adding java.lang.Object@190d11 at index 3
Adding java.lang.Object@a90653 at index 4
Adding java.lang.Object@de6ced at index 5
Adding java.lang.Object@c17164 at index 6
Adding java.lang.Object@1fb8ee3 at index 7
Adding java.lang.Object@61de33 at index 8
Adding java.lang.Object@14318bb at index 9
Removing java.lang.Object@addbf1
Exception in thread "main" java.util.ConcurrentModificationException
	at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
	at java.util.AbstractList$Itr.next(Unknown Source)
	at Foo.emptyBarList(Foo.java:15)
	at Foo.main(Foo.java:24)

Line 15 is for (Object o : barList) {

At first this seems like an odd place to get an exception - there's no obvious code in this line. But it makes sense when you realise that the Java compiler rewrites that line into something like:

Iterator<Object> it = barList.iterator(); while (it.hasNext) {Object o = it.next();

Most of the standard Java collections explicitly state that structural modifications to the collection will generally cause any iterators to throw a ConcurrentModificationException. Fortuantly there is a way round this: you're permitted to modify the list via methods on the iterator object (like Iterator.remove()). The concurrent collections also won't throw an exception, as the iterators in those are designed to be fully thread-safe and cope with (but don't necessarily show the result of) structual changes.

The fix in this case is easy enough: rewrite the loop to explicitly create an Iterator object and call the remove() method on that. And then add a unit test so that the method is actually tested, rather than being left as a hidden bug until someone does a completely unrelated change and actually adds code to call it!

torkell: (Default)

Okay, so it turns out that [livejournal.com profile] talismancer's post yesterday was filler. The moral balance has swung back!

As tempting as it is to fulfill the entirety of NaBloPoMo with us commenting on each other's posts, I feel like I should actually write something. So in the interests of increasing audience participation, I shall set a Java puzzler.

The puzzler consists of the following class:

import java.util.ArrayList;

public class Foo {
    private ArrayList<Object> barList = new ArrayList<Object>();

    public void fillBarList() {
        for (int i = 0; i < 10; i++) {
            Object o = new Object();
            System.out.println("Adding " + o + " at index " + i);
            barList.add(o);
        }
    }

    public void emptyBarList() {
        for (Object o : barList) {
            System.out.println("Removing " + o);
            barList.remove(o);
        }
    }

    public static void main(String[] args) {
        Foo f = new Foo();
        f.fillBarList();
        f.emptyBarList();
    }
}

Without running it (because that would be cheating and you will be mocked for it), what do you think will happen when Foo.main() is called?

torkell: (Default)

Something a colleague came up with at work the other day...

Consider the following Java classes:

public class FooBase {};

public class Foo extends FooBase {};

public class Bar {
  public Bar(FooBase) {
    System.out.println("Constructor Bar(FooBase) called");
  }

  public Bar(Foo f) {
    System.out.println("Constructor Bar(Foo) called");
  }
}

public class Test {
  public static void testSomething() {
    java.util.ArrayList<FooBase> list = new ArrayList<FooBase>();
    list.add(new Foo());
    FooBase item = list.get(0);
    Bar b = new Bar(item);
  }
}

Without compiling and running the program (because that would be too easy a way to solve this), what do you think will happen when you call Test.testSomething()?

My guess is that it will print "Constructor Bar(Foo) called", because while item is a reference of type FooBase the object being pointed to by item is actually of type Foo. But then the guy who came up with this pointed out that the output "Constructor Bar(FooBase) called" would also make sense, and if this were C++ that would be the expected output. The difference is all C++ knows is that it has a pointer to something that must be a FooBase, while Java knows that it has a pointer to something that is actually a Foo.

torkell: (Default)
Today's the first of November, which means it's time for National Blog Posting Month again. Let's see how well this one goes...

Now, it's really tempting to end the post with that, but a) that's a bit of a cop-out, and b) I think I did that last year. Let me check... okay, so it's not quite a repeat of last year, but it's still a cop-out and I'll get soundly mocked by [livejournal.com profile] talismancer if I try that this early in the month.

So. What has the [livejournal.com profile] boggyb been up to recently? Well, amongst other things I've been playing Professor Layton and the Lost Future, which is a real gem of a puzzle game. I've always liked puzzle games, and Professor Layton excels at this by being a puzzle game *with* plot. Lost Future begins by you receiving a letter from the future, with instructions on how to get there. Of course, nothing is quite what it seems, and the game is full of plot twists. It's not everyone's cup of tea, but I do recommend the series to anyone who likes puzzles.

It's taken me about 12 hours to beat the main plot, and along the way I've solved all the main puzzles (there's one or two I've not found yet), along with a couple of the unlockable puzzles and most of the side quests. Unlike what TV Tropes would have you believe I've also collected enough picarats for most of the unlockables, and should be easily able to get enough to unlock the rest with the bonus and secret puzzles. I've not got as many as I'd like though, as the game delights in trick puzzles. Consider this one as an example:


The following equation is formed of ten matchsticks, and uses Roman numerals:

I + IX = X

What's the fewest number of matches you need to move to make it a valid equation?
torkell: (Default)
On one side of a river there are three misssionaries and three cannibals. There is a boat which can be used to transfer people from one side of the river to the other. No more than two people can fit in the boat and it must have at least one person in it during any transfer. The problem is to find the shortest sequence of transfers which gets all six people from one side to the other without ever creating a situation where missionaries outnumber cannibals on either side of the river. (The idea being that if this happens, the missionaries will then be able to corrupt the cannibals by 'converting' them.)


The usual aim is to avoid the cannibals outnumbering the missionaries and eating them. But apparently that's not politically correct.

January 2026

S M T W T F S
     123
45678910
11121314151617
18192021222324
25262728293031

Syndicate

RSS Atom

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jan. 9th, 2026 01:09 pm
Powered by Dreamwidth Studios