22
Introduction to programming Class 2 Paul Brebner

Introduction to programming - class 2

Embed Size (px)

Citation preview

Page 1: Introduction to programming - class 2

Introduction to programmingClass 2

Paul Brebner

Page 2: Introduction to programming - class 2

A current “large” computer

• Remember Colossus? Early, big, expensive, programmable, but special purpose (code breaking) computer?

• D-Wave, also a big, expensive programmable special purpose (optimisation) computer

– http://www.dwavesys.com/

Page 3: Introduction to programming - class 2

$20M, = to 10th ranked supercomputersupercooled, only a few in use

Page 4: Introduction to programming - class 2

Finds best solution “instantly”

• Processing with D-Wave– A lattice of 512 tiny superconducting circuits, known as qubits, is

chilled close to absolute zero to get quantum effects– A user models a problem into a search for the “lowest point in a

vast landscape”– The processor considers all possibilities simultaneously to

determine the lowest energy required to form those relationships

– Multiple solutions are returned to the user, scaled to show optimal answers

• What will things be like in 50 years time? A quantum computer in your pocket.

Page 5: Introduction to programming - class 2

Travelling Sales Person problem

• Visit every city only once, with shortest path

– Very hard to solve, and gets harder > cities

– Fastest supercomputer = 30 minutes

– D-Wave = ½ second

Page 6: Introduction to programming - class 2

Revision

• How far did you get with the Exercises from Class 1?

• Process “Exhibition” examples

– A few more

• http://processing.org/exhibition/works/starnursery/index_link.html (Needs Quicktime) Software for the R.E.M. video Animal (youtube, screenshot next slide)

• http://processing.org/exhibition/works/yellowtail/index_link.html

Page 7: Introduction to programming - class 2
Page 8: Introduction to programming - class 2

Your favourites?

• ???

Page 9: Introduction to programming - class 2

Processing progress

• Activity 2– Can you find your way around Processing

Environment ok? Examples? Reference? Tutorial?

– Running programs?

– Statements and comments

– Setup() and draw()

– Variables

– Basic/primitive data types

– Any questions?

Page 10: Introduction to programming - class 2

Statements and Comments.

• Statements are the elements that make up programs. The ";" (semi-colon) symbol is used to end statements. It is called the "statement terminator."

• Comments are used for making notes to help people better understand programs. A comment begins with two forward slashes ("//").

• // The size function is a statement that tells the computer // how large to make the window. // Each function statement has zero or more parameters. // Parameters are data passed into the function // and are used as values for telling the computer what to do.size(640, 360);// The background function is a statement that tells the computer// which color (or gray value) to make the background of the display windowbackground(204, 153, 0);

Page 11: Introduction to programming - class 2

Variables

• A named place to store something – what?– The Type of the thing.

• Syntaxtype name; // undefined initial valuetype name = value; // assign an initial value.Note that “=“ means “put the value into the name” (not

equals which is “==“)!

• Examplesint numberOfPizzas = 12; // int are 32 bit integersboolean iLikePizza = true; // boolean is true or false (maybe)char theFirstLetterOfTheAlphabet = ‘a’; // “not a String”float howMuchOfThePizzaIGot = 0.3333333; // 32bit decimal

Page 12: Introduction to programming - class 2

Variables (contd)

• You must assign a value to a variable before you use itint pizzasIHave; // no valueint pizzasYouHave = pizzasIHave – 3; // error

• You must declare a variable before you try and use it (mostly)int pizzasYouHave = pizzasIHave – 3; // errorint pizzasIHave = 99;

height and width are pre-declared system variables for screen height and width (see Reference/Environment)

• You can’t declare the same variable more than once (mostly)int pizzasIHave = 99;int pizzasIHave = 100; // error

Page 13: Introduction to programming - class 2

Using variables

• Use wherever a non-variable can be used e.g.int pizzasIHave = 99;int pizzasYouHave;pizzasYouHave = pizzasIHave – 0;pizzasIHave = 100;pizzasYouHave = pizzasIHave – 99;

• Can only assign variables/values of the correct Type (mostly) – they don’t “fit”!int pizzasIHave = ‘x’; // errorint pizzasIHave = 99.0; // errorint pizzasIHave = true; // errorfloat howMuchOfThePizzaIGot = 1; // is 0K, type cast to 1.0

Page 14: Introduction to programming - class 2

Processing progress

• Activity 2 part 3– Did you manage to install Processing environment?

School? Home?

– How far did you get with Tutorials 1 & 2?

• We will using selected parts of a Processing text book:– Getting Started with Processing, by Reas and Fry

– This course was based on Processing V2.

– Note: This is available as an ebook:

– http://shop.oreilly.com/product/0636920031406.do

Page 15: Introduction to programming - class 2
Page 16: Introduction to programming - class 2

PAGE 9

Example 2-1: Draw an Ellipse

In the editor, type the following:

ellipse(50, 50, 80, 80);

This line of code means “draw an ellipse, with the center 50 pixels overfrom the left and 50 pixels down from the top, with a width and height of80 pixels.” Click the Run button.

If you’ve typed everything correctly, you’ll see the ellipse image above. Ifyou didn’t type it correctly, the Message Area will turn red and complainabout an error. If this happens, make sure that you’ve copied the examplecode exactly: the numbers should be contained within parenthesesand have commas between each of them, and the line should end with asemicolon.

One of the most difficult things about getting started with programmingis that you have to be very specific about the syntax. The Processingsoftware isn’t always smart enough to know what you mean, and can bequite fussy about the placement of punctuation. You’ll get used to it with alittle practice.

Page 17: Introduction to programming - class 2

ellipse(50, 50, 80, 80);

This line of code means ... Oh, I’ve forgotten already.

Remembering what the parameters mean and the order may be hard?

Easier to declare some variables and use them?

int positionAcross = 50; // x posint positionDown = 50; // y posint ellipseWidth = 80;int ellipseHeight = 80;

ellipse(positionCenter, positionDown, ellipseWidth, ellipseHeight);

Page 18: Introduction to programming - class 2

More programming examples

• PAGE 10 Ex 2.2– Add 2 variables for white (255) and black (0).– Where do mouseX and mouseY come from?– You can make special variables called CONSTANTS than can only be assigned to once and never

change their value• final int WHITE = 255;• final int BLACK = 0;• final int GREY = WHITE/2;• Try changing their value after first assignment...?• E.g. WHITE = BLACK; // ???

• PAGE 14 top, right-click on orange text in editor and get help!• Chapter 3 Draw

– X and y coordinates– Functions and parameters

• Page 16 Ex 3.1– size()

• Page 16 Ex 3.2– point()– Use the height and width system variables if you like

Page 19: Introduction to programming - class 2

More programming examples

• Basic shapes– Do up to Ex 3.7

• Drawing order Page 22 Ex 3.9-3.13– Processing programs executed in order– Shapes drawn later are drawn over the top of earlier shapes

• Colour (Page 26)– Gray scales (white = 255, black = 0)– Ex 3.14, Ex 3.15– Page 28 Ex 3.16 Colour (RGB) Additive colour model see next slide

• What’s the difference between fill(255) and fill(255, 255, 255)?

– Try the colour selector• What’s pure red? Pure green? Pure blue? How do you get orange? Yellow? Purple?• Color is a Type in Processing (remember it was invented by Americans so NOT COLOUR)

– color orange = color(255, 132, 0);– fill(orange);

• Try making some fruit and vegetable color variables and displaying them all on the screen• E.g. Orange, tomato, apple, watermelon, grape, banana, etc.

– Ex 3.17 transparency

Page 20: Introduction to programming - class 2
Page 21: Introduction to programming - class 2

Can you draw a bowl? Hint: P. 20 arc() functionAnd for your next trick... (Next slide)

Page 22: Introduction to programming - class 2

Giuseppe Arcimboldo an Italian painter best known for portrait heads made entirely of such objects as fruits,

vegetables, flowers, fish, and books