Home‎ > ‎ICT Programming‎ > ‎Java‎ > ‎

Connect 4 - Part 3

Connect Four

3. Part: Integrating the Components

This last part of Assignment 2 is less a programming assignment than a tutorial of how to build a stand-alone application from individual pieces. It is intended to show you how you can take your BlueJ-code and work with it in any Java environment. To make it easier for you, we will give you most of the code.

There will be a few questions throughout this assignment. You will have to submit the answers to these questions as part of the deliverables.


1. Task

Download Connect4Game.java and import it into your project. Look at the source code and try to figure out what it does. 
Hint: Find the method where everything gets started...


2. Task

Compile the code. If you have implemented Connect4Board and Connect4Logic according to the given specifications, there shouldn't be any errors. There might be a few warnings, but, well, you know what we do with these at the moment... We ignore them! If you encounter any problems, check your code again and see if your classes meet the requirements. If you cannot fix the problem, let me know right away!

Now play the game. To do so, create an object of type Connect4Game and call the appropriate method. 
Note: Don't worry if you haven't fully understood the code, yet. There aren't many methods to choose from. 

QUESTION 1: Which method starts the game?


3. Task

Since everything should work fine at this point, we would like to build a stand-alone application. Our first try is to use the code just the way it is.

Copy all source files of your project (i.e., the files that end with .java) into a new directory, which is not associated with BlueJ. You should have only the java files in this directory, no BlueJ files and no class files!

Open a terminal window, change into the new directory and compile Connect4Game.java using the Java 1.5 compiler. The command for this is:
javac Connect4Game.java
Do NOT compile any other sources!

Note 1: Make sure that you are using Java version 1.5. You can find out the version number by typing java -version

Note 2: If you are using a Mac at home, the correct compile command should be/System/Library/Frameworks/JavaVM.framework/Versions/1.5/Commands/javac Connect4Game.java
If you are not using this long javac command, you might end up with an old compiler version, which won't work for our program.

Note 3: On a Windows machine, the command depends on the directory structure on your computer. Open an MS-DOS windows and see if javac by itself works. If not, you'll have to find the location where java is installed. Then you have to use a command like C:\path\javac where "path" is the path to the java directory.

QUESTION 2: Which class files were created by compiling Connect4Game.java?


Execute the program by typing java Connect4Game

Note 1: On your home Mac this would be :
/System/Library/Frameworks/JavaVM.framework/Versions/1.5/Commands/java Connect4Game


Note 2: In Windows, you would type the appropriate path to the java command.

QUESTION 3: Can you execute the program? If not, what is the error message?



4. Task

So, why was it not possible to execute the program in the previous task? What went wrong?
Well, there is basically not a clear entry point into the program. Java doesn't know where to get started! We need to tell it that by providing a main method.

Let's do this now. Inside the class Connect4Game create a new method in exactly the following form:

    public static void main(String [] args)
    {
        // the code goes here...

    } 

Make sure that the main method is inside the class! I personally like to add it after all the other methods. You can edit the code in BlueJ.
 
Note: The keyword static means that the method main exists even if we haven't created an object of type Connect4Game, yet. If that wasn't the case, main could only be called after an object had been created. But who would create this object initially? You would need to have an object before calling the method, but you can't create an object without a method call. Get it?! This would basically be a "hen and egg" problem!
That's why we have a static main method, which can be called independent of any existing objects.

Inside this main method, we need to provide just enough code to get the whole thing started. This can be done in two lines.
Write code, which does the following:
  • In a first statement, declare and create an object of type Connect4Game. You can give it whatever name you like.
  • In a second statement, use this object and call its play method. That's it.

After you added these two lines of code, try the program in BlueJ. Do NOT create a new Connect4Game object. Instead, call the main method directly. It should work!


5. Task

Copy the modified Connect4Game.java file into the new directory that you created earlier and compile it again. Make sure you use the Java 1.5 compiler (see 3. Task). After compilation execute the program with the java command (see above).

QUESTION 4: Does it work as a stand-alone program?




Comments