Coding – Week 2 Functions, Arrays, and Objects. Functions  Functions are not a new concept – you’ve been using them already.  void setup() {} and void.

Slides:



Advertisements
Similar presentations
PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists.
Advertisements

Inheritance Writing and using Classes effectively.
Lecture 1: Comments, Variables, Assignment. Definitions The formal (human-readable) instructions that we give to the computer is called source code The.
Lesson Four: More of the Same
Q and A for Sections 1 – CS 106 © 2014 Victor Norman.
Data: Programming Design and Modularization IS 101Y/CMSC 101 Computational Thinking and Design Thursday, September 26, 2013 Marie desJardins University.
16-May-15 Sudden Python Drinking from the Fire Hose.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Week 10: Objects and Classes 1.  There are two classifications of types in Java  Primitive types:  int  double  boolean  char  Object types: 
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
CIT 590 Intro to Programming Java lecture 4. Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private,
ICM Week 2. Structure - statements and blocks of code Any single statement ends with semicolon ; When we want to bunch a few statements together we use.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
Loops We have been using loops since week 2, our void draw(){ } is a loop A few drawbacks of draw() –It is endless –There is only one draw() –It updates.
Lecture 3 IAT 800. Sept 15, Fall 2006IAT 8002 Suggestions on learning to program  Spend a lot of time fiddling around with code –Programming is something.
A tour around Java General introduction to aspects of the language (these will be covered in more detail later) After this tour you should have a general.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
CSE 380 – Computer Game Programming AI & Collision Strategy Erin Catto’s Box2D.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Week 14 - Monday.  What did we talk about last time?  Image manipulation  Inheritance.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Week 2 - Monday.  What did we talk about last time?  Software development  Lab 1.
11 Finding Winners Using Arrays Session 8.2. Session Overview  Find out how the C# language makes it easy to create an array that contains multiple values.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
CS 450: COMPUTER GRAPHICS PORTRAIT OF AN OPENGL PROGRAM SPRING 2015 DR. MICHAEL J. REALE.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
How to Create a Videogame By: Connor McCann. Java Java is one of many programming languages Java is used to run web browsers and most PC video games I.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Arrays BCIS 3680 Enterprise Programming. Overview 2  Array terminology  Creating arrays  Declaring and instantiating an array  Assigning value to.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (
CS107 References and Arrays By Chris Pable Spring 2009.
Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Looping and Counting Lecture 3 Hartmut Kaiser
Variables, Primitives, and Objects A Visual Learner’s Guide.
Pointers COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
Lesson 2: Reading a program. Remember: from yesterday We learned about… Precise language is needed to program Actors and Classes Methods – step by step.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
Written by: Dr. JJ Shepherd
GameDevClub CODE CHEAT SHEET NOTE: ALL OF THE CODE IS CASE-SENSITIVE AND THE SYNTAX IS STRICT SO A LOT OF YOUR ERRORS WILL PROBABLY COME FROM TYPOS If.
Week 9 - Wednesday.  What did we talk about last time?  2D arrays  Queen attacking pawn example  Started Game of Life.
Question of the Day  What three letter word completes the first word and starts the second one: DON???CAR.
1 CS161 Introduction to Computer Science Topic #15.
Data: Programming Design and Modularization IS 101Y/CMSC 101 Computational Thinking and Design Thursday, September 25, 2014 Carolyn Seaman Susan Martin.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Data Structures Arrays and Lists Part 2 More List Operations.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Programming Fundamentals. Today’s Lecture Array Fundamentals Arrays as Class Member Data Arrays of Objects C-Strings The Standard C++ string Class.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
Week91 APCS-A: Java Problem Solving November 2, 2005.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
User-Written Functions
Introduction To Repetition The for loop
Lesson 2: Building Blocks of Programming
RECURSION Haskell.
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Class code for pythonroom.com cchsp2cs
Presentation transcript:

Coding – Week 2 Functions, Arrays, and Objects

Functions  Functions are not a new concept – you’ve been using them already.  void setup() {} and void draw() {} are both functions, just Processing calls them automatically (at the beginning, and on every frame, of the program, respectively).  You can define your own functions, almost the same way, but they won’t be used automatically. You call them by writing the name of the function, followed by parentheses. void myFunction() { // Defines a function. // Set of instructions here (‘Do stuff’) } […] void draw() { // Defines your main loop, where all your code is run. myFunction(); // Runs the set of instructions (‘Does the stuff.’) }

Arguments  Remember: Everything is important.  Why the parentheses for functions? Because you can use arguments, which change the functions before you call them. void myFunction(string t) {// Defines the function text(50,50,t); } […] myFunction(“Draw this text”); // Calls the function  This does nothing but draw the text within the parentheses at. The set of instructions within the function uses the argument as a variable. You are setting that argument/variable when you call the function.

Functions that ‘Return’ Variables  The ‘void’ in a function definition just means this function won’t return any variables.  If a function does return a variable, it can be used in place of a variable of the class the function returns (integer, float, string, boolean, etc.)  If a function returns a variable, you write the class in place of ‘void’, and that function must return a variable of that class. int ReturnFive() { return 5; }  If used in place of a variable, whatever the function ‘returns’ will be used as the variable (i.e.: variable = ReturnFive() is identical to saying variable = 5)  You can do more complex operations to return more meaningful numbers, like mathematical functions that return the square root of something, etc.  This can all be combined. For example, you can use a return function as the argument of a function call ( example: GetSquareRoot( ReturnFive() ); )

Arrays  Remember what a variable is – an object that stores a single bit of information, like a number, a string of letters, a boolean value (true/false), etc. Think of it like a box.  An array is like a row of boxes, each holding a bit of information, exactly like a variable would. (Like this.)

More Arrays  The information arrays can store is just like those of variables. int[] numbers = new int[7]  Here we have an array with 7 indexes, which are all going to hold integers. This is almost identical to int number = 5. A variable is just a single ‘box’, holding one integer (in that case, 5).  We can set each ‘box’/index of the array, just like variables, like so: numbers[0] = 6;  This sets the 0 th space in the array.  Arrays start at 0, so one with 7 indexes/boxes will go from 0 – 6. Emphasis.  If you try to set a space that’s not in the array (i.e. not 0 – 6 ), you’ll get an error (so you can’t set numbers[7] in this example).

Arrays, Arrays, Arrays  For deciding what pill to take for the day: TakePill ( pills[dayofweek] ); TakePill() A function for taking a pill. pills[] the box of pills (an array with 7 indexes/boxes). dayofweek the day of the week, where 0 = Sunday, 1 = Monday, etc.  If ‘dayofweek’ didn’t start at 0, you would try to access pills[7] on Saturday, which gives you an error because pills[] only goes from 0-6. Then you’re off your meds, and before you know it you’re giving lectures about abstract concepts that don’t mean anything and nobody understands a word you’re saying, but they’re too shy to correct you.  This is what I wanted to show in the lesson on loops, but couldn’t: for (int i = 0; i < numbers.length; i++) { numbers[i] = 1; }  This will set every space in the array to 1.

I’m Gonna Talk About Arrays Until You Like It  This is useful for affecting multiple variables at once. For example, instead of: number1 = 1; number2 = 2; number3 = 3;  You can write: numbers[0] = 0; numbers[1] = 1; for (int i = 2; i < numbers.length; i++) { numbers[i] = numbers[i-1]+numbers[i-2]; // Assuming ‘numbers’ is an array of integers. }  This does the exact same thing, but you can make the array bigger or smaller, and it doesn’t matter if there are 3 variables, or 300. numbers.length is a variable that returns the size of the ‘numbers’ array automatically, so it will loop through all indexes/boxes in the array.  Yes, there totally are places where you would use this. Writing a function to affect every car on the streets in GTAV, for example. Games typically save all objects/instances of a class to arrays.

Object Classes  Wonder why I don’t give you an authoritative list of variable types (or classes)? Like integers, floats, strings, booleans, etc...  Because you can create your own.  These can be… well… anything. You could create a class for cars, NPCs, clouds of smoke, or fireballs… whatever could be described using discrete entities/instances.  Variables can hold instances of the objects you define. So you can define NPCs, and keep a list of NPC instances, and do things that affect every instance (like AI functions), etc.

Instances of Object Classes  An instance is just ‘an example or single occurrence of something’. You (as in you, person reading these words) are an instance of a human being. This is an instance of a lecture.  The object class is basically the abstract form these instances are derived from – the concept and definition of a ‘human being’ is different from the actual instances of human beings.  You (yes, you) do not define a human being, and what you do as a human is completely independent of other humans. To refer to you as ‘human’ just means you share in the class of human beings.  An instance is what a variable can contain. It won’t contain the entire object class.

Defining Object Classes  You define an object/class much like defining a function. class myObject {// Defining the class (note: class instead of void, // and no parentheses, because no arguments) int x, y; // Variables inside of your object. myObject(int a, int b) { // Instance constructor (a function; does take arguments) // (This defines how new instances are created! Think of it as // a function returning an instance) x = a; y = b;// Sets the x and y coordinates for the newly constructed // instance, using the arguments of the function. } void update() {// A function inside the object/class x = mouseX; y = mouseY; // Also sets the x and y coordinates }

Using Objects/Classes  myObject variable = new myObject(5,5);  This is going to store a new instance of the object in the variable.  The constructor is what is being used here. This is a function.  The arguments set the x and y of the new instance we’re creating, using the arguments.  You could then use variable.update(); to run the update() function within the object instance (indicated within the variable), which sets its x and y to the mouse coordinates. You can do this in the draw loop to have the instance update every frame, like everything else, or call the function at specific times (for example, if the object is an NPC/game character and its health reaches zero, you can call a function within it to destroy it).

Objects and Arrays  You can of course put objects into arrays, thus having an array of objects. myObject[] objects = new myObject[5]; // No instances are created yet.  This defines an array of the object class, which you can set the same as any array. objects[3] = new myObject(10,10); // Instance stored in array.  This sets the fourth space of the objects array to be a new instance of the object class ‘myObject’, with the arguments of its instance constructor being 10 and 10, to set its enclosed x and y coordinates to.  Read that carefully and stop me if makes no goddamn sense.  Seriously. Do it now.

That’s It.  That is all of the most basic fundamentals of code. All of them. Every one.  This was extremely quick, as you may have noticed. I wanted to show you how much you’ll have to deal with, and the full extent of what you’re expected to learn.  You can consider this ‘basics of programming’ – everything you need to get started. The rest is optional, but I would be tremendously happy to teach it.  We will continue to go through the Shiffman text, but it all hinges on this, which you can henceforth refer to as the Super Important Stuff.  Ask me about anything you have trouble with. You will not look stupid. Most classes take weeks to get through just one of these concepts.  That said, go to the lab and try this out. I’ll post these notes right away.