Programming

Java Concepts: Objects; Classes, Variables, and Methods

tl;dr (too long; didn’t read) – Java uses classes which declare variables that store information and are modified/retrieved by methods once an object of that class is instantiated. The next post will contain code complimenting these concepts. // Continue if you want to read more. /* Java is an object-oriented programming language, but just what is an object? To find out, we must first delve into the concept of classes. Classes are “templates” of objects. These “templates” dictate all the variables, or “data,” and methods, which are essentially “actions,” that the still unknown object will have. The class will have a name, along with variables and methods. The only time you may see these names is when you are instantiating an object from the class or calling (“executing”) a method of an object, which can only be done because of the code in its originating class. To instantiate means “to create an instance of,” which, if used in real-world context (although awkward), would be akin to saying “make me one of those, and make it just like the others.” Wait, I know I might have lost you by this point, but you needed to understand what variables, methods, and classes were before an object made any sense. We know a class is a template, right? Well, consider that you can instantiate multiple objects based on this class. What this means is that they are absolutely identical in every way except for maybe their data and name, which we normally call a “reference” (because it’s actually referring to a location in memory, a hexadecimal number that would glaze your eyes over!). If a metaphor would help, they’re all identical twins that know different things, but can do the same actions! So, even though you declare variables and methods in a class, when a variable is modified or a method is run they are not affecting the class but the object that was instantiated! Now it makes sense suddenly, doesn’t it? But wait! How does this “instantiation” thing work you ask? Good question, through a type of method (yes, these are methods) called constructors! What they do is (hold onto your hats because I’m using the next word in a different sense) instantiate the variables of an object so that the object knows what data is has in case it has to do something. Now wait, we just instantiated the object, why instantiate the variables; moreover, what’s the difference? The answer is we haven’t instantiated the object yet, we need to set values to the variables so that the object has data. Only then should the object be considered instantiated! So now we know what an object is: an instance of a class that has variables and methods dictated by the class. You might be wondering about methods at this point. Methods have, traditionally, two types: accessors, and mutators. What’s an Accessor? Well, quite simply, it accesses and returns (“gives back”) data stored in the object. Nothing too special, right? Mutators, on the other hand, can do all sorts of creative things but normally modify the data of an object in some way. You won’t always get data back from these, nor will they always tell you whether they successfully executed, but their purpose is clear: when something gets changed, this is the type of method it becomes. Something to note is that this is simply a naming convention, not a requirement. You can call them “Destroyer” methods and nobody can really say anything about it, as the compiler will not complain and your code will still run. Despite this, I encourage you to think of others who may read your code, and to try your best to keep to a standard  and/or convention! The next post will actually involve some code examples of all these concepts, which will be linked here upon creation. If you’re still having trouble, don’t fret, keep reading up on the concepts as much as you can and, if necessary, ask for metaphors or similes! */

Read more

Java: An Introduction to a Notorious Object-Oriented Language

tl;dr (too long; didn’t read) – Java is infamously difficult according to many programmers, but it’s good to know. The next post delves into terminology and concepts. // Continue if you want to read more. /* Java is notorious as being one of the most difficult to learn for beginner programmers, but don’t worry! This just means when you grasp it, the other object-oriented languages such as C# (Microsoft’s take on Java) or C++ will be a snap to pick up. It’s like learning Latin before you learn Spanish or French! Now, in no way am I saying Java was the first language, because it was probably (I may be wrong here) Assembly or some low-level language and you’ll worry about that if you have to take “Fundamentals of Computer Systems” or require binary/assembly code in whatever it is you’re doing such as x86; however, we’re not learning a low-level language, we’re learning a “high-level language.” The reason Java is considered a high-level language is because of the way it interacts with the system. Not only does it have its own compiler (think of it as a big translator that pre-builds programs for now) and “garbage collector” (which automates memory management) but it also has a virtual environment in which it is run, making it universally available among the operating systems. These are all reasons it’s favorable to have some idea how to code in Java, even if you’ll be writing mostly Javascript or C#, etc. In the next posts we’ll discuss what a class is, what a method and its different types are, what a variable and its different types/scopes are, and how they all fit together to create an instantiated entity or “object.” */

Read more

Track Referer and Entry Page with a Cookie

I found this simple script on the internet by Ammon Johns. He was nice enough to share it as long as we give credit. His script was originally to track a referer. It sets a “referrer” cookie which you can pull into any document using the php code <?php echo $_COOKIE[‘referrer’] ?>. I hope that helps someone, it took me forever to figure it out. There is a way to pull the information with javascript too something like “document.cookie.referrer or some such, but i’m no expert in javascript yet. I wanted to track the entry page as well because I had another script assigning a variable of k_wcid (omniture) to any person who came into our site through google’s PPC (Pay Per Click) Adwords campaign. This way we coudl track what site and keywords they came from with the referrer, and what page they landed on, which would also tell us if it was PPC or not.   The following code creates the cookie “URL” which can be called in PHP with <?php echo $_COOKIE[‘URL’] ?> The funny thing about setting and using these cookies is that you set the field with the value of “var=” (“URL=”, or “referrer=” for example)  and then call them withotu the “=”! Dont forget to give me some credit too! // JavaScript Cookie Code // Coding by Ammon Johns // www.webmarketingplus.co.uk // Modified for URL by Alexander Conroy “Geilt” // www.esotech.org – [email protected] // You may not modify or use this code except as stated in at // http://www.webmarketingplus.co.uk/promotions/cookie-tracking.html var cDomain = self.location.hostname; // cDomain becomes hostname www.vitalonehealth.com if(cDomain.indexOf(“.”) < cDomain.lastIndexOf(“.”)){ //Checks the number position of the domainname whcih is www. (4) vs the number position of .com which is 4.  if it is less then… var domainOffset = cDomain.indexOf(“.”)+1  //add 1 to the index number total, so if its 3 then it puts it to 4 cDomain = cDomain.substr(domainOffset); // domain now extracts 4 letters out. } if(document.referrer.indexOf(cDomain)==-1 && document.referrer!=”” && document.cookie.indexOf(“referrer=”)==-1){ var expDays = 90; var exp = new Date(); exp.setTime(exp.getTime() + (expDays*24*60*60*1000)); var refdate = new Date(); document.cookie = “referrer=” + escape(document.referrer); } if(document.cookie.indexOf(“URL=”)==-1){ var expDays = 90; var exp = new Date(); exp.setTime(exp.getTime() + (expDays*24*60*60*1000)); document.cookie = “URL=” + escape(document.URL); } var allCookies = document.cookie; var cPos = allCookies.indexOf(“referrer=”); if(cPos != -1){ var cdstart = cPos + 9; var cdend = allCookies.indexOf(“;”, cdstart); if(cdend == -1) cdend = allCookies.length; var cookieContent = allCookies.substring(cdstart,cdend); cookieContent = unescape(cookieContent); var cdatestart = cookieContent.indexOf(“&&&”, 0); var cdateend = cookieContent.length; var cRefer = cookieContent.substring(0,cdatestart); var cDateRef = cookieContent.substring(cdatestart +2,cdateend) } else{ var cRefer = “No cookie”; var cDateRef = “No cookie”; }

Read more

PHP Option Select Menu Generator Function – Dropdown Menu

http://snipplr.com/view/43100/php-option-select-menu-generator-function–dropdown-menu/

Read more

Code Ascetic – SEO Tracking in 4 Lines of Code

I am going to save you all a bunch of headaches and time staring into the sky for the SEO God’s to grant you an effective way to track the conversion from traffic to lead. It is as simple as a few lines of PHP code. I am sure there are equivalents in the other server side scripting languages that someone can kindly post in a reply. Here is the Snippet. if (is_null($_COOKIE[‘referer’])) setcookie(“referer”, $_SERVER[‘HTTP_REFERER’]); if (is_null($_COOKIE[‘entrance’])) setcookie(“entrance”, $_SERVER[‘PHP_SELF’]); What does this do? It checks if there is a current cookie for both the website passed Referer value and if there is a landing page value. If not, it grabs the referer from the last website the person has been too (including organic google search) and the page that they landed on, for instance, index.htm or /affordable-health-insurance/ becomes the value of $_COOKIE[‘entrance’] An easy way to check how your cookies are being set is through this simple PHP Script. You can save a file as test.php and include only the following code: print_r($_COOKIE); This prints the cookies you have set in order. Note: Most Dynamic CMS will already have a bunch of Cookies set that you can take advantage of once you figure out their name value through that smalls cript. The key to web programming is being able to do the most actions with the least amount of code and processing cycles / memory usage. This is similar to being an code ascetic, the more you can go without will bring you to a state of efficiency not many can achieve.  Keep this method for your records! Keep this ideology in your coding! Funny, I reduced this from 6 lines to 4 when i redid this Website and copied content over 😉

Read more

Why “GET” Can Be a Problem Over “Post”

This has to do purely with browser limitations. I recently encountered a major error using a Dialer management system that uses massive GET requests that fill the header with every parameter. I tried to create a custom query and save it so that I would be able to use it again. Unfortunatly it would return a error in internet connection page. I called up the company to find out what was going on. As usual, it is a problem they have never seen before and I was able to create an issue that can be a major problem to them in the future. The problem was that You can only have up to 2083 characters in the url for IE. I was trying to create a query with about 40 states included as well as other custom fields in their search feature. Knowing this it makes me wonder why even bother with the lack of security of PHP GET over POST. POST is cleaner, easier to manage and limits what the user can do without clear access. This makes for better programming and the necessity to check for problems and redundancies. The solution is simply resolved by switching to firefox which allows for longer URL Strings in the Header…but their software was “built” for IE. Ah well.  

Read more