17
Introduction to Programming Class 11, Quiz 2 Paul Brebner

Introduction to programming - class 11

Embed Size (px)

Citation preview

Page 1: Introduction to programming - class 11

Introduction to ProgrammingClass 11, Quiz 2

Paul Brebner

Page 2: Introduction to programming - class 11

What next?

• Revision quiz (number 2, at end)

• Chapter 8 Text Book - Functions

• Canon game extensions

Page 3: Introduction to programming - class 11

Function• In maths, a function is a calculation that produces an output from some inputs.

– E.g. square(x) = x * x

• In processing this is written:int square(int x) {

int result = x * x; // the variable result is local to the function

return result; // to make it visible outside the function return it

}

• And used like this:int y = square(3);

Or like this:println(square(3));

Or used lots of times with different parametersprintln(square(1) + square(2) + square(3));

Or println(square(square(square(2)));

• To make a function you need to need to tell processing what the output (return) type is, the name of the function, the types and names of any input parameters, how to calculate the output, and what the output is.

• In processing you don’t have to have a return output, or even any parameters.

– void means there is no return value. () means there are no input parameters. E.g.

void sayHello() {

println(“Hello”);

}

Page 4: Introduction to programming - class 11

We’ve already been using functions

• E.g.– All the built in processing primitives

• e.g. size(), ellipse(), etc

– void setup() and void draw()

• When to use functions?– When you want to do something over and over with

different values, makes code shorter, reduces chances of errors

– When you want to hide (and forget) the complexity of doing something

• Text book, chapter 8, Ex 8.1-8.8 (Dice and Owls)

Page 5: Introduction to programming - class 11

Arrays, Text book chapter 10

• Normal variables are good for small numbers of things– What if you need lots of things?– Arrays to the rescue– Arrays allow you to have lots of things (elements) with the same

name, and different “addresses” (index).

• E.g.int[] years = {1917, 1921, 1962, 1994, 1999};

– The name of the array is “years”.– The type of the elements is “int”.– [] means to make an array rather than normal variable.– = {1917, 1921, 1962, 1994, 1999}; means to make a big enough array

and fill it with these elements at the start.– Array indexes start at 0 and count up by 1.

• So 1917 is at position 0, 1921 position 1, ... 1999 at position 4.

– There are 5 elements in this array.

Page 6: Introduction to programming - class 11

Using arrays

int[] years = {1917, 1921, 1962, 1994, 1999};

• How to get the elements out?int year1 = years[0]; // index 0 gives 1st element- But watch out – if you use a index that is too big or too small you will get

an error!int year5 = years[5]; // error, max index is 4- To get all the elements use a for loop

for (int i=0; i < 5; i++)

println(years[i]);

– How many elements in an array?• Arrays have a local variable called length: arrayname.length (that’s a dot)for (int i=0; i < years.length; i++)

println(years[i]);

• What happens if you use <= ?for (int i=0; i <= years.length; i++)

println(years[i]);

Page 7: Introduction to programming - class 11

Creating arrays of fixed length• How do you create an array of given length and then fill it? Use new!

int[] randomNums = new int[100];

// note the keyword new, repetition of the int type, and number of elements, 100, in [], i.e. [100]

// fill with 100 random numbers between 0 and width

for (int i=0; i < randomNums.length; i++)

randomNums[i] = random(0, width);

// print them out

for (int i=0; i < randomNums.length; i++)

println(randomNums[i]);

• More complex arrays are possible– for multidimensional arrays (e.g. Screen locations, A chess board, etc):

String[][] chessBoard = new String[8][8];

chessBoard[4][0] = “White King”;

// start position for white king is col 5, row 1

– Variable length arrays that change length

• Text book– Functions: chapter 8, Ex 8.1-8.8 (Dice and Owls)– Arrays: Chaper 10, Ex 10.1-10.9

Page 8: Introduction to programming - class 11

Quiz 21. My name is:

2. In Processing (and Java) there are several different ways of repeating commands (loops).

Are the following “while” and “for” loops are the same: True or False

// a “while” loop

int i = 0;

while (i < 10)

{

ellipse(10,10,i,i);

i++;

}

// a “for” loop

for (int i=0; i < 10; i++)

ellipse(10,10,i,i);

Page 9: Introduction to programming - class 11

3 mousePressed is a global Boolean variable which has the value true after any

mouse button is pressed. Do these two code examples do the same thing? True or False

// example 1

if (mousePressed)

println(“Kaboom!”);

// example 2

if (mousePressed == true)

println(“Kaboom!”);

4 Remember that the test for equality is “==” (used in example 2 in Qn 3); and

assignment (setting the value of a variable) is “=” (used in example 3 below). Does example 3

do the same as example 1 above? True or False (Hint: Can mousePressed ever be false?)

// example 3

if (mousePressed = true)

Println(“Kaboom!”);

Page 10: Introduction to programming - class 11

5 Image file formats. Which of the following is not an image file format?a. PNGb. JPEGc. SVGd. WWWe. GIF

6 Which image file format above can compress large photo files the most (i.e. makes them smallest?)

7 Which image file format above smoothly scales Vector Graphics (shapes and lines) up and down to any size?

8 The internet (networked computers allowing many different programs to communicate) has been around since 1960s? 1970s? 1980s? 1990s?

Page 11: Introduction to programming - class 11

9 The World Wide Web (WWW) was invented in the 1990s? True of False

10 Which of the following protocols and programs are parts of the World Wide Web (WWW)? Circle them:

a. URLs (user friendly internet addresses)b. Domain Name Servers (DNS) – a “phonebook” for URLsc. HyperText Markup Language (HTML) - used to make web pagesd. HyperText Transfer Protocol (HTTP) - so web browsers and servers can communicatee. Web Serversf. Web Browsers (e.g. Chrome, Firefox, etc).

Page 12: Introduction to programming - class 11

1 Complete the following Truth Table (fill in the gaps) for the Logical Operation:

a. NOT (true if p is false, false if p is true). T is True, F is False:

p NOT p

T F

F

Page 13: Introduction to programming - class 11

1 Complete the following Truth Table for the Logical Operation:

a. AND (true if both p and q are true). T is True, F is False:

p q p AND q

T T

T F F

F T

F F

Page 14: Introduction to programming - class 11

1 Complete the following Truth Table for the Logical Operation:

a. NAND (NOT AND). false if both p and q are true, else true.

b. Hint: Just the same as NOT (p AND q):

p q p NAND q

T T

T F T

F T

F F

Page 15: Introduction to programming - class 11

14 In Processing, which of these Logical Operators are: AND, OR, NOT?

|| ________?

! ________?

&& ________?

15 In the following Processing example, what value is dontdrinkanddrive? true or

false

// remember, boolean variables are either true or false

boolean drink = true;

boolean drive = true;

boolean dontdrinkanddrive = !(drink && drive);

Page 16: Introduction to programming - class 11

16 In the following Processing example, what value is dontdrinkanddrive? true or

false

// remember, “||” means one or more are true

boolean beer = false;

boolean wine = false;

boolean spirits = false;

// is drink true?

boolean drink = (beer || wine || spirits);

boolean bike = true;

boolean car = false;

boolean truck = false;

// is drive true?

boolean drive = (bike || car || truck);

boolean dontdrinkanddrive = !(drink && drive);

17 Are these two “signs” the same (assuming that a value of true is obeying the sign):

Don’t Drink and Drive

Drink NAND Drive

Page 17: Introduction to programming - class 11

18 Can you work out what this program does? What happens when the ball hits the edges of the screen? Does it wrap around or

bounce? There is a bug! The ball vanishes off the top of the screen. How can you fix it?

float diameter = 40;

float radius = diameter/2;

float x;

float y;

float xspeed = 5;

float yspeed = 7;

void setup() {

size(500, 500);

x = width/2;

y = height/2;

}

void draw() {

background(0);

x += xspeed;

y += yspeed;

if ( (x + radius) > width || (x - radius) < 0 )

xspeed = -xspeed;

if ( (y + radius) > height )

yspeed = -yspeed;

ellipse(x, y, diameter, diameter);

}