Archive

Archive for the ‘IB Computer Science 2017’ Category

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 java.io.Serializable interface and you should set a version ID as well as shown below.

class Student implements java.io.Serializable {

   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);
            out.writeObject(obj);
        } catch (IOException i) {
            i.printStackTrace();
        }

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) {
            e.printStackTrace();
        }
Advertisements

Donate to Wikipedia

January 10, 2017 Leave a comment

I just donated to @Wikipedia. Support free knowledge! #iloveWikipedia

Donate to Wikipedia now

Nested loops: looking for duplicates in arrays

December 1, 2016 Leave a comment

A and B are arrays of integers. Construct code to output all of the values that are in both A and B.

With questions like this, imagine you had to do it manually. Imagine you have two long lists of numbers on paper. How would you find all the values that occur in both lists?

You would probably look at the first number from list A and then scan all the way down list B to see if it’s there. If it was you’d make a note of it. Then you would get the next number from list A and do the same thing.

A computer algorithm will do exactly the same thing.

In pseudocode this is:

loop I from 0 to A.length - 1
  loop J from 0 to B.length - 1
    if A[I] = B[J] then
      output A[I]
    end if
  end loop
end loop

In Java it is:

for (int i = 0; i < a.length; i++) {
  for (int j = 0; j < b.length; j++) {
    if (a[i] == b[j]) {
      System.out.println(a[i]);
    }
  }
}

First day handout and assessment plan: IB Computer Science II

August 4, 2016 Leave a comment

Case Study 2017.pdf

May 23, 2016 Leave a comment

This is the case study for the May 2017 exam. It is on the use of computer science in medicine. Sounds very interesting!

https://drive.google.com/file/d/0B-llANrmLzRQZEl4aFBSMnFTZjg/view?usp=docslist_api

Computer Science end-of-year exam: Grade 11

May 10, 2016 Leave a comment
  • 2 hours
  • 100 marks (60 on topics 2-7, 40 on Option D)
  • Don’t remove the question paper from the exam hall
  • Answer on lined paper provided

Good programming problems page

April 26, 2016 Leave a comment

Most of these are at the right level for IB Computer Science. There are some good links at the bottom too.

http://adriann.github.io/programming_problems.html