SlideShare a Scribd company logo
ACM init() 
Day 5: November 20, 2014
From last time 
• More about arrays 
• Method 
• Parameters 
• Return Values 
• Blocks 
• How to use a block 
• Parameters
Area Methods HW 
I wanted you to make a program that uses methods to 
calculate the area of various shapes. It was to be called like 
this:
First step 
We called a method called "area", using the shape name and 
two side lengths as arguments.
Second step 
The area method then called a method that calculates and 
prints the area of each shape.
A new data type 
We 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.
Hash example
Hash example continued 
What will this print? 
Make your own hash!
Hash syntax 
In general, a hash looks like this: 
A hash consists of key-value pairs. Each key maps to a value.
Creating a hash 
There 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!
Adding to a hash 
Say we create an empty hash called pets. Now we want to add 
our pets to it!
Accessing a hash 
We can access elements of a hash just like elements of an 
array.
Your turn! 
Create a hash called about_me. In the hash, put your name, 
your age, and your favorite color.
Answer 
There are two ways to do this:
Iterating over hashes 
Last week we went over how to iterate over arrays: 
We can iterate over hashes in a very similar way.
Iterating over hashes 
Iterating 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.
Prints out...
Any questions on 
hashes?
Now we get to do the cool 
stuff... Object Oriented 
Programming.
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!
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.
Strings as objects 
We 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.
What is a class? 
A class is just a way of organizing and producing objects with 
similar attributes and methods.
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!
Defining a class 
First, we need to make a class for our student. Here's how to 
start: 
Now we have an empty class!
Initializing a class 
Every 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:
Initializing a class 
We wanted our student to have a name, an ID, and a major. 
Let's do that!
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.
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".
Creating objects 
We can create a bunch of Student objects! Each one will have 
its own name, ID, and major.
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
Adding methods to classes 
We added a method called sayHi! Now how do we use it?
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
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!
Let's add more methods!
Let's add more methods!
Using the new methods 
These new methods can be called the same way as the first 
one! What will the following print out?
Answer
Let's create our own 
class together!
Any questions on 
classes (so far)?
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
Homework 
On 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.
ACM init() Day 5

More Related Content

Similar to ACM init() Day 5

python note.pdf
python note.pdfpython note.pdf
python note.pdf
Nagendra504676
 
Oz_Chap 2_M3_Lesson Slides_Variables.pptx
Oz_Chap 2_M3_Lesson Slides_Variables.pptxOz_Chap 2_M3_Lesson Slides_Variables.pptx
Oz_Chap 2_M3_Lesson Slides_Variables.pptx
ALEJANDROLEONGOVEA
 
Ruby objects
Ruby objectsRuby objects
Ruby objects
Reuven Lerner
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rdConnex
 
classes and objects.pdf
classes and objects.pdfclasses and objects.pdf
classes and objects.pdf
NeethuRajagopalan1
 
Chap 3 Python Object Oriented Programming - Copy.ppt
Chap 3 Python Object Oriented Programming - Copy.pptChap 3 Python Object Oriented Programming - Copy.ppt
Chap 3 Python Object Oriented Programming - Copy.ppt
muneshwarbisen1
 
Oops in PHP
Oops in PHPOops in PHP
Oops in PHP
Mindfire Solutions
 
06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt
ParikhitGhosh1
 
6 Object Oriented Programming
6 Object Oriented Programming6 Object Oriented Programming
6 Object Oriented Programming
Deepak Hagadur Bheemaraju
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
amiable_indian
 
Mongo and Harmony
Mongo and HarmonyMongo and Harmony
Mongo and HarmonySteve Smith
 
Ruby object model - Understanding of object play role for ruby
Ruby object model - Understanding of object play role for rubyRuby object model - Understanding of object play role for ruby
Ruby object model - Understanding of object play role for ruby
Tushar Pal
 
Ruby Interview Questions
Ruby Interview QuestionsRuby Interview Questions
Ruby Interview Questions
Sumanth krishna
 
201707 CSE110 Lecture 19
201707 CSE110 Lecture 19   201707 CSE110 Lecture 19
201707 CSE110 Lecture 19
Javier Gonzalez-Sanchez
 
Object concepts
Object conceptsObject concepts
Object concepts
Aashima Wadhwa
 
CSCI 200 Java Chapter 03 Using Classes
CSCI 200 Java Chapter 03 Using ClassesCSCI 200 Java Chapter 03 Using Classes
CSCI 200 Java Chapter 03 Using Classes
DanWooster1
 

Similar to ACM init() Day 5 (20)

python note.pdf
python note.pdfpython note.pdf
python note.pdf
 
Oz_Chap 2_M3_Lesson Slides_Variables.pptx
Oz_Chap 2_M3_Lesson Slides_Variables.pptxOz_Chap 2_M3_Lesson Slides_Variables.pptx
Oz_Chap 2_M3_Lesson Slides_Variables.pptx
 
Init() Day 4
Init() Day 4Init() Day 4
Init() Day 4
 
Ruby objects
Ruby objectsRuby objects
Ruby objects
 
Ruby object model
Ruby object modelRuby object model
Ruby object model
 
Presentation 3rd
Presentation 3rdPresentation 3rd
Presentation 3rd
 
classes and objects.pdf
classes and objects.pdfclasses and objects.pdf
classes and objects.pdf
 
Chap 3 Python Object Oriented Programming - Copy.ppt
Chap 3 Python Object Oriented Programming - Copy.pptChap 3 Python Object Oriented Programming - Copy.ppt
Chap 3 Python Object Oriented Programming - Copy.ppt
 
Oops in PHP
Oops in PHPOops in PHP
Oops in PHP
 
06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt
 
6 Object Oriented Programming
6 Object Oriented Programming6 Object Oriented Programming
6 Object Oriented Programming
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
 
Mongo and Harmony
Mongo and HarmonyMongo and Harmony
Mongo and Harmony
 
Ruby object model - Understanding of object play role for ruby
Ruby object model - Understanding of object play role for rubyRuby object model - Understanding of object play role for ruby
Ruby object model - Understanding of object play role for ruby
 
Ruby Interview Questions
Ruby Interview QuestionsRuby Interview Questions
Ruby Interview Questions
 
Object concepts
Object conceptsObject concepts
Object concepts
 
13 inheritance
13   inheritance13   inheritance
13 inheritance
 
201707 CSE110 Lecture 19
201707 CSE110 Lecture 19   201707 CSE110 Lecture 19
201707 CSE110 Lecture 19
 
Object concepts
Object conceptsObject concepts
Object concepts
 
CSCI 200 Java Chapter 03 Using Classes
CSCI 200 Java Chapter 03 Using ClassesCSCI 200 Java Chapter 03 Using Classes
CSCI 200 Java Chapter 03 Using Classes
 

More from UCLA Association of Computing Machinery

ACM init() Spring 2015 Day 1
ACM init() Spring 2015 Day 1ACM init() Spring 2015 Day 1
ACM init() Spring 2015 Day 1
UCLA Association of Computing Machinery
 
An Introduction to Sensible Typography
An Introduction to Sensible TypographyAn Introduction to Sensible Typography
An Introduction to Sensible Typography
UCLA Association of Computing Machinery
 
Building a Reddit Clone from the Ground Up
Building a Reddit Clone from the Ground UpBuilding a Reddit Clone from the Ground Up
Building a Reddit Clone from the Ground Up
UCLA Association of Computing Machinery
 
ACM Init() lesson 1
ACM Init() lesson 1ACM Init() lesson 1
ACM Fall General Meeting
ACM Fall General Meeting ACM Fall General Meeting
ACM Fall General Meeting
UCLA Association of Computing Machinery
 

More from UCLA Association of Computing Machinery (8)

ACM init() Day 3
ACM init() Day 3ACM init() Day 3
ACM init() Day 3
 
ACM init() Day 2
ACM init() Day 2ACM init() Day 2
ACM init() Day 2
 
ACM init() Spring 2015 Day 1
ACM init() Spring 2015 Day 1ACM init() Spring 2015 Day 1
ACM init() Spring 2015 Day 1
 
Intro to Hackathons (Winter 2015)
Intro to Hackathons (Winter 2015)Intro to Hackathons (Winter 2015)
Intro to Hackathons (Winter 2015)
 
An Introduction to Sensible Typography
An Introduction to Sensible TypographyAn Introduction to Sensible Typography
An Introduction to Sensible Typography
 
Building a Reddit Clone from the Ground Up
Building a Reddit Clone from the Ground UpBuilding a Reddit Clone from the Ground Up
Building a Reddit Clone from the Ground Up
 
ACM Init() lesson 1
ACM Init() lesson 1ACM Init() lesson 1
ACM Init() lesson 1
 
ACM Fall General Meeting
ACM Fall General Meeting ACM Fall General Meeting
ACM Fall General Meeting
 

Recently uploaded

Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
Kartik Tiwari
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
gb193092
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 

Recently uploaded (20)

Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 

ACM init() Day 5

  • 1. ACM init() Day 5: November 20, 2014
  • 2. From last time • More about arrays • Method • Parameters • Return Values • Blocks • How to use a block • Parameters
  • 3. Area Methods HW I wanted you to make a program that uses methods to calculate the area of various shapes. It was to be called like this:
  • 4. First step We called a method called "area", using the shape name and two side lengths as arguments.
  • 5. Second step The area method then called a method that calculates and prints the area of each shape.
  • 6. A new data type We 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.
  • 8. Hash example continued What will this print? Make your own hash!
  • 9. Hash syntax In general, a hash looks like this: A hash consists of key-value pairs. Each key maps to a value.
  • 10. Creating a hash There 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!
  • 11. Adding to a hash Say we create an empty hash called pets. Now we want to add our pets to it!
  • 12. Accessing a hash We can access elements of a hash just like elements of an array.
  • 13. Your turn! Create a hash called about_me. In the hash, put your name, your age, and your favorite color.
  • 14. Answer There are two ways to do this:
  • 15. Iterating over hashes Last week we went over how to iterate over arrays: We can iterate over hashes in a very similar way.
  • 16. Iterating over hashes Iterating 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.
  • 18. Any questions on hashes?
  • 19. Now we get to do the cool stuff... Object Oriented Programming.
  • 20. 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!
  • 21. 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.
  • 22. Strings as objects We 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.
  • 23. What is a class? A class is just a way of organizing and producing objects with similar attributes and methods.
  • 24. 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!
  • 25. Defining a class First, we need to make a class for our student. Here's how to start: Now we have an empty class!
  • 26. Initializing a class Every 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:
  • 27. Initializing a class We wanted our student to have a name, an ID, and a major. Let's do that!
  • 28. 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.
  • 29. 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".
  • 30. Creating objects We can create a bunch of Student objects! Each one will have its own name, ID, and major.
  • 31. 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
  • 32. Adding methods to classes We added a method called sayHi! Now how do we use it?
  • 33. 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
  • 34. 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!
  • 35. Let's add more methods!
  • 36. Let's add more methods!
  • 37. Using the new methods These new methods can be called the same way as the first one! What will the following print out?
  • 39. Let's create our own class together!
  • 40. Any questions on classes (so far)?
  • 41. 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
  • 42. Homework On 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.