Saturday, November 30, 2013

What is the ii/IV - V/IV - IV progression


ii/IV is read as "two of four" which is Gm7 in the key of C.

V/IV is read as "five of four" which is C7 in the key of C.

and IV is F in the key of C.

You can find an example of this by Greg Howlett on thins link.




  
Free Lessons for Chruch Pianists:
Free lessons, tips and downloads for church pianists

DVD Courses (Reharmonization, Play by Ear!, Arranging, Accompanying, Theory for Church Pianists, etc.):
Over 30 hours of DVD instruction for church pianists

Sunday, November 10, 2013

How to install Ruby on Rails offline in Windows 7 and use WEBrick as web server

When online
  1. Download railsinstaller-2.2.1.exe from http://railsinstaller.org/
  2. Download uglifier-2.2.1.gem from http://rubygems.org/gems/uglifier

When offline (on another computer)
  1. Execute railsinstaller-2.2.1.exe
  2. Open command window
  3. Using the command window, navigate to the directory where uglifier-2.2.1.gem is saved
                                 cd <path where uglifier-2.2.1.gem is saved>
  1. Type the following command:
                                gem install --local uglifier-2.2.1.gem
  1. Create your Rails site with the following command:
                                rails new <name of your site>
  1. Using the command window, navigate to the directory of your site:
                                cd <path of your site>
  1. While inside your site's directory, run the web server (WEBrick):
                                rails server
  1. Open your web browser to view your site: http://localhost:3000/

Tuesday, June 11, 2013

From Nand to Tetris - My Solutions to the Projects


I have created a repository to contain my solutions to the projects of the course "From Nand to Tetris"
(You can also get the reading materials and projec files at http://www.cs.huji.ac.il/course/2002/nand2tet/projects.html)

Here is the repository of my solutions: https://github.com/jboyflaga/Nand2Tetris
(Some of my solutions are not very good even though the output is still correct. Be sure to also look at other people's solutions for reference.)

UPDATE (November 17, 2013): I deleted the GitHub repository of my solutions to the Nand2Tetris projects 1 to 8 because someone who is teaching it (Alex Lipov) requested for it to be removed because it violated the course terms (http://www.nand2tetris.org/terms.php).

If you want to know how to start the course just read the whole Chapter 1 available in http://www.nand2tetris.org/course.php or http://www.cs.huji.ac.il/course/2002/nand2tet/projects/proj1.html. You will know what to do especially after you read the last part of chapter 1.

Enjoy!!!

Friday, May 31, 2013

From Nand to Tetris - Project 4 (Fill)

This is my solution to the exercises (Fill) in Project 4 of the free course From Nand to Tetris
Other Links:
http://www.cs.huji.ac.il/course/2002/nand2tet/projects.html
// Runs an infinite loop that listens to the keyboard input. 
// When a key is pressed (any key), the program blackens the screen,
// i.e. writes "black" in every pixel. When no key is pressed, the
// program clears the screen, i.e. writes "white" in every pixel.

// (RESET_SCREEN_COUNTER)
 // @SCREEN
 // D=A
 // @counter
 // M=D    // counter = SCREEN

(LISTEN_FOR_NO_KEY_PRESS)
 // reset screen counter
 @SCREEN
 D=A
 @counter
 M=D
 
 // if KBD == 0 jump to CLEAR_SCREEN
 @KBD
 D=M
 @CLEAR_SCREEN
 D;JEQ

 // loop (keep on checking if KBD == 0
 @LISTEN_FOR_NO_KEY_PRESS
 0;JEQ

(LISTEN_FOR_KEY_PRESS)
 // reset screen counter
 @SCREEN
 D=A
 @counter
 M=D
 
 // if KBD > 0 jump to BLACKEN_SCREEN
 @KBD
 D=M
 @BLACKEN_SCREEN
 D;JGT
 
 // loop (keep on checking if KBD > 0
 @LISTEN_FOR_KEY_PRESS
 0;JEQ
 
(BLACKEN_SCREEN) 
 // blacken 16-bit memory location
 @counter
 A=M    // A = counter
 M=-1   // blacken current memory location
  
 // if KBD == 0 jump to LISTEN_FOR_NO_KEY_PRESS (to reset counter)
 @KBD
 D=M
 @LISTEN_FOR_NO_KEY_PRESS
 D;JEQ

 // increment counter
 @counter
 M=M+1
 
 // check if you have reached memory location 24575 (the final address for the screen)
 D=M
 @24575
 D=A-D
 @LISTEN_FOR_NO_KEY_PRESS
 D;JLT   // goto LISTEN_FOR_NO_KEY_PRESS if counter > 24575
 
 // loop BLACKEN_SCREEN if you have NOT reached memory location 24575
 @BLACKEN_SCREEN
 0;JEQ 

(CLEAR_SCREEN)
 // clear 16-bit memory location
 @counter
 A=M    // A = counter
 M=0    // clear current memory location
 
 // if KBD > 0 jump to LISTEN_FOR_KEY_PRESS (to reset counter)
 @KBD
 D=M
 @LISTEN_FOR_KEY_PRESS
 D;JGT
 
 // increment counter
 @counter
 M=M+1
 
 // check if you have reached memory location 24575 (the final address for the screen)
 D=M
 @24575
 D=A-D
 @LISTEN_FOR_KEY_PRESS
 D;JLT   // goto LISTEN_FOR_KEY_PRESS if counter > 24575
 
 // loop CLEAR_SCREEN if you have NOT reached memory location 24575
 @CLEAR_SCREEN
 0;JEQ

Friday, May 3, 2013

The Humans Project - using the HotTowel SPA template


This is a very very simple app I created to practice developing a SPA (Single Page App).

I used the HotTowel SPA template created by John Papa.
To learn more about HotTowel go to http://www.johnpapa.net/hottowel/.

HotTowel uses the Durandal SPA framework
To know more about Durandal go to the folowing pages:
http://durandaljs.com/pages/get-started
http://durandaljs.com/pages/videos/

The project is available on github.
 
(This is not yet finished)
(And pardon the look of the pages... I'm not a very good designer...)





  
Free Lessons for Chruch Pianists:
Free lessons, tips and downloads for church pianists

DVD Courses (Reharmonization, Play by Ear!, Arranging, Accompanying, Theory for Church Pianists, etc.):
Over 30 hours of DVD instruction for church pianists

Thursday, May 2, 2013

Using X509Certificate2UI.SelectFromCollection() with WPF and MVVM

I had a task which involves making the user select a certificate from the store.
(We are using .NET)

I used WPF and the MVVM pattern for the UI; and I used the MVVM Light Toolkit to help with MVVM development.

I used the X509Certificate2UI.SelectFromCollection() method to show a dialog for the user to select a certificate (X509Certificate2UI is in the System.Security.Cryptography.X509Certificates namespace, System.Security.dll assembly)
X509CertificateCollection certificateCollection = X509Certificate2UI.SelectFromCollection(
                    store.Certificates, "Caption", "Message",
                    X509SelectionFlag.SingleSelection);


My problem was the dialog that X509Certificate2UI.SelectFromCollection() is not modal and it does not close when I close the main window.

The solution was to use the overloaded method of X509Certificate2UI.SelectFromCollection() which accepts a handle to the parent window.
WindowInteropHelper windowHwnd =new WindowInteropHelper(this); 
IntPtr hWnd = windowHwnd.Handle;
X509CertificateCollection certificateCollection = X509Certificate2UI.SelectFromCollection(
                    store.Certificates, "Caption", "Message",
                    X509SelectionFlag.SingleSelection, hWnd);

[To know more about Win32 Handles go to "Win32 Handle (HWND) & WPF Objects - A Note" - http://www.abhisheksur.com/2010/12/win32-handle-hwnd-wpf-objects-note.html ...]

But there was another problem: Because I was using MVVM with WPF I have to put this logic inside my ViewModel. But ViewModel does not have direct acces to the Window element!

The solution was to pass the Window object as parameter to a Command object as shown below

[The View - XAML]

<Window ...  Name="TheMainWindow"
   DataContext="{Binding Main, Source={StaticResource Locator}}">
  <Button Command="{Binding SelectCertificateCommand}" CommandParameter="{Binding ElementName=TheMainWindow}" Content="Select Certificate From the Store" />
 </Window>

[The ViewModel]
[ViewModelBase and RelayCommand are from the MVVM Light Toolkit]
    public class MainViewModel : ViewModelBase
    {
        private X509Certificate2 _selectedCertificate;

        public ICommand SelectCertificateCommand { get; private set; }

        public MainViewModel()
        {
            this.SelectCertificateCommand = new RelayCommand(
                (window) =>
                {
                    X509Certificate2 cert = PickCertificate(StoreLocation.CurrentUser, StoreName.My, window);
                    if (cert != null)
                    {
                        this.Certificate = cert;
                    }
                });
        }

        public X509Certificate2 Certificate
        {
            get { return _selectedCertificate; }
            set
            {
                if (_selectedCertificate == value) { return; }
                _selectedCertificate = value;
                RaisePropertyChanged(() => Certificate);
            }
        }

        private static X509Certificate2 PickCertificate(StoreLocation location, StoreName name, Window window)
        {
            X509Store store = new X509Store(name, location);
            try
            {
                store.Open(OpenFlags.ReadOnly);

                // pick a certificate from the store 
                X509Certificate2 certificate = null;
                X509CertificateCollection certificateCollection = X509Certificate2UI.SelectFromCollection(
                    store.Certificates, "Select a certificate", "Select a certificate",
                    X509SelectionFlag.SingleSelection, new WindowInteropHelper(window).Handle);
                if (certificateCollection.Count > 0)
                {
                    certificate = (X509Certificate2)certificateCollection[0];
                }

                return certificate;
            }
            finally
            {
                store.Close();
            }
        }
    }





  
Free Lessons for Chruch Pianists:
Free lessons, tips and downloads for church pianists

DVD Courses (Reharmonization, Play by Ear!, Arranging, Accompanying, Theory for Church Pianists, etc.):
Over 30 hours of DVD instruction for church pianists

Saturday, April 27, 2013

Public Key Encryption and Digital Signature: How do they work?



I recently had a task which involves Encryption and Digital Signature. This paper from CGI.com is one of the things that helped me understand how public key encryption and digital signatures work.

http://www.cgi.com/files/white-papers/cgi_whpr_35_pki_e.pdf

I hope this also will help you.

Monday, April 8, 2013

My solution to chapter 12 exercises of JavaScript Step by Step 2e

1. Create a webpage that sends a cookie to the browser. Set the expiration date ahead
one day. Verify that the JavaScript code sent the cookie to the browser by viewing it as
it gets set or after it’s been stored on the computer. You could accomplish this second
part of the exercise; by using JavaScript or by viewing the cookies on the computer.

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <script>
        var cookieName = "mycookie-1";
        var cookieValue = "mycookievalue-1";
        var date = new Date();
        date.setTime(date.getTime() + (24 * 60 * 60 * 1000));
        var expireDate = date.toGMTString();

        var myCookie = cookieName + "=" + cookieValue + ";expires=" + expireDate;
        document.cookie = myCookie;

        // the following code is not part of exercise 1
        // another cookie
        var cookieName2 = "mycookie-another";
        var cookieValue2 = "mycookievalue-another";
        var date2 = new Date();
        date2.setTime(date2.getTime() + (24 * 60 * 60 * 1000));
        var expireDate2 = date2.toGMTString();

        var myCookie2 = cookieName2 + "=" + cookieValue2 + ";expires=" + expireDate2 + ";path=/;domain=localhost;";
        document.cookie = myCookie2;
       
        alert(document.cookie);
    </script>
</body>
</html>


2. Create a webpage that sends a cookie with the cookie’s expiration date set ahead one
week, and set the secure flag. This page can be the same one you created for Exercise
1, but be sure to give the cookie a different name so that you’ve created two separate
cookies, one for each exercise. Also, be sure to enable the secure flag for the cookie in
this exercise, not for the cookie in Exercise 1.

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <script>
        var cookieName = "mycookie-2";
        var cookieValue = "mycookievalue-2";
        var date = new Date();
        date.setTime(date.getTime() + (7 * 24 * 60 * 60 * 1000));
        var expireDate = date.toGMTString();

        var myCookie = cookieName + "=" + cookieValue + ";expires=" + expireDate + ";secure";
        document.cookie = myCookie;
    </script>
</body>
</html>

3. Create a webpage that attempts to read the cookie with the secure flag set. Did you
receive the cookie? If not, what would you need to do to receive it?

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <script>
        var cookies = document.cookie.split(";");
        var cookLength = cookies.length;
        for (var c = 0; c < cookLength; c++) {
            alert(cookies[c]);
        }
    </script>
</body>
</html>

4. Create a webpage that reads the cookie you created in Exercise 1. Use a for loop and an
if conditional to display an alert() dialog box when the cookie with the correct name is
found within the loop. Don’t display an alert() dialog box for any other cookies.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <script>
        var cookies = document.cookie.split(";");
        var cookLength = cookies.length;
        for (var c = 0; c < cookLength; c++) {
            var pairs = cookies[c].split("=");
            var cookieName = pairs[0];
            var cookieValue = pairs[1];

            // display only the cookie with name of "mycookie-1"
            if (pairs[0] === "mycookie-1") {
                alert("Name: " + cookieName + " -> " + "Value: " + cookieValue);
            }
        }
    </script>
</body>
</html>

My solution to chapter 11 exercises of JavaScript Step by Step 2e

1. Create a webpage that contains an onclick event handler connected to a link using a
DOM 0 inline event. The event handler should display an alert stating “You Clicked
Here”.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <a href="#" onclick="alert('You Clicked Here');">This is a link</a>
</body>
</html>


2. Change the webpage created in Exercise 1 to use the newer style of event handling
shown in ehandler.js (in the companion content) and connect the same click/onclick
event to display the alert created in Exercise 1.

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="../../Scripts/ehandler.js" type="text/javascript"></script>
</head>
<body>
    <a id="link1" href="#">This is a link</a>

    <script type="text/javascript">
        function showAlert() {
            alert("You Clicked Here");
        };
        var link1 = document.getElementById("link1");
        EHandler.add(link1, "click", showAlert);       
    </script>
</body>
</html>


3. Create a webpage with a link to http://www.microsoft.com. Make that link open in a
new tab.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <a href="http://www.microsoft.com." target="_blank">http://www.microsoft.com</a>
</body>
</html>

Friday, March 22, 2013

My solution to chapter 10 exercises of JavaScript Step by Step 2e

 This is my solution to Chapter 10 programming exercises of JavaScript Step by Step 2e.

1. Create a document containing a paragraph of text that you create and append using
the DOM. Create a link immediately after this paragraph that links to a site of your
choice, also using the DOM. Make sure that all the elements have id attributes.

JavaScript:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    
</head>
<body>
    <script type="text/javascript">
        // create <p> the append to body
        var element = document.createElement("p");
        element.setAttribute("id", "p1");
        element.textContent = "I am a p";
        document.body.appendChild(element);

        // create <a> then append to body
        element = document.createElement("a");
        element.setAttribute("id", "a1");
        element.setAttribute("href", "http://jeremiahflaga.blogspot.com");
        element.textContent = "this is my blog";
        document.body.appendChild(element);
    </script>
</body>
</html>

TypeScript:
// create <p> the append to body
var element: HTMLParagraphElement = <HTMLParagraphElement>document.createElement("p");
element.setAttribute("id", "paragraph1");
element.textContent = "I am a paragraph";
document.body.appendChild(element);

// create <a> then append to body
var element2: HTMLAnchorElement= <HTMLAnchorElement>document.createElement("a");
element2.setAttribute("id", "anchor1");
element2.setAttribute("href", "http://jeremiahflaga.blogspot.com");
element2.textContent = "this is my blog";
document.body.appendChild(element2); 


2. Create a document with any elements you like, or use an existing HTML document that
contains id attributes in its elements. Retrieve two of those elements, make changes to
them, and put them back into the document. The type of change you make depends
on the type of element you choose. For example, if you choose an a element, you might
change the href; if you choose a p element, you might change the text.

JavaScript:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    
</head>
<body>
    <p id="p1">I am a p</p>
    <a id="a1" href="http://jeremiahflaga.blogspot.com">this is my blog</a>

    <script type="text/javascript">
        // change text of <p>
        var element = document.getElementById("p1");
        element.textContent = "I am a p of exercise #2";
        document.body.appendChild(element);

        // change text of <a>
        element = document.getElementById("a1");
        element.setAttribute("href", "http://www.johnpapa.net/");
        element.textContent = "Click here to go to John Papa's blog";
        document.body.appendChild(element);
    </script>
</body>
</html>

TypeScript
// change text of <p>
var element: HTMLParagraphElement = <HTMLParagraphElement>document.getElementById("p1");
element.textContent = "I am a p of exercise #2 - from typescript";
document.body.appendChild(element);

//  change text of <a>
var element2: HTMLAnchorElement = <HTMLAnchorElement>document.getElementById("a1");
element2.setAttribute("href", "http://www.johnpapa.net/");
element2.textContent = "Click here to go to John Papa's blog - from typescript";
document.body.appendChild(element2);
 
 
3. Create a document by using the DOM that contains a table with at least two columns
and two rows. Add some text in the table cells.

JavaScript:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    
</head>
<body>
    <script type="text/javascript">
        // create <table> the append to <body>
        var table = document.createElement("table");
        document.body.appendChild(table);
        table.setAttribute("border", 1);

        // create <tr> (row) then append to <table>
        var row = document.createElement("tr");
        table.appendChild(row);

        // create <td> (cell) then append to <tr>
        var cell = document.createElement("td");
        row.appendChild(cell);
        cell.textContent = "Cell #1";

        // create <tr> (row) then append to <table>
        var row = document.createElement("tr");
        table.appendChild(row);

        // create <td> (cell) then append to <tr>
        var cell = document.createElement("td");
        row.appendChild(cell);
        cell.textContent = "Cell #2";
    </script>
</body>
</html> 
 
TypeScript
// create <table> the append to <body>
var table: HTMLTableElement = <HTMLTableElement>document.createElement("table");
document.body.appendChild(table);
table.setAttribute("border", "1");

// create <tr> (row) then append to <table>
var row: HTMLTableRowElement = <HTMLTableRowElement>document.createElement("tr");
table.appendChild(row);

// create <td> (cell) then append to <tr>
var cell: HTMLTableCellElement = <HTMLTableCellElement>document.createElement("td");
row.appendChild(cell);
cell.textContent = "Cell #1 - from typescript";

// create <tr> (row) then append to <table>
row = <HTMLTableRowElement>document.createElement("tr");
table.appendChild(row);

// create <td> (cell) then append to <tr>
cell = <HTMLTableCellElement>document.createElement("td");
row.appendChild(cell);
cell.textContent = "Cell #2 - from typescript"; 

My solution to chapter 9 exercises of JavaScript Step by Step 2e

 This is my solution to Chapter 9 programming exercises of JavaScript Step by Step 2e.
 
1. Use the availHeight and availWidth methods to determine whether a screen is at least 768 pixels high and 1024 pixels wide. If it’s not, display an alert() dialog box stating the size of the available screen.

JavaScript:
if (screen.availHeight < 768 && screen.availWidth < 1024) {
    alert("Available height: " + screen.availHeight + "\n"
        + " Available width: " + screen.availWidth);
}

TypeScript: same as above

2. Alter the step-by-step exercise that used the location object to display an alert() dialog box based on the values of the query string. Specifically, display the word “Obrigado” if the country is specified as Brazil, and display “Thank you” if the country is Great Britain. Test these conditions.

JavaScript:
if (location.search) {
    var querystring = decodeURI(location.search).substring(1);
    var splits = querystring.split('&');
    for (var i = 0; i < splits.length; i++) {
        var splitpair = splits[i].split('=');
        if (splitpair[0].toLowerCase() === 'country') {
            if (splitpair[1].toLowerCase() === 'brazil') {
                alert('Obrigado');
            }
            else if (splitpair[1].toLowerCase() === 'great britain') {
                alert('Thanks you');
            }
        }
    }
} 
 
TypeScript:
if (location.search) {
    var querystring: string = decodeURI(location.search).substring(1);
    var splits: string[] = querystring.split('&');
    for (var i = 0; i < splits.length; i++) {
        var splitpair: string[] = splits[i].split('=');
        if (splitpair[0].toLowerCase() === 'country') {
            if (splitpair[1].toLowerCase() === 'brazil') {
                alert('Obrigado');
            }
            else if (splitpair[1].toLowerCase() === 'great britain') {
                alert('Thanks you');
            }
        }
    }
} 


Wednesday, March 13, 2013

My solutions to programming exercises in JavaScript Step by Step 2e - Chapters 4 to 8


Even though I have already studied JavaScript in the past, I have never really done programming exercises using it. So I solved the exercises of JavaScript Step by Step 2e.

Because I am also currently learning Typescript, I solved the exercises, starting on chapter four, using both JavaScript and TypeScript.
(You can learn about TypeScript in John Papa's Blog - http://www.johnpapa.net/typescriptpost/)

You can download my solutions to chapters 4 to 8 here.



Sample code (Chapter 8 Exercise #2)
2. Create an object to hold the names of three of your favorite songs. The objects should have properties containing the artist, the song length, and the title for each song.

JavaScript:

function Song(artist, length, title) {
    this.artist = artist;
    this.length = length;
    this.title = title;
}

var songs = new Object;
songs.song1 = new Song("artist1", 101, "title1");
songs["song2"] = new Song("artist2", 102, "title2");
songs["song3"] = new Song("artist3", 103, "title3");

for (var i in songs) {
    alert(i + "=" + songs[i].title);
}

TypeScript:

class Song {
    private _title: string;

    artist: string;
    length: number;

    constructor(artist: string, length: number, title: string) {
        this.artist = artist;
        this.length = length;
        this._title = title;
    }

    get title(): string {
        return this._title;
    }

    set title(value: string) {
        if (value == undefined) throw 'value for title is undefined';
        this._title = value;
    }
}

var songs = new Object();
songs["song1"] = new Song("artist1", 101, "title1");
songs["song2"] = new Song("artist2", 102, "title2");
songs["song3"] = new Song("artist3", 103, "title3");

for (var i in songs) {
    alert(i + "=" + songs[i].title);
}