Saturday, April 1, 2017

I moved my blog (again) :)

I moved my blog again. This time to GitHub Pages.

I have written the reasons here. Check it out if you want to know why. :)

Happy coding!!

And enjoy doing the other good things that you love doing. (And let's already stop doing the bad things we are doing :) )

Saturday, February 25, 2017

My "Teacher Jboy" blog was removed

A few weeks ago, I created a blog named teacherjboy.blogspot.com.

It already has a Google+ Page and a Facebook Page (I renamed it back to www.facebook.com/jboyflaga)



As you can see from that About page, I really wanted to become a teacher :D

But when I opened it today Blogger says that it was removed.


So I tried to login to my google account for that blog. There was a message that says about invalid activity(?). After entering my password it asked for my phone number to verify my account. So I entered my phone number, entered the verification code, and I was able to login.

My posts are still in my Blogger dashboard.


There are only three posts. So what I did is copy those three posts here in this blog.

So sad. :(

I have to accept that I'm not destined to be a teacher. :(

But... happy coding still!! :)


UPDATE (after an hour I think, when I tried to refresh again the tab on my browser where I opened teacherjboy.blogspot.com...):

My "Teacher Jboy" is back!!!

I do not know what happened!

But my profile is not showing on the page.

And it has this "Using Blogger as Unknown" on the dashboard.

I tried to google on how to solve this problem.

I found this.
  1. Login to www.Blogger.com
  2. Choose the desired blog from your Blogger dashboard
  3. Go to Settings > User Settings > General > User Profile
  4. Check if Blogger is selected. (You can either change this to your Blogger profile or your Google+ profile)
  5. Click on Save Settings
Great!


Sunday, February 19, 2017

Substituting polymorphism for if/else or switch/case - Example #1 - Shapes

This is Example #1 of the series of examples I'm going to give on "Substituting polymorphism for if/else or switch/case statements".


You have a new task


It's your first day at work. You are very excited to be involved in a real-world project.

Your team lead tried to explain to you the purpose of the project you are involved in.
"Here at Hugis Inc., we strive to be the best drawer of shapes."
After an hour, he gave you your first task.

You have to implement this method:

(Because I used C# for many years, I'm going to display C# in these posts. But I will also write these examples in Python and maybe other OO languages in the future. You can view them here.)

The codebase of the project you are involved in already have these classes:
  • a base Shape class,
  • classes for Circle, Square, and Triangle that derives from the Shape class.


The Evil code

This task is so easy!!! (you say to yourself)
You then started typing on the keyboard. Click! Click! Click!



Then after a few hours of hard work, you pushed your code to the repository, waiting for your team lead's comments.

The Good Code


Your team lead's comment came in.
"Good job!"
Wow!
"But there is a better way of doing this.

"We want to put the function drawCircle() inside the Circle class, and the function drawSquare() inside the Square class, because we want to have an easy access them anywhere in the system.

"Come over here. I'm going to show you how I will do the changes."
You then went to his table and watched him refactor your code.
"Because we want to be able to draw each Shape that we have, we will create an abstract method named draw() in our base Shape class.

     abstract public void Draw();

"Do you know what an abstract method is?"
No.
"An abstract method is a method definition that has no implementation -- like the getArea() method in the Shape class.

"Notice that getArea() does not have an implementation inside the Shape class. But any class that inherits from the Shape class is required to provide an implementation for getArea()"
Ahh! The Shape class did not provide an implementation for getArea() because different shapes has different formulas for getting the area! Great!

Now I understand what you want me to do with drawing shapes.

Let me be the one to refactor my code, please. I think I already know what to do.
"Ok! Go Ahead!"
You went back to your machine and started refactoring your code.

(Please go to the GitHub repository to view the complete refactoring.)



MTGOTB, TCOTU, BGWML

Saturday, February 18, 2017

Substituting polymorphism for if/else or switch/case - Introduction

A few weeks ago I saw this post from a facebook friend:


He is saying jokingly something like "Programming is like love... don't switch() because it will end up with a break... LOL"

I then asked him if his alternative to switch/case is polymorphism. He said no; and that he does not yet know what polymorphism is.

So after a few days, I thought of creating a blog post where I will teach how to replace if/else or switch/case statements with polymorphism.

I'm going to try to teach you how to replace your if/else with polymorphism

Have you seen code like this one below?
      If (typeOfShape is Circle)
         drawCircle()
      Elseif (typeOfShape is Square)
         drawSquare()
      Elseif (typeOfShape is Triangle)
         drawTriangle()
Or this one?
      Switch (typeOfShape)
         Case Circle:
            drawCircle()
         Case Square:
            drawSquare()
         Case Triangle:
            drawTriangle()
If you have been programming for a few years already, I believe you have seen lots of code that looks like those.

But did you know that, if you are using an object-oriented language such as Java, C# or Python, codes like those ones above are sometimes considered evil?
Evil? (you might say).
Yes! EVIL!

But I did not say that they are always evil. I said that they are sometimes evil. ("Sometimes evil" because I only have a few years of programming experience. "Often evil" when I have more :D . I'm just joking.)

They are evil because they might make modifications in the future much harder than they should be.
(you might be asking…) How else am I going to do conditional constructs like that? If/else and switch/case are the only ones available!
There is actually another method available to us on how to do conditions in OO languages. It is called polymorphism.

I found this great explanation by Steve Guidetti about polymorphism from code.tutsplus.com:
Polymorphism is a long word for a very simple concept. 

Polymorphism describes a pattern in object oriented programming in which classes have different functionality while sharing a common interface. 

The beauty of polymorphism is that the code working with the different classes does not need to know which class it is using since they're all used the same way. 

A real world analogy for polymorphism is a button. Everyone knows how to use a button: you simply apply pressure to it. What a button "does," however, depends on what it is connected to and the context in which it is used -- but the result does not affect how it is used. If your boss tells you to press a button, you already have all the information needed to perform the task.
There are already lots of available resources on the web about polymorphism. [The] Google [search engine] is your friend. Ask your friend to teach you polymorphism. But I recommend you to start with that explanation from code.tutsplus.com I linked to above, or from stackoverflow.

Then after that, go back here because I'm going to give you a number of examples on how to convert your if/else and switch/case statements into polymorphic code.

(Another reason why I delegate the explanation to others is because I'm not very good at explaining things using the English language :) )
Why are you going to give me lots of examples? I think one example will do. 'You think I'm dumb?
No. I do not think you are dumb.

It's only that I read an article a few months ago which claims that giving students lots of examples of a problem and their solutions can help them easily spot similar occurrences of that problem and can help them easily solve those kinds of problems. (But I already forgot where that article is located. Maybe I took a note of it somewhere but I'm not able to find it. I will just update this post when I will find it in the future)

Here are the examples:

1. Drawing Shapes
2. to be posted
3. to be posted
4. to be posted

Examples from other people:
https://sourcemaking.com/refactoring/replace-conditional-with-polymorphism

Have fun coding!!!


MTGOTB TCOTU BGWML

Saturday, February 4, 2017

Hints for Nand2Tetris Chapter 1 Exercises

I have already done Chapters 1 to 8 of Nand2Tetris last 2013. But I want to finish it. I have decided to do everything all over again.

I am not allowed to put online my solutions to the Nand2Tetris exercises. So what I'm going to do instead is to give some hints on how to solve them.


Here are the hints for the Chapter 1 Exercises:

1. Not gate - look at the Nand truth table; notice the output when both a and b are the same

2. And gate - use your newly created Not gate (and the Nand gate of course)

3. Or gate - we can use the idea from the Canonical Representation presented on page 9. But instead of focusing on the 1 outputs of the Or function, let's focus instead on the 0 output.

The Or function has only one 0 output.


This means that we can be able to create an Or gate without using an Or gate. haha
We can solve this using the And and Not gates

"We will Not the And."


4. Xor gate - we can now use the Canonical Representation from page 9.

5. Multiplexor (Mux) - same as #4; use the Canonical Representation from page 9

6. Demultiplexor (DMux) - use truth table to determine when a and b will be 1; then use the Canonical Representation



7 to 10. Multi-Bit gates (Not16, And16, Or16, Mux16) - read "A.5.3 Busses" of the Appendix to know more about how to solve these.

Do something like this, for example, for the Multibit Not gate:

    Not(in=in[0], out=out[0]);
    Not(in=in[1], out=out[1]);
    ...

    Not(in=in[9], out=out[9]);
    ...

11. Multi-Way Or (Or8Way) - use Or gates and internal pins (see A.5.2 of Appendix A for an explanation for internal pins)

12 Multi-Way/Multi-Bit Multiplexor

       12.a (Mux4Way16) - use Mux16 and temporary internal pins



       12.b Mux8Way16 - similar to 12.a but also use Mux4Way16

13. Multi-Way/Multi-Bit Demultiplexor - quite similar to 12.a and 12.b

       13.a DMux4Way - Use DMux and internal pins



       13.b DMux8Way - same to 13.b but also use DMux4Way 


If you have any comments or corrections or ideas on how to make the hints better, please tell me. Thanks!




Monday, August 8, 2016

I'm reviewing data structures and come graph algorithms using Stanford's CS106X

A few weeks ago, I wanted to be able to solve some problems on UVA Online Judge that requires some knowledge of graph algorithms. So I thought of trying to do again the assignments for Stanford's CS106B that I did last 2011.

The problem was that the assignment files that I have requires Visual Studio 2005. I tried to find ways to make it work on VS 2015 but I failed.

So I went to the latest CS106B website to download their latest assignment files (maybe they are using Visual Studio 2015 now?).

... Toinks! I needed to login to download the files. But I'm not a Stanford student! So I was not able to download the files.

Luckily, I found the latest website of CS106X and the files are downloadable. I thought to myself that the problems for CS106X might be more difficult than CS106B's but I think I can be able to do this now because I already experienced solving problems similar to these before.

The handout on the Course Information says that uploading my code to GitHub is a violation to the Honor Code...

The following four activities are among the many considered to be Honor Code violations in this course:
1. Looking at another student's code.
2. Showing another student your code, or making your code public so that it's searchable and easily discovered online or elsewhere.
3. Discussing assignments in such detail that you duplicate a portion of someone else's code in your own program.
4. Uploading your code to a public repository (e.g. github.com or bitbucket.com) so that others can easily discover it via word of mouth or search engines. If you'd like to upload your code to a private repository, you can do so on bitbucket or some other hosting service that provides free-of-charge private hosting.

... so I'm not going to put my code on GitHub or anywhere where anyone can download it. (I placed my solutions online last 2011 when I did CS106B but I already removed them.)

What I'm going to do instead is to put screenshots and the finished executable files of my solutions online just to give proof that I solved the exercises.

You can see the screenshots and executable files of my solutions on my GitHub.

Here is the screenshot of the first problem that I did:



Thursday, August 4, 2016

An easy to read Discrete Math book, I hope :)

I found a book which I believe can help me begin to study Discrete Mathematics and other parts of theoretical computer science: the Building Blocks of Theoretical Computer Science by Margaret M. Fleck.

I found the book from a list of books on Github.

I became interested with the book because the preface says "Everyone can do proofs".

And then says this:
"Some of you are brilliant young theoreticians and you’ll think this stuff is fun because it’s what you naturally like to do. However, many of you are future software and hardware engineers. Some of you may never think of formal mathematics as “fun.” That’s ok. We understand. We’re hoping to give you a sense of why it’s pretty and useful, and enough fluency with it to communicate with the theoretical side of the field."
I'm happy that there are math people out there who are considerate to programmers like me who do not have a very rich math background... and they are doing something to make it easier for us to understand that things that they understand!!! Wow!

I actually do think that formal mathematics is fun. I just don't understand them now.

I hope this book will help get me started in studying the more complex parts of computer science.


Another thing is that, a few days ago, I actually found another interesting book that I believe is similar to the one above: "Mathematical Foundations of Computing" by Keith Schwarz. It is a course reader of Stanford's CS 103 last Winter 2016.

What made me become interested to this one is the statement in the Course Information about prerequisites:
"CS103 is a theory course, but doesn't have any math prerequisites. You should feel comfortable with high-school algebra (i.e. factoring and multiplying polynomials), but you do not need to have taken precalculus or calculus prior to taking CS103. We'll build up all the mathematical machinery we need as we go."
I thought I need calculus when studying discrete math. Well, maybe it is needed in more advanced discrete math? I don't know.

But I will first use "Building Blocks of Theoretical Computer Science" then the second book, "Mathematical Foundations of Computing".

Happy coding!! I mean, mathematicking :D :D


Here's an inspiring video that I got from the webpage of Stanford's CS 103 last Winter 2016:



She says, "Math is hard. But you can do it."

Thanks!