Obj-C: How to do basic math (Part II)

On March 20th, I showed you how to do basic math in Objective-C.  Now let’s take it a step further.

Suppose you need to add 4 numbers, and then divid that by 5.  The problem says that you must add the numbers first before dividing them by 5.

A long time ago (when I wrote in BASIC) I learned a technique that even works today.  You use the open bracket “(” and close bracket “)” to tell the computer what to calculate first.  When that is done, then it will go on to the rest of the problem.  Here’s how it works:

int a = 34;
int b = 12;
int c = 56;
int d = 87;
int answer = 0;
answer = (a + b + c + d) / 5;

Let’s break it down:

1. We initialize A – D with numbers for our math problem.
2. Since the answer variable will show the final answer, we initial it with the number zero (0).
3. The answer variable will first add what ever is in-between the brackets (), and then go on to divid that by 5.

The way to show the value of answer depends on your needs.  You could use a NSLog statement or a label.

That’s it.  Happy coding!

Obj-C: How to spell out Numbers as Text

ProgrammingMagifierYesterday, I showed you in this article on how to do basic math in Objective-C (and it could work in other languages as well).

Now today, I am going to show you a unique feature that you can put inside your apps – how to convert a standard number such as 934 and have the app convert them to letters – such as “nine hundred thirty four“.

But why would we need this?  Well, let’s say you were writing a app to write out checks.  You would need the app to print the spelled out version of an amount that a user would input in to a text field, or you’re writing an app that actually says a number to a user using speech.

For this we use a little known command called NSNumber.  We use it to format a number and then tell NSNumber how to format the number for final output to the user.

Again, this is a basic example.  For more complexed output, do a search on Google.com.

Let’s take a look at the below code:

NSNumber *value = [NSNumber numberWithInt:534];
NSNumberformatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterSpellOutStyle];
NSLog(@”Numeric: %@\nText: %@”,value,[formatter stringFromNumber:value]);
[NSNumberformatter release];

Now, let’s take a look at the code:

1.  Assign NSNumber the variable “value” and initialize it with the number 534.
2. Call the NSNumber formatter to get it ready to format the output to a user.
3.  Set the value *formatter to the NSNumberformatter command.
4.  Use the formatter value to tell the computer how to format the number.
5.  Print the result to the terminal.
6.  Release the NSNumberFormatter from memory.

That’s it.  Happy coding!

Obj-C: How to do basic math

MathTestMath has not been one of my strong points – even with the computer helping me throughout the years.  So, when I had to learn how to format a math problem using languages such as Objective-C, I was not a happy camper.

But formulating a math problem for the computer to see, figure out, and spit the answer out is easier then I thought it would be, especially for a math dunce like myself.

This article will cover the basics of how to give the computer a problem so it can give you a result.

For those of you who are using Xcode, this will be a simple program that will output the result to a NSLOG and print the answer to the console and everything will be done in the .m file.  Create a new single screen application and name it MATHTEST or something like that.

Here is the entire code.  I’ll explain what’s going on after….

    int x = 15;

    int y = 23;

    int answer = 0;

    answer = (x + y);

    NSLog(@”%d”,answer);

 Okay, let’s run down what’s going on here:

1.  We initialize the “X” variable to hold the number “15”.
2.  We initialize the “Y” variable to hold the number “23”.
3.  We initialize the “answer” variable with the value of “0”  *
4.  Let the value of answer add the
 and values (in this case, X = 15 and Y = 23).
5.  Now print the value of answer to the console (which should be 38).

That’s it….Happy coding.

You be the programmer!

HammerLogoWhen I started this web site, it was out of frustration of not finding any good coding examples for code that I needed to know quickly – especially with good-ol’ Android.

Now, it’s time for YOU to have a unique experience.  Over the next few days – or weeks – I am going to be posting code that may or may not have bugs in it.  The code may be for Android, iOS, or something else.  It is up to you to look at the code, copy it, run it, and then tell ME what’s wrong with it – if anything.

What do you get in return?  A chance to write an article about your findings that may be published on this web site for all to see.  The article must be on the topic that the code was written in.  How you found the error, what you did to fix it, and any hints and/or tips to get around the problem.  This is a unique opportunity to show others and the world what you’re made of.  If this unique opportunity takes off, your name may be included in a “top 100” programmers list that may appear on this web site later this year.

All challenges will begin with the subject Challenge followed by information of the challenge.  Then a small explanation will appear as to what the code is supposed to do, and then the code will follow.  Copy the code, run it, and see what happens.

This will begin in a few days – so get ready to show the world what you’re made of.  Good Luck!

 

How to develop for Windows or Windows Phone on a Mac

VirtualBoxLogoIf you want to develop apps for Android or iOS, then a Mac will do just fine.  The Android programming language has a GUI called Eclipse that will let one compile, test, and even upload the app to Google’s Play Store.

iOS has Xcode that programmers can use to develop Mac and iOS programs.

But what if you want to develop for the Windows platform?  One does not want to spend big bucks getting a Windows computer just to make Windows or Windows Phone apps.

Fortunately, their is a way.  You’ll have to obtain a full version of Windows though.

You can run the Windows environment inside of what is known as a “Virtual Window.”  To do this, you’ll need a Mac application that puts a virtual drive on your Mac for Windows to reside in.

Unlike what these companies would lead you to believe, you do not have to purchase the program.  You can download a program called VirtualBox from here.

Once the program is installed, follow the installation instructions.  Then, put the Windows installation DVD in your DVD drive.  Launch VirtualBox, and follow the screens to configure the “Virtual Drive” for the version of Windows you’re installing.

After Windows installs, go in to the operating system and download/install the required Visual Studio files as if you were on a “real” Windows machine.

Happy Coding!

Programming Basics: How to create and use a flowchart

So, you want to make an app for a company.  They have told you what they need and how they need it to work.  The app has to include simple instructions and commands so that anyone can use it.  You have the code in your mind, and how to program it.  But what about the most important thing in any program – how it will work (flow) for  a user?

Flow Charting has been around for a long time.  Some of the earliest programmers (myself included) used flowcharts to get a basic idea of how a program is going to work.  While most newer programmers don’t seem to use them as much, a flowchart can help you see how a program is going to work, and (in some cases) even act.

Most of today’s programming books even use flowcharts to help diagram what a program is doing when a user runs it.  But they do not seem to go in to how they came up with the flow or even go into some of the basics of what a Flowchart is or even does.

Let’s take a look at a real flowchart to see how it looks and works:

The above is a simple flowchart showing what a golfer does before they decide if they want to play golf or not.

All flowcharts start out with the word START.  This is how a programmer knows the chart is showing the beginning of a program.

1. Next, the golfer wants to see the weather on The Weather Channel.

2. He/she sees the weather, and then decides if they want to play golf.

3. If no, stay home and go to option #5.

4. If yes, get ready to play golf and go to option #5.

5. Then, since that block of code is done, stop the program altogether.

Each symbol of a flowchart has a meaning:

* An oblong circle usually means a flow is starting or ending.

* A rectangle means that a user is interacting with your program or outputting information to a device.

* A diamond means that the program has to make a decision (YES or NO).

* The lines in a flowchart show the flow of a program.

A flowchart can be small, medium, or large depending upon the complexity of a program.  I have seen flowcharts take up an entire banquet table a few times.

Happy coding!

Obj-C: Learning Objective-C, a Primer

If you’re just starting to consider developing for a platform such as iOS, then you’ll need a basic understanding of how the language is formed and formatted.

You’ll need to know the very basics (no matter if you think you know it all).

The one place to start with is by reading an Objective-C Primer.  This information contains basic information about the language and help you get ready to write and read code.

Their are many Primers on the Internet.  While most are very good (like this one for iOS) others assume way too much from the reader, such as, knowing how to initialize a variable.

How ever you decide to learn programming, do it slowly and learn it as best you can.  Despite what other people claim, you won’t know a language inside and out when you write your first program.

Android: Getting sample code for learning

As with any computer language, Android can be hard to learn and understand (especially for old programmers like myself) because one not only has to know the actual Android language, but they should also learn the Javascript language as well.  Sure, one usually learns one and then the other, but what happens when one day your boss assigns you a programming project in Android, and only gives you a week to do it (which has happened to me more then once).  So you get the books and watch videos, but nothing can replace looking at code.  When you do, you know that the programmer that supplied it for you has already gone through some heartache and moments that he or she wanted to throw the computer out the window until they got the code to work (now admit it, we’ve all been there!)

While you look at someone else’s code, keep in mind that every programmer (myself included) has their own way of doing something. What one programmer writes may not be the way you may write something – and despite what some pain in the ass teacher or programming book says, that’s okay.  My theory is, “As long as it works the way the end user needs it, who cares?”

 Like I said in an earlier article, “Programming is an art form – so treat it as such.”

Below is a list of resources that I use on a daily basis to help me in coding:

About.com
Google Android Developer (http://developer.android.com)
Google.com
iCodeBlog.com
Stackoverflow.com

That’s it for now.  🙂

Myths and Facts about Programming

When most people decide that they want to learn programming, they either learn it by reading books, looking at sample code, or going to college.  Which ever the person decides, they run in to myths about programming – either by accident or on purpose.

By accident, I mean that a person may hear something like “oh, if you learn programming, you’ll be able to make games”

By on purpose, I mean something like “if you learn programming, you must know each and every command before you even attempt to write your first program.”

While both statements can seem to be true – they are not.

First and foremost, you will not be able to memorize each and every command of a programming language or even know what it does and doesn’t until you have had experience in writing programs for a while.  I have been a programmer since 1984 (yes, I am old) and I still keep my reference books handy.  Also, most computer languages are changing almost daily – which means there may be a new (and better) way to do something.  Programming languages such as Android and iOS are good examples, as those companies add and subtract commands from their respective languages with each update.

Don’t be afraid to look at someone else’s code.  Even though every programmer has his/her own way of doing something, you can usually pick and choose what you need to know to get something working.

Choose how to books that talk in english and not seem that the writer is talking over your head.  I hate when the writer ass-u-mes that the reader has to know the history of a computer language and devotes an entire chapter to it.  Just teach me how to code – damn it!!!

No question is a stupid one – especially when it comes to programming.  If someone makes you feel like an ass for asking a question, go somewhere else for the answer.

Finally, writing code is an art form – so treat it as such.  You are in control of the way an end user will use your application – not the other way around.

With these tips in mind, you could become a great developer of something fantastic – just learn your ass off!

 

 

Where to learn programming (Part 2)

By: Dan Uff, Senior Editor

My last article about the above topic was so popular, that I decided to expand it below.

When someone learns how to program, they usually take college courses or go to their local Community College.  That’s all well in good, but that means possibly taking night or weekend courses.  If you work during the day like I do, that can suck.

Fortunately with the Internet, someone can take online courses in the privacy of their own home and on their time.  I am not talking about an on-line college such as University of Phoenix.  I am referring to courses that you can take on your own time and you only pay for the courses that you need.

Two of the most popular web sites are Udemy and Lynda.  I have tried both of these, and in my opinion, they both offer excellent ways to learn the basics and advanced programming techniques.  Both sites offer online course materials and also offer video step by step coding tips and techniques.

The courses can range from $99.00 and up depending on how in-depth you want to get.

They also offer video demos of a course either on their web site, or on YouTube.com.

For more information on these courses, click on the links above or do a search on Google.com for “online programming courses”.