decoration decoration decoration
decoration
leaf leaf leaf leaf leaf
decoration decoration

Exercise 42: Is-A, Has-A, Objects, and Classes

Today is practice on using “has-a” and “is-a” to describe the relationship between classes and objects.

Here is my work:

And here are the study drills:

  1. Let’s see, so if we think about Ruby in terms of objects, instead of classes, every time we define a class, we create a new instance of class named Class. Each of these instances contains everything related to the structure of the object. Only one instance for each class definition is needed in a typical ruby application. I.e: You do not need both Salmon.new() and Salmon2.new()
  2. Yes.
  3.  This is inheritance at work – if a new function is written in the base class, all of the following classes inherit that function, whereas if it were in a child class, only that class would be able to access it.
  4.  I did this for the same card_shuffler program :

 

5. I added an array of hobbies and a hash list of groceries to the People class, then tested it out with mary.

6. Yes, I think there is an is-many relationship, and multiple inheritance would mean something like:

class A inherits class B
class A inherits class C

which turns out is known as the Diamond Problem.

This problem occurs when things have multiple values or traits they’ve inherited – which one should they use? I also read that Ruby does not support multiple inheritance.

 

This was an arduous exercise is finding, creating, and analyzing the relationships between classes and objects. I feel much better about these associations!

 

Exercise 41: Learning To Speak Object Oriented

As in learning any language, there is a big chunk of time dedicated to memorization – how to associate a word or phrase with a group of words or a sentence that means something to you. Here, I spent time making note cards for words and phrases, as well as examples using these to learn them. For your sake, here is a link to the exercise.

After I learned the phrases, I reentered the code here:

 

With my output here:

 

Pretty good, right?

 

And then for the study drills listed here:

I chose to use this code:

1. For each class give its name and what other classes it inherits from.

There is a class  named Shuffler, which doesn’t inherit from any other class.

There is a class  named Counter, which doesn’t inherit from any other class.

There is a class  named Controller, which doesn’t inherit from any other class.

2. Under that, list every function it has and the parameters they take.
Here are the functions it has. To put it into the English phrases I’ve been practicing,

Class Shuffler has-a:

  • initialize function that takes the parameters (cards and cut)
  • shuffle function
  • cut_deck function
  • mix_cut_cards function
  • add_extra_cards function

Class Counter has-a:

  • initialize function that takes the parameter (shuffler)
  • shuffle_cards function
  • display_shufflers_cards function
  • check_deck function
  • card_eq_original? function that takes the parameter (card)
  • one_cycle function
  • find_all_card_cycles function
  • find_least_shuffles function

Class Controller has-a:

  • initialize function
  • get_cards function
  • get_cut_point function
  • convert_num_to_string function that takes the parameter (number)
  • puts_to_screen function that takes the parameters (shuffles, total_time)

3. List all of the attributes it uses on its self.

Class Shuffler uses:  :initial_cards, :cut, :top_cut, :top, :bottom, :extra, and accesses :cards as well.

Class Counter uses: :shuffler_initial_cards, :least_shuffles, and accesses :shuffle_count, :card_cycles, :card_indices, :shuffler as well.

Class Controller uses none.

4. For each attribute, give the class this attribute is.

:initial_cards, :cut, :top_cut, :top, :bottom, :extra, :cards have the class Shuffler,

:shuffler_initial_cards, :least_shuffles,  :shuffle_count, :card_cycles, :card_indices, :shuffler have the class Counter.

 

Alright this makes sense. The newest piece here to me is the attr_accessor and attr_reader lines, but I found a good article to explain it with examples, here

Exercise 4: Variables And Names

In this exercise, I will be writing comments and reading the script out loud forward and backward to make it make sense in English.

Here is my code:

 

 

 

First, carpool_capacity was not defined properly or yet.

  1. No, You get an integer in return.
  2. Check.
  3. Check.
  4. Check.
  5. Check.
  6. Check.

Straight forward exercise— all good!

 

Exercise 3: Numbers and Math

Moving right along to exercise 3:

Here was my code:

 

and here was my output:

Again, all good and dandy,  so I moved on to the study drills.

  1. Check.

And that’s it! To be honest, I had some trouble understanding the modulus (%) character, because I kept thinking about it as the decimal piece, but now that translation is clear.

Exercise 2: Comments and Pound Characters

Exercise 2 was basic as well, and getting used to the lingo of # meaning “Don’t try and read this, Ruby – this is for us humans!”

 

 

 

And here was my output :

 

Easy peasy! So then I moved on to the study drills:

  1. Yes, I was correct about the # character.
  2.  Check
  3.  Check
  4. Check

Exercise 1: A Good First Program

I am currently learning Ruby through Zed Shaw’s Learn Ruby the Hard Way.

I will document my progress and takeaway thoughts as I work my way through the exercises.

Exercise 0 had me choose a text editor and gave me a review on working the terminal. See below:

 

Next, I moved on to Exercise 1, which was an effective program on communicating with Ruby.

and here was my output:

Easy peasy! So I moved onto the study drills…

1. & 2. See above

3. The # sign commented out the line of code, telling Ruby to not try to run it. Used to explain what the code is doing.

 

 

 

Day 34

9:30 am , actually getting ready to move to my next project.. but first..

My Dungeon Game!  I coded it in Ruby, then made a map in Photoshop. So proud.

 

 

11am

Okay, started planning the map of my next game. Took 3 ruby practice quizzes to sharpen up.  Going to try to solve the rails problem #4 this afternoon.

Day 25

1:30pm

Alright, so I decided against the classes at CCSF – I’m making excellent progress here, and I think I need the flexibility right now to work on different projects/look for opportunities around me.

And so here’s the progress:

Yesterday, fixed a client’s bug, and made progress in Ruby tutorial.

Today:

  1. More Ruby progress, to a point where I am starting a bigger project (tomorrow).
  2.  Fixed the 2nd issue on my Rails coding challenge (woo!)
  3. Going to work on some design skills now, maybe pertaining to creating invitations?

Happy Mondayyyyyyyyyyyyyyyyyy

Day 22

So, the “job fair” was a bust. It turned out to be an orientation, which I couldn’t stay for. But, the good news is that it did get me to revamp my resume and print out a few copies, so not all is a loss. And  since my recent resume update, I sent it to a company, and they have viewed it 3 times. Sooo I must be doing something right!

Other news: I’m officially halfway through LearnRubytheHardWay (woohoo!), blazed through the test, and am now memorizing logic tables. The funny thing is, I have taught truth tables, so I understand it, but this is useful because I would like to be able to do it quickly, and know my options.

Also, I am narrowing down my options for classes, I will make some decisions this week.

In other news, my Web Dev clients are sending me work this week – so that’s keeping me busy as well!

Phewph. Anyway, gonna take a break now, lots of teaching tonight.

xx

 

- PAGE 1 OF 2 -

Next Page ⇀

loading
×