CIT 590 Assignment 7: Jigsaw Puzzle
Spring 2013, David Matuszek

Purposes of this assignment

General idea of the assignment

In industry it is not unusual to be asked to translate some program from one language to another. This is a similar assignment.

Here is a Java assignment I made a couple of years ago: http://www.cis.upenn.edu/~matuszek/cit591-2011/Assignments/10-jigsaw-puzzle.html

Here is my solution to the assignment: jigsaw.zip

Your assignment is simply to translate my solution into Python. Insofar as possible, use the same classes, the same constructors, the same methods. Use the same names--do not change from the camelCaseConvention to the underscore_convention. If you discover something that is not possible to do using the exact same structure, bring it up on Piazza so that I can issue a correction or clarification.

Details

Command-line parameters

The assignment says to use command-line parameters for the number of rows and number of columns. While this can be done easily enough in Python, it cannot be done from within IDLE. Instead, just provide these two parameters in your main method, main(row, column), in JigsawPuzzle.

Class variables and class methods

In both Python and Java, most variables and methods belong to objects (also called instances). These are called instance variables and instance methods.

In both Python and Java, some variables and methods may belong to the class itself. These are called class variables and class methods. In Python, class methods are denoted with @classmethod (class variables aren't specially marked). In Java, class methods and variables are marked with the keyword static, hence they are sometimes called static methods and static variables.

Python Java
class MyClass:
    var = 1000
    
    def foo(self, n):
        print "foo", self
        self.var = n

    @classmethod
    def bar(self, n):
        print "bar", self
        self.var = n

instance_1 = MyClass()
instance_2 = MyClass()
instance_1.foo(1)
instance_2.foo(2)
MyClass.bar(3)
print instance_1.var, instance_2.var, MyClass.var
public class MyClass {
    static int static_var = 1000;
    int instance_var;

    void foo(int n) {
        System.out.println("foo " + this);
        instance_var = n;
    }
    
    static void bar(int n) {
        System.out.println("bar"); // Cannot use "this"
        static_var = n;
    }
    public static void main(String[] args) {
        MyClass instance_1 = new MyClass();
        MyClass instance_2 = new MyClass();
        instance_1.foo(1);
        instance_2.foo(2);
        MyClass.bar(3);
        System.out.println(instance_1.instance_var + " " +
                           instance_2.instance_var + " " +
                           static_var);
    }
}
foo <__main__.MyClass instance at 0x10266c098>
foo <__main__.MyClass instance at 0x10266c0e0>
bar __main__.MyClass
1 2 3
foo MyClass@1860045
foo MyClass@47bb2cb
bar
1 2 3

Notes:

  • Python's self is almost the same as Java's this. Both refer to the instance that is executing the code.
  • In a class method, Python's self refers to the class, but Java's this cannot be used at all.
  • Python allows instance variables and class variables to have the same name; Java doesn't.

Due date

Decide which of you will submit the assignment (only one assignment per team, please!) and submit it by 6am Friday March 1. Zip (don't use .rar) your .py files and submit the zipped file to Canvas. No other form of submission will be accepted.