ACM init() Day 5

43
ACM init() Day 5: November 20, 2014

description

Slides from day 5 of ACM init()

Transcript of ACM init() Day 5

Page 1: ACM init() Day 5

ACM init()Day 5: November 20, 2014

Page 2: ACM init() Day 5

From last time• More about arrays

• Method

• Parameters

• Return Values

• Blocks

• How to use a block

• Parameters

Page 3: ACM init() Day 5

Area Methods HWI wanted you to make a program that uses methods to

calculate the area of various shapes. It was to be called like this:

Page 4: ACM init() Day 5

First stepWe called a method called "area", using the shape name and

two side lengths as arguments.

Page 5: ACM init() Day 5

Second stepThe area method then called a method that calculates and

prints the area of each shape.

Page 6: ACM init() Day 5

A new data typeWe know that an array is a list of variables. However, arrays are

indexed using numbers. There is another data type that can change this: a hash

A Hash is a dictionary-like collection of unique keys and their values. While an array uses integers as its index, a Hash

allows you to use any object type.

Page 7: ACM init() Day 5

Hash example

Page 8: ACM init() Day 5

Hash example continued

What will this print? Make your own hash!

Page 9: ACM init() Day 5

Hash syntaxIn general, a hash looks like this:

A hash consists of key-value pairs. Each key maps to a value.

Page 10: ACM init() Day 5

Creating a hashThere is more than one way to make a hash. We already saw

the first way: typing out key and value pairs in brackets.

How would we make an empty hash?

my_hash = Hash.new

This will create a completely empty hash. Make sure to capitalize Hash!

Page 11: ACM init() Day 5

Adding to a hashSay we create an empty hash called pets. Now we want to add

our pets to it!

Page 12: ACM init() Day 5

Accessing a hashWe can access elements of a hash just like elements of an

array.

Page 13: ACM init() Day 5

Your turn!

Create a hash called about_me. In the hash, put your name, your age, and your favorite color.

Page 14: ACM init() Day 5

AnswerThere are two ways to do this:

Page 15: ACM init() Day 5

Iterating over hashesLast week we went over how to iterate over arrays:

We can iterate over hashes in a very similar way.

Page 16: ACM init() Day 5

Iterating over hashesIterating over a hash is very similar to iterating over an array, but we have to print out two different things: the key and the

value.

Page 17: ACM init() Day 5

Prints out...

Page 18: ACM init() Day 5

Any questions on hashes?

Page 19: ACM init() Day 5

Now we get to do the cool stuff... Object Oriented

Programming.

Page 20: ACM init() Day 5

Object-Oriented Programming

So far in Ruby we've seen a lot of variables and data types.

Numbers, strings, booleans, arrays, hashes, etc.

We can do even more!

Page 21: ACM init() Day 5

Object-Oriented Programming

In Ruby, almost everything is an object. !

What is an object? Objects have methods, which you've seen before, and attributes, which are just data.

!We've even used them before without even knowing it! Take a

look.

Page 22: ACM init() Day 5

Strings as objectsWe have set strings to certain values before:

name = "Kelly" !

We can also use methods with strings: name.length

!In this case "name" is a string object with a .length method and

a length attribute of 5. !

"name" is now part of something called the string Class.

Page 23: ACM init() Day 5

What is a class?

A class is just a way of organizing and producing objects with similar attributes and methods.

Page 24: ACM init() Day 5

What is a class?We're going to build our own class. Let's think of a student.

Some things a student has: name

ID major

!Let's define a class called Student!

Page 25: ACM init() Day 5

Defining a class

First, we need to make a class for our student. Here's how to start:

Now we have an empty class!

Page 26: ACM init() Day 5

Initializing a classEvery class needs a method called initialize. When the class is

created, the initialze method will automatically be called. !

Create an empty initialize method in the Student class:

Page 27: ACM init() Day 5

Initializing a classWe wanted our student to have a name, an ID, and a major.

Let's do that!

Page 28: ACM init() Day 5

Initialzing a class

In Ruby, we use @ before a variable to signify that it's an instance variable. This means that the variable is attached to

the instance of the class. !

Any variable defined withing the initialize function must have an @ in front of it. This variable is now a member variable of

the class.

Page 29: ACM init() Day 5

Creating objects

Our class is defined enough to create an object now! Let's create a new student:

I just created a new Student object with name set to "Kelly", idNum set to "123456789", and major set to "CS".

Page 30: ACM init() Day 5

Creating objectsWe can create a bunch of Student objects! Each one will have

its own name, ID, and major.

Page 31: ACM init() Day 5

Let's make our Students do things

What does a student do? Let's add some methods to our Student class. A student can:

Say hi Take a test Freak out

Page 32: ACM init() Day 5

Adding methods to classes

We added a method called sayHi! Now how do we use it?

Page 33: ACM init() Day 5

Using classes

To call a method that is part of a class, just type in the name of the class variable, then a period, then the method name:

variable_name.method_name

Page 34: ACM init() Day 5

Using classes

I had a Student class called "me" and a student class called "other". Calling the "sayHi" method on both of them will print

out: !

Kelly says hi! Bob says hi!

Page 35: ACM init() Day 5

Let's add more methods!

Page 36: ACM init() Day 5

Let's add more methods!

Page 37: ACM init() Day 5

Using the new methodsThese new methods can be called the same way as the first

one! What will the following print out?

Page 38: ACM init() Day 5

Answer

Page 39: ACM init() Day 5

Let's create our own class together!

Page 40: ACM init() Day 5

Any questions on classes (so far)?

Page 41: ACM init() Day 5

What we did today• Hashes

• Creating hashes

• Putting information in hashes

• Iterating over hashes

• Classes

• Creating classes

• Putting variables and methods in classes

• Using methods in classes

Page 42: ACM init() Day 5

HomeworkOn the next page is a very simple game that uses a class called Player. I wrote the code to play the game for you, but you have

to write the class. You can write the methods in the class however you want, but all the methods that are in the loop I

wrote for you have to be present. !

The idea of the game is that two players take turns either attacking or healing. On each turn they can choose to attack the

other player or heal themselves. You should define a variable called health in your Player class to keep track of this. If you want you can make the game more interesting and add in

different types of attacks that have different effects on health.

Page 43: ACM init() Day 5