Tuesday, September 29, 2015

String Functions

Strings are sequences of characters. They are useful for storing data and also for accepting user input, whether it be from a terminal command line or a web form.

I find these particular string functions indispensable in my own web programming. I'm sure there are many other frequently-used string functions.

ActionC++PHPJavaScript
Split a string into an array of stringsexplodesplit
Compare stringscomparestrcmp< or >
Find a character within a stringfindstrchrindexOf
Get length of stringsizestrlenlength
Find a string within a stringstrstrstrstrindexOf
Convert a string to hexadecimalstd::hextoString(16)
Get a substringsubstrsubstrsubstring
Convert string to floating pointatoffloatvalparseFloat
Convert string to integeratoiintvalparseInt
Upper case first charactertoupperucfirsttoUpperCase
Trim white spacetrimtrim
Replacereplacestr_replacereplace
Regular expression replacecpreg_replacereplace
Determine if string is a numberis_numeric!isNan

There are also a few useful functions that relate to single characters only, but they are usually used in the context of string processing.

ActionC++PHPJavaScript
Get the ASCII value of a characterchrfromCharCode
Determine if character is a digitisdigitctype_digitparseInt and slice
Determine if a character is upper caseisupperctype_uppercompare with toUpperCase


Sunday, September 27, 2015

ASCII Character Set

Hex Decimal Octal



Decisions Using the Switch Statement

You can also make decisions using the switch statment. It looks like this:

switch (exponent) {
    case 0 :
        // do nothing
        break;
    case 1 :
        input = input * 10;
        break;
    case 2 :
        input = input * 100;
        break;
    case 3 :
        input = input * 1000;
        break;
    default :
        // do nothing
        break;
}

The above code implements simple exponentiation for languages that do not support it natively. Note that each case is an integer. The general rule is that the different cases must be types that can be converted to integer at compile time. The computer science term for such types is "enumerable." Integers, characters, and enumerations would be valid here. (An enumeration is just a way of giving names to different integers, like constant literals.) Characters are valid enumerable types because they can be considered a restricted set of integers. For example, the letter 'A' evaluates to the integer 65. See the ASCII character set for more information.

Friday, September 25, 2015

Programmer's Code Editor

It is always a good idea to use a good programmer's text editor for any kind of code development. Important features of a programmer's text editor include:
  • Syntax code coloring
  • Language-aware auto indent and formatting
  • Strict text file save with line ending options
If you are doing PHP or JavaScript programming and you don't need an IDE but you do need an editor, I recommend NotePad++ for Windows, TextMate for Mac, and Brackets for either Windows or Mac. I no longer recommend TextWrangler because it does not have language-aware formatting.

Please, never use a program like NotePad, WordPad, Microsoft Word, or TextEdit to edit code. They do not have the proper options to safely save your files as strict text. They also do not have code coloring or auto indent.


Integrated Development Environment (IDE)

If you are learning C++, Java, Swift, iOS programming, or Android programming, it is helpful, and maybe necessary, to use an integrated development environment (IDE) to write, debug, and test your programs.

An IDE contains all the traditional programming needs in one package:
  • Code editor
  • Debugger
  • Simulator
  • Project management
  • User interface development

Thursday, September 24, 2015

HTML Entities

These are some HTML entities I have used on this blog or use from time to time. This is not an exhaustive reference. These are just the ones I found most useful.

Beware: not all browsers support all these entities, especially the UTF-8 shapes. I have found this particularly true on mobile browsers.

EntityAppearanceName


Decisions and Branching

We must have a way for the program to make a decision, and then execute different code based on the decision. If the number is less than 10, we do one thing, otherwise we do something else. If the object is a rectangle, we obtain the area by multiplying its width by its height; but if the object is a circle, we obtain its area by multiplying its diameter by π.

Indeed, I would venture a guess that the majority of the time, popular computer programs are making decisions. (This is less so for scientific programs that do extensive numerical computations.)

The Resistor Color Code

I use this color code as an example in my classes for writing branching clauses, loops, and functions.

0Black
1Brown
2Red
3Orange
4Yellow
5Green
6Blue
7Violet
8Gray
9White

The color code works like this: there are (at least) three color bands on the resistor. The first band indicates the first digit in the resistance. The second band indicates the second digit. The third digit is a multiplier. 

So, for example, if the bands were Yellow, Violet, and Orange, this translates to 4, 7, and 3. The  resistance value would be 47 × 1000 or 47 K Ω.

The computation would look like this:

R = ((10 * a) + b) * 10 c

where a, b, and c are the translated values of the three color bands, in order.


Foundational Programming Concepts

I teach three programming classes. There are several concepts that I must teach in all my classes because they are so foundational. Once you have mastered these concepts in any programming language, it is easy to transfer the knowledge to other languages.

Wednesday, September 23, 2015

Typing the Apple Symbol

After seeing my post how to type the command key / cloverleaf symbol, someone asked for a similar post how to draw the Apple symbol.

As a digression, this is a story I heard. In the early days of Apple, when Susan Kare was designing icons for the first Macintosh operating system, they used the Apple symbol on the keyboard command key, so she put the Apple symbol in all the menus for keyboard shortcut. When Steve Jobs saw this, he something to the effect of "Apples everywhere, too many apples!"

Look now in the File or Edit menu of your Mac, and see all the keyboard shortcuts there. Imagine if these were all Apple symbols! Now you can imagine why SJ was taken aback.

So Susan Kare looked for a distinctive but non-threatening symbol, and she found the cloverleaf symbol in a book of symbols used on Swedish maps to indicate a point of interest while traveling.

Anyway, beware how you use the Apple symbol. As a solid symbol, instead of an outline, it can be imposing. In an HTML document (web page), you can type the Apple symbol by adding this HTML entity to your code:

&#63743;

It should look like this:


If you need to type the Apple symbol into a word processing document or other document on your Mac, the keyboard shortcut is Option-Shift-K.


Creating Colors with UIColor

The UIColor object lets you store colors and also set colors on objects in your storyboard. The UIColor object comes with some colors built in:


ColorRGB HexSample
UIColor.blackColor()000000
UIColor.darkGrayColor()555555
UIColor.lightGrayColor()aaaaaa
UIColor.whiteColor()ffffff
UIColor.grayColor()808080
UIColor.redColor()ff0000
UIColor.greenColor()00ff00
UIColor.blueColor()0000ff
UIColor.cyanColor()00ffff
UIColor.yellowColor()ffff00
UIColor.magentaColor()ff00ff
UIColor.orangeColor()ff8000
UIColor.purpleColor()800080
UIColor.brownColor()996633

However, there is also a UIColor method init() that lets you specify a custom color that is not in the list above. For example, supposed you wanted to use the color Chocolate, which is not in the above list. The RGB hex values for Chocolate are #D2691E. You could create this color with the following method call:
view.backgroundColor = UIColor.init (
    red: 0.82, green: 0.41, blue: 0.12);
Note that we have to convert the hex numbers to equivalent floating point numbers in the range 0.0 to 1.0 first. If you have the hex colors but not the floating point equivalents, you can build the math into your function call:
view.backgroundColor = UIColor.init (red: CGFloat(0xD2)/255, 
                                   green: CGFloat(0x69)/255, 
                                    blue: CGFloat(0x1E)/255);

Here is a link to the Apple documentation of UIColor.


Boolean Logic

Boolean logic is a way of describing relationships between ideas that can have the truth values TRUE and FALSE. We describe these relationships in a diagram called a truth table:

xyx OR yx AND y
FalseFalseFalseFalse
FalseTrueTrueFalse
TrueFalseTrueFalse
TrueTrueTrueTrue

Here is an example of boolean logic. Suppose we have a set of colored shapes. We might ask two kinds of questions. If we choose one of the colored shapes, we might ask: is it blue and a circle? If we choose a blue circle, it is blue and also a circle. If we choose a yellow circle, it is not blue and a circle, because it is yellow (not blue). If we choose a blue square, it is not blue and a circle, because it is a square (not a circle). So whatever shape we choose must satisfy both the requirements, that it be both blue and also a circle.

Tuesday, September 22, 2015

Useful Keyboard Shortcuts

These keyboard shortcuts are useful when working on the Mac. You can avoid using the mouse or trackpad, and save your wrists a lot of pain.

Windows has similar shortcut keys, but they use the Control key instead of the Command key.

⌘ Space Spotlight
⌘ Tab Switch application
⌘ S Save
⌘ D Desktop
⌘ Z Undo
⌘ X Cut
⌘ C Copy
⌘ V Paste
⌘ , Preferences
⌘ R Refresh
⌘ W Close Window
⌘ L Edit URL
⌘ N New file
⌘ F Find/Search
⌘ G Search again

Blogger lets you type HTML tables into the HTML tab, and it lets you add styles to your tables too.

Typing the Command Key (Cloverleaf) symbol

In an HTML document (web page), you can type the cloverleaf symbol by adding this string to your code:

&#8984;

It should look like this:


Make sure you include the ampersand, the hash (pound or sharp sign), and the semicolon at the end.

In a word processing document, such as Microsoft Word or Apple Pages, you need to go to the symbol insert function, and look for "point of interest" symbol. It is unfortunately not called the cloverleaf symbol, and you can't search for it within a word processing document that way.

In a pinch, you can always just search Google for cloverleaf symbol to get this information.


Running Windows on Mac

Running Windows on Mac is frustrating for two reasons. One, Windows itself is inherently frustrating if you are a Mac user. Two, the emulation software (Parallels, Fusion, Virtual Box) is slow.

Don't forget that in order to run Windows on a Mac, you must pay for the emulation software, and you must also pay for a legal copy of Windows. For the same price, you may be able to buy an inexpensive Windows laptop.

Which is more frustrating? Running Windows on a Mac, or running Windows on an inexpensive and possibly underpowered machine? Only you can decide. But be aware that you do have options.

If you use Windows relatively infrequently, running it on you Mac may be more economical.

Here are some reasons why I have Windows installed on my Mac:
  • To test my web sites on Internet Explorer.
  • To use Excel and PowerPoint functions that are not available in the Mac versions.
  • To compile C# code on Visual Studio.
  • To help my students who are running Visual Studio or WAMP on their Windows computers.
Here is some software you can run without actually installing Windows:
  • The Mac versions of Word, Excel, PowerPoint, and Outlook.
  • MAMP (the Mac equivalent of WAMP).
  • Eclipse or ADT for compiling Java programs and Android apps.
  • Mono for running C# code from the shell.

Blogger lets you insert styles and scripts

Click a color well to change the color of this text.

Red #ff0000
Orange #ff8000
Yellow #ffff00
Green #00ff00
Cyan #00ffff
Teal #008080
Blue #0000ff
Violet #ff00ff
Purple #008080
Black #000000
White #ffffff
Gray #808080

 

Macs I've known and loved

PurchasedModelComments
1985Mac 512KMy first Mac. Later upgraded to 512KE then Mac Plus.
1991Mac IIsiSJSU bookstore.
1992Powerbook 100Price Club (Costco).
1994Powerbook 165cMacWarehouse.
1997Performa 6400Ugly computer. Bought from Small Dog Electronics.
2003Power Mac G4 MDDPowerful computer at the time. Bought from Mac Pro in San Jose.
2005Powerbook G4 AluminumBeautiful computer. Mac Pro in San Jose?
2009MacBook Pro 15”Tech Restore in Concord. My first Intel Mac. I needed it to run Windows.
2012Mac MiniA little powerhouse.
2013MacBook Pro Retina 13”I use it for PowerPoint and teaching my classes.
2014iMac RetinaThe retina screen is beautiful.

This cool Tableizer web site lets you create an HTML table from spreadsheet data or tab-delimited data and you can then insert the HTML into your web site. In Blogger, you must paste the resulting HTML into the HTML tab on your post, not the Compose tab.


My advice for buying a Mac

I first compiled this information in 2012 to help some friends who wanted to buy Macs. I updated it in early 2014 to help some students who were taking a class in Mac/iOS programming. (Such requires use of a Mac; it cannot be done on a PC.) Now I'm updating it again.

This is what entitles me to give this advice: I have used many different Macs since 1985, and I buy about one every other year, so I've been doing this for a long time. I have bought both brand new and refurbished. I have opened almost every Mac I've ever owned to upgrade it (except the new ones that can't be opened.) I am stingy, so I always look for the best deal and the best strategy.  

[1] Your friend who works at Apple may be able to get you a discount. However, Apple employees are inundated with such requests from their friends, and they have a limited number of discounts to use, so employees usually reserve the discounts for their family members.


[2] Don't be afraid to buy a refurbished Mac. Apple sells them for about 15% less than list price. A refurbished computer has been returned to a tech who checks everything and fixes what may be broken. Many new computers ship with problems or even DOA. A refurbished computer has been checked a second time beyond the normal factory testing. (This is true of other computer brands besides Apple too.) Be careful: you may be buying last year's model.