Archive

Archive for the ‘IB Computer Science 2018’ 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();
        }

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]);
    }
  }
}

Basic Java Class Structure

November 28, 2016 Leave a comment

Basic knowledge for IB Computer Science is the structure of a Java class. You should be able to create the class, its instance variables, constructor, get and set methods, and a toString() method, without even need to stop and think.

Question:

Create a Point class that has two instance variables x and y, corresponding to its coordinates on the Cartesian plane. Include an appropriate constructor that initialises these variables. Encapsulate the variables and provide a toString() method.

Answer:

public class Point {
   private int x;
   private int y;

   public Point(int x, int y) {
       setX(x);
       setY(y);
   }

   public int getX() {
      return x;
   }

   public int getY() {
      return y;
   }

   public void setX(int x) {
      this.x = x;
   }

   public void setY(int y) {
      this.y = y;
   }

   public String toString() {
      return "(" + x + "," + y +")";
   }
}

Java Quiz on Monday 7th November

November 3, 2016 Leave a comment

Need for and use of data types

  • int
  • double
  • String
  • boolean

Definition example, advantages of: Encapsulation, Polymorphism, Inheritance

  • Creating basic classes
  • instance variables
  • a constructor
  • methods

References and NullPointerExceptions

Basic algorithms and method definition

  • Passing parameters
  • Returning values
  • Basic flow of control (if, for, while)
  • Looping through arrays
  • Standard algorithms such as finding the max of an array, etc

Questions types:

  • Write a method that…
  • Interpret code (and perhaps create trace table)
  • Outline/explain the advantages/disadvantages of…
  • Adding code/variables to a class to change its behaviour, e.g. adding a constructor or encapsulating a variable

Remember

  • Get clues as to the structure of your answer by the number of marks available
  • You can get marks for writing code in longer answer questions
  • If you don’t know the full algorithm, you can still get marks for whatever code you do write
  • What, when, how, why, otherwise

Printing the numbers from 0 to 99

August 8, 2016 Leave a comment

Students often get hung up on slight syntax differences, as if knowing how to program is the same as knowing a language’s syntax. It isn’t. Look at these examples and check you understand that they do the same thing. It is the logic of your programs that is important, not the brackets and semi-colons.

IB Computer Science pseudocode

loop J from 0 to 99
  output J
end loop

Python

for j in range(0,100):
  print(j)

Java

for (int j = 1; j < 100; j++) {
  System.out.println(j);
}

 

Using Python on Mac

August 8, 2016 Leave a comment

The IDLE that comes with Python for Mac only gives you an interactive REPL (ie You type a line of Python and it executes it as soon as you hit Enter.). In order to run files, this is what you have to do.

  • Download Python 3.x from https://www.python.org/downloads/ and run the pkg file
  • Make a desktop shortcut to Python Launcher
  • Open up TextEdit and choose Format, Make Text Only
  • Write your Python program, e.g. print (“Hello World!”)
  • Save as HelloWorld.py
  • Find HelloWorld.py in a Finder window and drag it onto your new Python Launcher desktop shortcut
  • Bob’s your uncle

Alternatively you can find a full IDE. My understanding is the PyCharm is pretty good.

https://wiki.python.org/moin/IntegratedDevelopmentEnvironments