Archive for the ‘Computer Programming’ Category

Inserting Multiple Rows into a SQLite Database from Java

November 26, 2017 Leave a comment
package sqlitetest;
import java.sql.*;

public class SqliteTest {

    public static void main(String args[]) {
        Connection connection = null;

        try {
            /* The next line will give a ClassNotFound error unless the
               sqlite jar is in the classpath. To add it in Netbeans, 
               right-click on the project, choose Properties, Libraries 
               and add the path to the jar file. On Linux I saved it in 
               /usr/local/sqlite/sqlite-jdbc-3.20.0.jar, but this will be
               different if you are using Mac or Windows.
            connection = DriverManager.getConnection("jdbc:sqlite:test.db");

            // The database must already have a country table with these fields.
            // In my table there is also a CountryId primary key field, but the
            // database assigns values to that field automatically.
            String sql = "insert into Country (CountryName, Population) values (?, ?)";
            PreparedStatement ps = connection.prepareStatement(sql);

            // In larger applications you would probably loop through a list
            // of country objects. This is just a simple demo.

            ps.setString(1, "United States of America");
            ps.setString(2, "320000000");

            ps.setString(1, "China");
            ps.setString(2, "1400000000");


        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        System.out.println("Insert successful");

Simple chat server in Python

November 24, 2017 Leave a comment

Add the following code to and run.


import socket

sock = socket.socket()
host = 'localhost' # Change this to "" to receive connections from any host
port = 12221  # Any unreserved port but must be same as client
sock.bind((host, port))

conn = None

while True:
   if conn is None:
       # Halts
       print ('[Waiting for connection...]')
       conn, addr = sock.accept()
       print('Got connection from', addr)
       # Halts
       print ( '[Waiting for response...]')
       print (conn.recv(1024))
       msg = input("Enter something to this client: ")

Add the following code to


import socket

sock = socket.socket()
host = 'localhost' # Change this to remote ip as necessary
port = 12221 # Any unreserved port but must be same as server

sock.connect((host, port))
print ('Connected to', host)

while True:
msg = input("Enter something for the server: ")
# Halts
print ('[Waiting for response...]')
print (sock.recv(1024))

Nice Tkinter Tutorial

November 14, 2017 Leave a comment

Code is STUFF

November 9, 2017 Leave a comment

In English we distinguish between count nouns and non-count or mass nouns. Consider what feels wrong about these sentences:

  • My glass is full of waters.
  • I’ve got sands in my shoes.
  • Could I borrow a money?
  • I want to buy a furniture.

The reason these are wrong is that water, sand, money and furniture are examples of mass nouns. They are STUFF. You cannot pluralize stuff and you cannot refer to “a stuff”.

Why am I telling you this? Because “code”, as in lines of computer code, is also a mass noun. Code is STUFF. You cannot refer to “codes” and you cannot write “a code”.

Please try to remember this!

Course Information for All Courses 2017-2018

August 15, 2017 Leave a comment

Java Serialization Example

April 27, 2017 Leave a comment

Java provides a method of saving objects to file, called Serialization. In order for an object to be serializable it must implement the interface and you should set a version ID as well as shown below.

class Student implements {

   String name = "";
   Integer age = 0;
   Double gpa = 0.0;

   private static final long serialVersionUID = 1L;

You can save the object to file like this:

        try {
            FileOutputStream fileOut = new FileOutputStream(path);
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
        } catch (IOException i) {

And you can load it like this:

        try {
            FileInputStream fileIn = new FileInputStream(path);
            ObjectInputStream in = new ObjectInputStream(fileIn);
            obj = (Student)in.readObject();
        } catch (FileNotFoundException fnf) {
            System.out.println("No serialized file found at " + path + ". Creating new blank object.");
            obj = new Student();
         catch (Exception e) {

Is IB Computer Science for me?

January 24, 2017 Leave a comment

Key things to remember:

About 50% of the course is programming, so you will need to enjoy programming. I have given you a short diagnostic challenge below. If you enjoy trying to solve it, then you should do IB Computer Science.

High school computer science is generally NOT a pre-requisite to study computer science at university, so you can do computer science later, even if you don’t do it now. This is not true of other subjects like chemistry and physics.

I don’t set homework outside of reviewing what we’ve done in class and revision for summative assessments. All summative assessments take the form of written tests in the style of the IB Computer Science exam. There are three per semester, including end-of-year and mock examinations. That’s not a lot of tests, but I will expect you to have prepared well.

As with all IB subjects you will have a substantial internally assessed project. In IB Computer Science this takes the form of a software development project in which you must plan, design, code and implement a computer program for a real client.

Do you like programming?

This algorithm prints every item in a list of 100 things.

i = 0
while i < 100:
  print list[i]
  i = i + 1

This algorithm prints every item in the list from back to front.

i = 100
while i > 0:
  print list[i-1]
  i = i - 1

This algorithm prints “Hooray” if it finds the number 7 in the list:

i = 0
while i < 100:
  if list[i] == 7 then:
    output "Hooray"
  i = i + 1

Can you write an algorithm to determine if the list is a palindrome?
(ie it is the same forwards as backwards).