Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lesson 2: Building Blocks of Programming

Similar presentations


Presentation on theme: "Lesson 2: Building Blocks of Programming"— Presentation transcript:

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

2 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

3 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; }

4 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!

5 (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 = ; 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

6 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”)

7 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.

8 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)

9 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.

10 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

11 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

12 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

13 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.

14 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.

15 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.

16 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

17 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

18 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; } }

19 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?

20 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 us. > Zarrin: >Carol: See you guys next week!


Download ppt "Lesson 2: Building Blocks of Programming"

Similar presentations


Ads by Google