Computer Science I Variables. Methods. Classwork/Homework: Re-do or do new drawing using variables & function(s). Zip folder and upload to Moodle dropbox.

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

Variables in C Amir Haider Lecturer.
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Animation Mrs. C. Furman. Animation  We can animate our crab by switching the image between two pictures.  crab.png and crab2.png.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
CS 201 Functions Debzani Deb.
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.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Visual C++ Programming: Concepts and Projects Chapter 6A: Methods (Concepts)
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Introduction to Python
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
1 Please switch off your mobile phones. 2 Data Representation Instructor: Mainak Chaudhuri
The Java Programming Language
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CSE 131 Computer Science 1 Module 1: (basics of Java)
Section 4 - Functions. All of the programs that we have studied so far have consisted of a single function, main(). However, having more than one function.
Procedural programming in Java Methods, parameters and return values.
1 CS161 Introduction to Computer Science Topic #9.
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Programming Games Logic. Slide show. Range input. Storage. Datatypes. Binary numbers. Homework: Catch up. This includes uploading projects to your server.
Variables and Functions Alice. Naming is Important If you get a new pet one of the first things you do is name it Gives you a way to refer to the new.
Computer Science I Classes and objects Classwork/Homework: Examine and modify my examples. Make your own.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
Computer Science I Programming in Java (programming using Processing IN Java, using IntelliJ IDE) Classwork/Homework: copy your Processing projects over.
Computer Science I Recap: variables & functions. Images. Pseudo-random processing.
Computer Science I Looping. User input. Classwork/Homework: Incorporate looping & user input into a sketch.
Side effects A side effect is anything that happens in a method other than computing and/or returning a value. Example: public class hello { public int.
Methods: functions & procedures. Top-down programming Divide-and-conquer technique Divide-and-conquer technique Problem is broken down into a series of.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
Programming games Drawing logo. Expressions. Formulas. Credit cards. Form validation. Homework: your credit card program. Continue uploading work.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Computer Science I Text input. Transformations. Yet another jigsaw. Classwork/Homework: Create new application making use of transformations.
Computer Science I Animations. Bouncing ball. The if statement. Classwork/homework: bouncing something. Compress and upload work to Moodle.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Computer Science I More class examples. Paths. Jigsaw. Tolerance. Classwork/homework: Your class project. Post proposal for midterm project.
Time to upload Virtual something.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Pass-by-Value - default passing mechanism except.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Copyright © Texas Education Agency, Computer Programming Variables and Data Types.
Chapter 1.2 Introduction to C++ Programming
Whatcha doin'? Aims: To start using Python. To understand loops.
Chapter 1.2 Introduction to C++ Programming
Classwork: Examine and enhance falling drop(s) or make your own.
Computer Science I Variables. Methods.
Introduction to Computer Science / Procedural – 67130
Multiple variables can be created in one declaration
Primitive Data, Variables, Loops (Maybe)
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Computer Science I Fonts. Building on one jigsaw to make another.
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Programming games Classes and objects (used for Jigsaw, Bouncing stuff, other projects) Homework: Complete cannonball. Video or Audio. Your own project.
2009 Test Key.
Variables in C Topics Naming Variables Declaring Variables
Variables and Constants
Introduction to Methods and Interfaces
Presentation transcript:

Computer Science I Variables. Methods. Classwork/Homework: Re-do or do new drawing using variables & function(s). Zip folder and upload to Moodle dropbox.

Extending the language Programming languages have facilities to extend the language: Variables: data Methods: code Later Classes: defining objects, which are ways of connecting code and data (called methods & variables)

Terminology In Processing, which is based on Java, the proper term is method. – We will talk about classes, which define objects, which have variables and methods later. I may slip and call them functions. There also is distinction in some, but not all programming languages, between – Function: calculates a value – Procedure or subroutine: performs a task

Angles How to measure angles? – Familiar system: 360 degrees in a circle. This was invented. It is arbitrary. Extra credit opportunity to post history. – Intrinsic measure: radians. Recall: the length of circumference of a circle is 2* pi *radius. Think of wrapping the radius around the circle. Processing provides built-in constants PI represents a half circle. We know this as 180 degrees TWO_PI corresponds to 360 degrees. QUARTER_PI to 45 degrees. HALF_PI to 90 degrees

Demonstrate variables with example // smiley face void setup() { size(800,600); ellipseMode(CENTER); } void draw() { ellipse(100,100,80,100); fill(0); ellipse(85,90,10,10); ellipse(115,90,10,10); noFill(); arc(100,110,40,30,QUARTER_PI,3*QUARTER_PI); }

Let's go to Processing Copy over smiley Test, enhance, test While we have it, let's save – Network drive, if you have one – Otherwise, "local" folder. Save with new name.

But What if I wanted to change something? What are all these numbers? What if I wanted to draw two faces? What if I wanted to change the face or draw 2 or more different faces?

Variables Variable is a construct for associating a name with a value. You can change the value. It can vary. In Processing, you DECLARE the variable. – This requires specifying what the datatype of the variable will be. Is it an integer (whole number)? Is it a number with a fraction part? Is it a string of characters? Is it a true or false value? Other? – Then use the name in place of the value. I will show the code and talk more about this later.

Example of use Expressions are combinations of literal values and variables. For example, recalling radians – Assume angle is a variable holding some angle in degrees, then angle * PI / 180 produces value in radians.

Function Processing provides a way to specify code. A Processing function has a signature line that indicates what parameters the function has and what value it returns. Again, I will demonstrate these concepts in the smiley example.

Planning Make variables out of all the values (numbers) in the example. Define a method called smiley.

Variables int ctrx, ctry; int faceWidth, faceHeight; int eyeXoffset, eyeYoffset; int eyeSize; int mouthYoffset, mouthWidth, mouthHeight; I make these all integers. I could (and maybe should) make them float.

What values for these variables? Notice just one eyeXoffset value. Why? It occurred to me that these values are not independent. Some are based on other values, namely faceWidth and faceHeight. Show first improved version. Make it draw 2 faces. Then improve it further.

So will set eyeXoffset equal to an expression based on faceWidth. I came up with the numbers 15/80. Improve it if you wish! Need to make sure it is an integer The declaration of eyeXoffset is int eyeXoffset = int((15.0/80.0)*faceWidth);

int ctrx = 100; int ctry = 100; int faceWidth = 80; int faceHeight = 100; int eyeXoffset = int((15.0/80.0)*faceWidth); int eyeYoffset = int(.10*faceHeight); int eyeSize = 10; int mouthYoffset = int(.10*faceHeight); int mouthWidth = int(.5*faceWidth); int mouthHeight = int(.3*faceHeight);

void setup() { size(800,600); ellipseMode(CENTER); } void draw() { smiley(ctrx,ctry); }

void smiley(int x,int y) { ellipse(x,y,faceWidth,faceHeight); fill(0); ellipse(x-eyeXoffset, y-eyeYoffset,eyeSize,eyeSize); ellipse(x+eyeXoffset, y-eyeYoffset,eyeSize,eyeSize); noFill(); arc(x, y+mouthYoffset, mouthWidth,mouthHeight, QUARTER_PI,3*QUARTER_PI); }

Claim: this an improvement! More typing but much clearer what is going on. No more naked numbers (aka literals). And, what if you wanted to draw 2 faces? – Same face Did need to fix bug: reset fill to white. Notice comments! – Change dimensions – Now significant saving in code

The smiley method is called twice void draw() { smiley(ctrx,ctry); smiley(2*ctrx, 2*ctry); } void smiley(int x,int y) { fill(255); //reset fill back to white ellipse(x,y,faceWidth,faceHeight); fill(0); // set fill to black ellipse(x-eyeXoffset,x-eyeYoffset,eyeSize,eyeSize); ellipse(x+eyeXoffset,x-eyeYoffset,eyeSize,eyeSize); noFill(); //no fill for mouth arc(x,y+mouthYoffset,mouthWidth,mouthHeight,QUARTER_PI,3*Q UARTER_PI); }

Change dimensions of face Need to include width and height as parameters to smiley function. Put code for setting related values into smiley Need to move some variables into smiley – This makes them LOCAL variables, as opposed to GLOBAL.

int ctrx = 100; int ctry = 100; int faceWidth = 80; int faceHeight = 100; int skinnyFaceWidth = 60; int skinnyFaceHeight = 130; int eyeSize = 10;

void setup() { size(800,600); ellipseMode(CENTER); } void draw() { smiley(ctrx,ctry,faceWidth, faceHeight ); smiley(3*ctrx, 3*ctry,skinnyFaceWidth, skinnyFaceHeight ); }

void smiley(int x,int y, int w, int h) { int eyeXoffset = int((15.0/80.0)*w); int eyeYoffset = int(.10*h); int mouthYoffset = int(.10*h); int mouthWidth = int(.5*w); int mouthHeight = int(.3*h); fill(255); ellipse(x,y,w,h); fill(0); ellipse(x-eyeXoffset,x- eyeYoffset,eyeSize,eyeSize); ellipse(x+eyeXoffset,x- eyeYoffset,eyeSize,eyeSize); noFill(); arc(x,y+mouthYoffset,mouthWidth,mouthHeight,QUARTER_ PI,3*QUARTER_PI); }

Admission In my first version of the program drawing two faces, I neglected to reset fill. This was a semantic error: an error of meaning. The program worked but didn't do what I wanted. A syntactic error is an error of form: like a grammatical error. The program will not run at all and Processing will tell me something.

Recap: variable, method Variable is programming construct to associate a name with a value. Variables need to be declared. Can be assigned values. A method is defined with a header (aka signature line) specifying return value and parameter values. IN the body of the method, the parameters values are used along with any local variables and global variables.

Recap: statements Assignment statement – An assignment statement uses a single = sign. Think of make the left hand variable equal to the current value of the expression on the right. Preview/warning: there also is a double equal sign that means: check if these two values are equal. – target = expression ; Declaration statement method definition method invocation (aka method call)

Note Some methods do not return any value. – void setup() { } – All our methods so far don't return values. Some methods compute a value and return it to where the method is invoked – total = balance + tax(balance); assumes there is a method named tax that has input for which it uses the name balance and computes based on the input

Classwork/Homework Take your drawing(s) from last class OR make new drawings Modify by using variables and your own methods. Save As – This creates a folder – Use Save for subsequent saving, but Do consider saving with new names – When you are satisfied, Save As using naming convention. Compress the folder Upload to moodle dropbox. – Will demonstrate.