Lesson 2: Building Blocks of Programming

Slides:



Advertisements
Similar presentations
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Advertisements

Written by: Dr. JJ Shepherd
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
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.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
Week 2 - Monday.  What did we talk about last time?  Software development  Lab 1.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
CSE 501N Fall ‘09 12: Recursion and Recursive Algorithms 8 October 2009 Nick Leidenfrost.
Agenda Object Oriented Programming Reading: Chapter 14.
Variables, Primitives, and Objects A Visual Learner’s Guide.
ICAPRG301A Week 2 Strings and things Charles Babbage Developed the design for the first computer which he called a difference engine, though.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
© A+ Computer Science - A reference variable stores the memory address of an object. Monster fred = new Monster(); Monster sally.
CSCI 1226 FALL 2015 MIDTERM #1 REVIEWS.  Types of computers:  Personal computers  Embedded systems  Servers  Hardware:  I/O devices: mice, keyboards,
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
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.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Coding – Week 2 Functions, Arrays, and Objects. Functions  Functions are not a new concept – you’ve been using them already.  void setup() {} and void.
Eastside Robotics Alliance / Newport Robotics Group 1 T/Th, 6:30 – 8:30 PM Big Picture School Day 3 · 10/9/2014.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Basic concepts of C++ Presented by Prof. Satyajit De
Week 2 - Wednesday CS 121.
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
User Interaction and Variables
Chapter 2 Basic Computation
Java Coding – part 2 David Davenport Computer Eng. Dept.,
A basic tutorial for fundamental concepts
Lecture 5: Some more Java!
Agenda Warmup AP Exam Review: Litvin A2
EGR 2261 Unit 4 Control Structures I: Selection
Debugging and Random Numbers
Multiple variables can be created in one declaration
Variables and Primative Types
Variables and Arithmetic Operators in JavaScript
Agenda Warmup Lesson 2.5 (Ascii, Method Overloading)
Programming – Touch Sensors
Lecture 11 B Methods and Data Passing
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
CS2102: Lecture on Abstract Classes and Inheritance
Structures Lesson xx In this module, we’ll introduce you to structures.
Phil Tayco Slide version 1.0 Created Oct 2, 2017
Object Oriented Programming (OOP) LAB # 8
Lesson 4: Overall Review and Assessment
Variables ICS2O.
Lesson 1: Fundamentals of Programming
Coding Concepts (Data- Types)
Building Java Programs
Fall 2018 CISC124 2/15/2019 CISC124 TA names and s will be added to the course web site by the end of the week. Labs start next week in JEFF 155:
State Machines 6-Apr-196-Apr-19.
Introduction to Primitives
Introduction to Primitives
Java Programming Language
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
CS2013 Lecture 7 John Hurley Cal State LA.
Review: libraries and packages
Welcome back! October 11, 2018.
References Revisted (Ch 5)
Object-Oriented Design AND CLASS PROPERTIES
Methods and Data Passing
Intro to Programming (in JavaScript)
Building Java Programs
Presentation transcript:

Lesson 2: Building Blocks of Programming Fe Maidens Programming 2017-2018 Welcome back! Remember to encourage note taking → mention the assessment in 2 weeks

Review of Last Week Last week we learned a lot of important concepts in programming. Do you remember them? What are the only values a boolean can store? Work in groups of two or three to write a conditional where we eat if we are hungry, or study if we’re not. (5 ish min group activity) Walk around and see how people are doing. Ask one group to write theirs up on the board. Doesn’t have to be in Java syntax, can be pseudocode. Ideal answer is: if (hungry == true) { Eat food } Else { study

Robot Code (from lesson one) Here’s the real code we used to make the robot shoot the ball. Robot.cannon.spinWheels(-v, -v); if (RobotMap.turningCam == true) { Robot.cannon.turnCam(); RobotMap.turningCam = false; }

What is code? Code is a way to interface with computers, as we talked about last week. Code is made out of information and processes: We store information using variables. Variables are just like the ones you see in math! They’re a name that represents a value, like “x = 4”. Every variable has a type. These are either “primitives” - the fundamental stuff, like numbers and letters - or “classes” - more complicated things like shapes. You’ve seen processes before: they’re the methods we wrote last time!

(int stands for integer.) Variables Let’s talk a little bit more about variables. When you make a variables, you have to tell the computer what type it is and what it’s name is. This is called declaration. Then, you give it a value. When you give a variable a value, you instantiate it. Here’s an example: public int x = 6; type (int stands for integer.) name value Point out use of type to transition into different types, also mention we’ll go into more depth about this a little further on

Variable Types Different kinds of variables are used for different things. Here are a couple of the most used ones. boolean - stores a true or false value int - stores an integer number (4, 7, 2265) double - stores decimal numbers (3.0, 10.5, 22.65) char- stores single characters (‘a','b','c') string - stores a word or multiple characters (“Hello”, “It is October 11”)

Public vs Private While declaring your variables, you would write “public” before the type. We did the same thing when creating our methods and classes. But what does “public” mean? When we create a public variable or class in Java, we are allowing other classes in the same project to have access to the code inside the class. Private classes keep its variables and methods hidden. Sometimes, we use “private” in cases where we have the same variable name or method name in different classes. In methods, when declaring a variable, we didn’t write public or private. This is because the method variable will be public or private based on the method’s status.

Making Variables Earlier, we showed you how to declare and instantiate an integer. It looked like this: public int x = 6; Let’s try to do the same with other types of variables. Work in groups to try to create a variable of each type: boolean, int, double, char, and string. 5-10 min, have ALL groups go up to put one example up on the board. We’ll go over as a group. See if anyone asks questions about the word “public” or just accept it (will go over public vs private later on)

public int sum(int num, int num2){ Methods Last week, we used a method to make the robot shoot a ball. But we can use that idea and apply it to any programming problem! We use methods when we want to be able to perform a set of prewritten steps. This is how we create a method: public int sum(int num, int num2){ [code goes here] } Let’s take a look at each part of this.

Writing Methods public int sum (int num, int num2) The return type of a method is what the method will give back to us when we call it. In this case, it will return an int that will be the sum of num and num2. The return type can be any of the variable types we just learned about. Methods can also do actions and not return a value to us. When we don’t want anything to be returned, we write: public void [name]([parameters]) Return type name parameters

Writing Methods (cont.) public int sum (int num, int num2) The name of a method should be short and concise, but still describe the purpose of the method. Methods typically have the same naming convention as variables do. We can use camelCase for the name if we use more than one word. Return type name parameters

Writing Methods (cont.) public int sum (int num, int num2) Parameters are a special type of variable that are specific to methods and are always in parentheses. These parameters are used inside the methods so the same actions can be taken on different values. If the method doesn’t use parameters, the parentheses still need to be present. The code inside this method would read: public int sum(int num, int num2){ return num + num2; } Return type name parameters

public int sum(int num, int num2){ Variables in Methods You can put as many lines of code into methods as you need, but it might get hard to keep track of. Some methods can get really complicated, so we use variables to make things simpler for us. For example, we can use a variable in the method from the previous slides. public int sum(int num, int num2){ int sum = num + num2; return sum; } Draw attention to how if we return a variable, it must have the same type as the method’s return type, or we willg et an error.

Calling Methods To use our methods, we need to call it from somewhere else. When we call a method, we use its name followed by values for the specified parameters in parentheses. For example, to call the method we just discussed, we would write: sum(10,3). If we had a method that took no parameters, we would include empty parentheses. Now let’s try to write our own methods! Try to write a method that finds the difference of two doubles, or the product of two ints. 10 min. Have one or two groups write their method on the board, and have the others figure out how to call the method.

Group Activity Make a method that finds the circumference of the circle. Make a method that finds the area of the circle. Make a boolean method that determines whether you should bring an umbrella or not.

Classes When using a lot of variables and methods, it can get confusing really quickly. Let’s say you wanted to have information about a parallelogram, with variables for the length of sides and angles, and methods to find perimeter and area. To make coding easier, we can use classes, which act as a blueprint and can store many variables and methods. public class Parallelogram { [variables and methods go here] } Mention that we won’t be going super in depth about classes → too complicated. Will just go over the basics

Basics of Classes In our classes, we can create variables that the methods in the class can use. public class Parallelogram{ int side1; int side2; public int getPerimeter() { return side1 + side2; } { Briefly mention constructors as how we pass the class the values for these variables, but will not be going in depth about it in precut lessons

Class Constructors Constructors tell the computer how to make a new version of the object we want. They're a part of almost every class. Constructors are run when you use the word new to describe an object. public class Parallelogram { int side1, side2, angle1, angle2; public Parallelogram(int s1, int s2, int a1) { side1 = s1; side2 = s2; angle1= a1; angle2=180-a1; } }

Any Questions? Conclusions We introduced a lot of new information today. We talked about: Variables Types of variables Writing and using variables Methods Writing and using methods Classes Basics of using variables and methods Public vs Private Any Questions?

What’s next: Our next meeting will be next Monday October 16th, right here after 9th period. Next week you’ll start learning about the specific parts of the robot that we can control before writing actual robot code. We’ll also start writing real robot code, and we’ll show you the robot code we used in the past. The things we learned today are going to be really important, but we know it was a lot to handle, so if you have any questions, you can email us. > Zarrin: aliz@bxscience.edu >Carol: shaot@bxscience.edu See you guys next week!