Saturday, August 25, 2012

Multi Threadinging in Java

The Basics

to create a thread-able  program you need the following
 requires
  Runnable interface                -> Runnable Object
  Requires a Thread Manager (in main) -> Thinking to use hash-map to handle this
  Requires Thread Launch -> Start method;




------------------------
-------
--------------------

**********************ugly class notes ignore this is for me only***************

Runnable Object ->(Starter Method) an object that is runnable (multi thread)

public void run() -->launch 2ND thread



Runnable Object is an Object that implements an interface Runnable which contains a single method

Creator of Threads: how to create a thread manager Object


Java Support
Thread (Manager) ||  Runnable




uggg explanation is a mess but here is the a code example



public class Message implements Runnable  {                                                            
                                                                                                                                
private String message;                                                                                     
                                                                                                                                                                                        
public Message (String msg)                                                                             
{                                                                                                                     
message = msg;                                                                                     
                                                                                                               
}                                                                                                                        
                                                                                                                         
@Override                                                                                                       
public void run()                                                                                            
                                                                                                                       
{                                                                                                                      
 while(true)                                                                                                   
 {                                                                                                                  
for (int i = 0 ; i < 100; i++)                                                                       
    System.out.print(message);                                                                       
       //Thread.yield()// TODO Auto-generated method stub                               
                                                                                                                  
   System.out.println();                                                                                      
Thread.yield();                                                                                         
 }                                                                                                                     
}                                                                                                                    
                                                                                                                              
}                                                                                                                              



public class Main {

public static void main(String[] args)
{
//create Runnable objects
Message m1 = new Message("X");
Message m2 = new Message("Y");
Message m3 = new Message("Z");
//Create Thread manager objects
Thread t1 = new Thread (m1);
Thread t2 = new Thread (m2);
Thread t3 = new Thread (m3);
//start threads
t1.start();
t2.start();
t3.start();
for (int i = 0 ; i < 1000; i++)
    System.out.print("M");
// System.out.println(m1);
//System.out.println(m2);
//System.out.println(m3);
}
}



Tuesday, August 21, 2012

JAVASCRIPT == or ===

In JavaScript we have loosely defined vars so this means that if you have

a = "5"
b = 5

that using (a==5) will return true even though they are not the same var (object) type. This can be annoying and can lead to errors. On the more realistic side who compares strings to ints without first parsing anyway. Well being a loose var system I guess it could happens to anyone. So the solution is to use === which will test for both value and object type to be equal.

Now the question comes up, what about comparing properties in polymorphic objects. At first thought we you might think that === will cause the comparison to fail when comparing two objects that are of different sibling types of a common parent. However this is not an issue as when doing this comparison we are actually only test each var that was extracted from those objects and not the object themselves. In other words the var type of the property becomes the comparable object which requires us to revert to using the === anyways.

So to answer the unanswered question: who compares strings to ints without parsing first well the answer would JavaScript programmers :)

JAVASCRIPT : ECMASCRIPT STANDARDS and GREAT RESOURCES

Finally figuring this out, JAVASCRIPT API or at least the standards guide follows ECMASCRIP requirements. So Finlay an actually resource that actually means something. This is so important.


http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf

also see the following book
Event Processing in Action
Authors:Opher Etzion
Peter Nibblett

book 2: This is the best Java Script book I have to date, the best part is it actually has a real API in it, I have been looking for this for weeks now I found it. This book is key to learning JavaScript correctly.

Java Script : The Definitive Guide , 6th edition or higher
By:David Flanagan  publisher: O'REILEY

Monday, August 20, 2012

Event Handdling in General

Ever get frustrated while programing your event handling: mouse, keyboard,buttons etc. I have found this missing link, I ran across this book call Event Processing in Action. I was curious and was frustrated programming my mouse in JavaScript so I bought it. I read the first three chapters so far and it's a wonderful book. It teaches event handling from a theoretical point of view so it might not help you with whatever language your working with, however it will improve your understanding of what is really happening in the background. Armed with this knowledge you will be able to turn your self in a Event Handling Guru. So just to share with whoever is interested in the topic I thought this book should be brought to light.

The book :
Event Processing in Action
Authors: Opher Etzion & Peter Niblett

Friday, August 17, 2012

JAVA SCRIPT: arguments variable

arguments

this is a key word var in javascript it holds all vars past to a function regardless if the constructor could assign them or not. So in short this allows you to create functions with an arbitrary number of functions.

JAVASCRIPT:JS-LINT

Today I learned about JSLINT. Js Lint is a program to which you can run your javascript through to check for errors, coding style and various conventions. As javascript has very loose rules it difficult to debug. Following the JSLINT conventions and rules will hopefully help you produce better, more reusable and sharable code. JSLINT was a developed by Yahoo's top developers.

you can get JSLINT and documentation at:
http://www.jslint.com/

also see the videos by Douglas Crockford at
http://www.youtube.com/results?search_query=Douglas+Crockford&oq=Douglas+Crockford&gs_l=youtube.3..35i39j0l9.5610.7677.0.8340.5.5.0.0.0.0.208.775.0j4j1.5.0...0.0...1ac.RCxFWzyRivM

Douglas Crockford: The JavaScript Programming Language

Why learn JavaScript & some great video footage

Watch these videos (sorry have to use youtube) since the normal hosting site is a joke!

I have update this link, will make it easier to locate all 3 movies!
http://www.youtube.com/playlist?list=PL5586336C26BDB324&feature=plcp

I'm always reading about creating fast and usable websites

Ever wounder why people refuse to use  or abandon websites: I have started a list of examples of such sites.

1.) http://www.yuiblog.com/blog/2010/02/03/video-crockonjs-1/ # just try to down load the HD videos have fun, or you an opt for the alternative and watch the embedded one which will give you a total headache in minuets because of the poor sound and video quality.

Thursday, August 16, 2012

javaScript: Nested Functions

Another Intresting concept that poped up in JavaScript
:nested functions inside of other functions : This provides a way to make a function private. If you want these be accessable there must be a accessor method in the outfunction otherwise they will be unreachable outside of the outter function

Here is an Example of what this looks like.

<!DOCTYPE html>
<html>
<head>
    <title>Nested Functions</title>
    <script type="text/javascript">
          function initiateMadness() {
            var sparta = {
                name : "Sparta"
            };
            function madness() {
                alert("THIS. IS. " + this.name.toUpperCase() + ".");
            }
            document.onclick = makeAMessenger(madness, sparta);
        }
       
//access the content of the inner function
   function makeAMessenger(func, context)
        {  
            window.name = context.name;
            func();
        }
        initiateMadness();
    </script>
</head>
<body>
<script type="text/javascript">
</script>
<h1> Heading 1 </h1>
<p> THis is my program </p>
    
</body>
</html>

Java Script : Array of functions

Another intersting concept that I came across:
Java Script allows you to create an array of functions, took me a while to make this work but I finally got it. Here is example of how to do this:

<!DOCTYPE html>
<html>
<head>
    <title>Array of functions</title>
<script type="text/javascript">
  
function transform(array)
   {
         function makeFunction(value)
             {return function()
                {
                    return value;
                }
             }
         
       var output = new Array();
       for(var i = 0; i < array.length; ++i)
          {
              output[i] = makeFunction(array[i]);
          }
       return output;
   }
</script>
</head>
<body>
<script type="text/javascript">
myArray = [1,2,3,4,5];
myArray = transform(myArray);
for(var i =0 ; i < myArray.length; ++i)
{
    alert( myArray[i]());
}
</script>

</body>
</html>

MY WorkSpace

After college light reading

Now that I'm out of college I can fianlly take the time to leaen how software is built in the real world :)

Tuesday, August 14, 2012

THE HTML DOM

http://www.w3schools.com/htmldom/dom_methods.asp
http://www.w3schools.com/htmldom/dom_nodes_access.asp

Java Script Notes + html

!
      <!--   script goes here  //--> #this format will hide the script from the user


A
  
action   =[url] #Specifies where to send the form-data when a form is submitted 

B
  boxes # pops up a box for the user to interact with
     types
          alert #alert("sometext");  use to alter user they missed something
          confirm#comfirm("some text"); use to confirm something
          prompt#prompt("some text",defaultValue)
                example# var userName=prompt("Please enter your name","Harry Potter");


  Break in line #<br /> #add a line break


 button # creates a button user can interact with
   Sub Types
         #button
             #<button type="button" onclick="myFunction()">



button methods # used to program the button
   #onclick
       #<button type="button" onclick="myFunction()">

D
  
   document  #javascript class with many function
       functions
                  .getElementById   #access html element
                         #document.getElementById("demo").innerHTML="My First JavaScript";
                   .write # write directly into the html document
                         #document.write("<p>My First JavaScript</p>");



F

<form> Creates a form for the user to enter data

 function <function name> #design your own java Script function f
     #function myFunction(){code goes here}



I

id " " id html element
    #<p id="demo">My First Paragraph</p>

input type = "type goes here "#create a moduel the user can interact with
  #button

R
   return #used to return a value at end of a function



S
 scr = "scriptname.js" # points and loads external java script file
 <script> Insert Java Script

 <script type =" script type">
     #text/javascript


U
<ul> #unordered list
    #followed by several <li> # list item
V

value # assign a value to a button (value is usally a string of text)
  

var  [var name] # declare a variable

Saturday, August 11, 2012

Notes on Git / GIT HUB

Git add # add file to the stage
 
  [file name] add file to stage
  . # add all file and sub directories to stage
  -all #add all file in current directory

Git commit # commit staged files
   -m  add a message# " message goes here "

git remote
  add # add a remote to remote list
  rm # delete a remote from the remote list

git remote add #example
     git remote add origin https://github.com/DanielHaro/my_project.git


git init # create a Git repository for current project

git push remote  # pushes code to remote site
    Master (or any branch) send code to stated branch

git pull remote # pulls data from remote and updates local data
    Master(or any Branch) grabs data from stated branch

How to start a new repsoitory
1) Declare the depository on your git site in my case git hub
2) in the root directory of your project issue command ->git init
3) issue-> git add .
4)issue ->git commit -m "inital commit"
5) create remote ie ->git remote add origin https://github.com/DanielHaro/my_project.git
6) git pull origin master
7) git push origin master
That's it your done :)


Music That I listen to while coding

I have been listening to theses groups lately

Aphex Twins
Massive attack
Orbital
Zero_Seven
Thievery Corp
OSunLade

WebSites That I have joined or use as a resource

General /tools

http://stackoverflow.com #programmers blog site and much much more
https://github.com # Code storage and sharing site
http://newrelic.com  # monitors web site performance and helps improve it
http://www.blogger.com # where I keep my programming notes :)
http://www.heroku.com # Launching site to test my web programs
http://www.linkedin.com # resume and serves as a central hub to my professional info /projects
http://www.behance.net  # Where I can display some of my more graphical programming projects


HardWare/ Supplies / Books / software
http://www.amazon.com # where I get most my programming books and spend most my money
http://www.newegg.com # they take what ever money is left over ->preferred hardware shop
http://www.studica.com # Great place to get compilers and other software at academic pricing


API/DOCS

https://developer.mozilla.org/en-US/docs/JavaScript/Language_Resources?redirectlocale=en-US&redirectslug=JavaScript_Language_Resources #deocs on java script

http://api.jquery.com/ #api for JQuery

http://ruby-doc.org/core-1.9.3/ # api for ruby

http://docs.oracle.com/javase/6/docs/api/ #api for java

http://gitref.org/ # docs for git

http://www.w3schools.com/default.asp # has docs / api /tutorial for many languages

Ruby Rails: switches

Rails new:
-T #do not generate a test Directory; do this to replace test with Rspec also need to install Rspec $rails generate rspec:install

HTML CHEAT SHEET :TAGS

%
<%=link_to 'link name', .... > create a link




B
<body> This is core html coding area: contains all data and interaction with the user
<br> single line break #has not end tag





H
<h#> HTML header # documented to only be (1-6)?
<head> container for the head elements # see http://www.w3schools.com/tags/tag_head.asp
<HTML>  root element also lets browser know that this is a HTML document

P
<p> paragraph tag, start a new paragraph #adds spaing and formating to document text


T

<tr> define a row
<th>header cell
<td>standatd cell
<table> create a table
<title> Create a title # this is a required <head> element

using Json in Java: non file output

I had to write some java code that used JSON format for the final out but did so with out using files:
Here was the soulition to make this happen

while(counter < numOfTags)
{
if(outList[counter].getType() == 0)
temp.addProperty(outList[counter].getTagName(),outList[counter].getFloatData());
else
temp.addProperty(outList[counter].getTagName(),outList[counter].getStringData());
counter++;
}
String outputString = mySon.toJson(temp);

notes:
int counter;
Device[] outList;
Gson mySon = new Gson();
JsonObject temp = new JsonObject();
int numOfTags;

pushing to HeroKu : ERROR sqlite3

I have not really got to the source of the problem, I even read somewhere that heroku does not allow sqlite for security reason: who know the really story of why this does not work. Regardless here is the fix/

Add this code to the ruby gem file and commit it to your project and then you will be able to upload to heroku.




group :production do
 gem 'pg', '0.12.2'
 end
 group :development do
 gem 'sqlite3', '1.3.4'
 end

Ruby programming !sort


I was searching for a quick way to sort objects in Ruby and I found this ingenious method online.
deviceArray.sort! {|a,b| a.getName.downcase <=> b.getName.downcase}

notes
deviceArray : an array of objects of type device
downcase : only needed if sorting alpa numerically or can use upcase

GIT HUB: Error while pushing

Error : did you run git update server....
 1. I fixed this by updating my remote: the problem is that the url is case sensitive\

Error: files out of Sync
 1. to fix this use git pull [remote name] [branch name] #branch name usally master

Error: No branch specified : add brach at end of push or pull comde