# Donate to Wikipedia

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

Donate to Wikipedia now

# Nested loops: looking for duplicates in arrays

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

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.

```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

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)
• 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

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

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.

• 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

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

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

# Turtle graphics in Python

Here is a sample program to get you going:

```import turtle
scr = turtle.Screen()
t = turtle.Turtle()
t.forward(100)
t.left(90)
t.forward(50)
turtle.done()
```

Here is the documentation from Python.org:

https://docs.python.org/2/library/turtle.html