14 May, 2009

Math for Programmers

1. Math is a lot easier to pick up after we know how to program. In fact, if we're a halfway decent programmer, we'll find it's almost a snap.

2. They teach math all wrong in school. Way, WAY wrong. If we teach ourself math the right way, we'll learn faster, remember it longer, and it'll be much more valuable to us as a programmer.

3. Knowing even a little of the right kinds of math can enable we do write some pretty interesting programs that would otherwise be too hard. In other words, math is something we can pick up a little at a time, whenever we have free time.

4. Nobody knows all of math, not even the best mathematicians. The field is constantly expanding, as people invent new formalisms to solve their own problems. And with any given math problem, just like in programming, there's more than one way to do it. We can pick the one we like best.

5. Math is... ummm, please don't tell anyone I said this; I'll never get invited to another party as long as I live. But math, well... I'd better whisper this, so listen up: (it's actually kinda fun.)

Java vs. Python Productivity

#################################################
JAVA:---------------

statically typed:>>>>>>>>>>>>>>>>>>

In Java, all variable names (along with their types) must be explicitly declared. Attempting to assign an object of the wrong type to a variable name triggers a type exception. That's what it means to say that Java is a statically typed language.

Java container objects (e.g. Vector and ArrayList) hold objects of the generic type Object, but cannot hold primitives such as int. To store an int in a Vector, you must first convert the int to an Integer. When you retrieve an object from a container, it doesn't remember its type, and must be explicitly cast to the desired type.

PYTHON:-----------

dynamically typed:>>>>>>>>>>>>>>>>>>>>>>

In Python, you never declare anything. An assignment statement binds a name to an object, and the object can be of any type. If a name is assigned to an object of one type, it may later be assigned to an object of a different type. That's what it means to say that Python is a dynamically typed language.

Python container objects (e.g. lists and dictionaries) can hold objects of any type, including numbers and lists. When you retrieve an object from a container, it remembers its type, so no casting is required.

#################################################
Example:>>>>>>>>>>>>>>>>>>

In the following example, we initialize an integer to zero, then convert it to a string, then check to see if it is empty. Note the data declaration (highlighted), which is necessary in Java but not in Python. Notice also how verbose Java is, even in an operation as basic as comparing two strings for equality.

JAVA:------------------

int myCounter = 0;
String myString = String.valueOf(myCounter);
if (myString.equals("0")) ...

// print the integers from 1 to 9
for (int i = 1; i < 10; i++) {
System.out.println(i);
}


PYTHON:----------------

myCounter = 0
myString = str(myCounter)
if myString == "0": ...

# print the integers from 1 to 9
for i in range(1,10):
print i

#################################################
EXAMPLE:>>>>>>>>>>>>


Your application has an Employee class. When an instance of Employee is created, the constructor may be passed one, two, or three arguments.

If you are programming in Java, this means that you write three constructors, with three different signatures. If you are programming in Python, you write only a single constructor, with default values for the optional arguments.

JAVA:------------

public class Employee
{
private String myEmployeeName;
private int myTaxDeductions = 1;
private String myMaritalStatus = "single";

//--------- constructor #1 -------------
public Employee(String EmployeName)
{
this(employeeName, 1);
}

//--------- constructor #2 -------------
public Employee(String EmployeName, int taxDeductions)
{
this(employeeName, taxDeductions, "single");
}

//--------- constructor #3 -------------
public Employee(String EmployeName,
int taxDeductions,
String maritalStatus)
{
this.employeeName = employeeName;
this.taxDeductions = taxDeductions;
this.maritalStatus = maritalStatus;
}
...


PYTHON:-------------

class Employee():

def __init__(self,
employeeName, taxDeductions=1, maritalStatus="single"):

self.employeeName = employeeName
self.taxDeductions = taxDeductions
self.maritalStatus = maritalStatus
...
#############
In Python, a class has only one constructor. The constructor method is simply another method of the class, but one that has a special name: __init__



#################################################

APPENDIX: About static vs. dynamic typing, and strong vs. weak typing, of programming languages.

JAVA:-----------

In a statically typed language, every variable name is bound both (1) to a type (at compile time, by means of a data declaration) and (2) to an object. The binding to an object is optional — if a name is not bound to an object, the name is said to be null. Once a variable name has been bound to a type (that is, declared) it can be bound (via an assignment statement) only to objects of that type; it cannot ever be bound to an object of a different type. An attempt to bind the name to an object of the wrong type will raise a type exception.



PYTHON:----------------

In a dynamically typed language, every variable name is (unless it is null) bound only to an object. Names are bound to objects at execution time by means of assignment statements, and it is possible to bind a name to objects of different types during the execution of the program.


#################################################
Email Me:-------- codes47@gmail.com

guess...????? About java and python

##########################################
JAVA:---------
statically typed:>>>>>>>>>>>>>>

The classic "Hello, world!" program illustrates the relative verbosity of Java.

In Java, all variable names (along with their types) must be explicitly declared. Attempting to assign an object of the wrong type to a variable name triggers a type exception. That's what it means to say that Java is a statically typed language.

Java container objects (e.g. Vector and ArrayList) hold objects of the generic type Object, but cannot hold primitives such as int. To store an int in a Vector, you must first convert the int to an Integer. When you retrieve an object from a container, it doesn't remember its type, and must be explicitly cast to the desired type.

PYTHON:--------

dynamically typed:>>>>>>>>>>>>>

In Python, you never declare anything. An assignment statement binds a name to an object, and the object can be of any type. If a name is assigned to an object of one type, it may later be assigned to an object of a different type. That's what it means to say that Python is a dynamically typed language.

Python container objects (e.g. lists and dictionaries) can hold objects of any type, including numbers and lists. When you retrieve an object from a container, it remembers its type, so no casting is required.

#########################################
JAVA:--------

verbose:>>>>>>>>>>>>>>
"abounding in words; using or containing more words than are necessary"

PYTHON:--------

concise (aka terse)>>>>>>>>>>>
"expressing much in a few words. Implies clean-cut brevity, attained by excision of the superfluous"
#########################################

JAVA:-----
public class HelloWorld
{
public static void main (String[] args)
{
System.out.println("Hello, world!");
}
}


Python:----

print "Hello, world!"

&

print("Hello, world!") # Python version 3
##########################################
EXAMPLE:>>>>>>>>>>>>>>>>>>>>

In the following example, we initialize an integer to zero, then convert it to a string, then check to see if it is empty. Note the data declaration (highlighted), which is necessary in Java but not in Python. Notice also how verbose Java is, even in an operation as basic as comparing two strings for equality.

JAVA:---------

int myCounter = 0;
String myString = String.valueOf(myCounter);
if (myString.equals("0")) ...

// print the integers from 1 to 9
for (int i = 1; i < 10; i++) {
System.out.println(i);
}

PYTHON:----------

myCounter = 0
myString = str(myCounter)
if myString == "0": ...

# print the integers from 1 to 9
for i in range(1,10):
print i
#########################################
BEST EXAMPLE:>>>>>>>>>>>>>>>

Your application has an Employee class. When an instance of Employee is created, the constructor may be passed one, two, or three arguments.

If you are programming in Java, this means that you write three constructors, with three different signatures. If you are programming in Python, you write only a single constructor, with default values for the optional arguments.

JAVA:>>>>>>>

How to Improve your Skills as a Programmer

Gather complete requirements.
Take the time to write down what the end product needs to achieve. Clarity of thought at this stage will save a lot of time down the line.
Write an implementation plan (or model).

For something small and self-contained, this might just be a basic flowchart or an equation.
For larger projects, it helps to break it into modules and consider what job each module must do, how data gets passed between modules, and within each module how it will function.

Although it is fun to dive straight into code, it is equally tedious to spend hours debugging. By taking the time to design the structure on paper, you will drastically reduce your debugging time (and you may also spot more efficient ways of doing things even before you write the first line of code).

Add Comments to your code.

Whenever you feel your code needs some explanation, drop some comments in. Each function should be preceded by 1-2 lines describing the arguments and what it returns. (Comments should tell you why more often than what. Remember to update the comments when you update your code!)

Use naming conventions for variables.

It will help you keep track of what type the variable is and also what it's purpose is. Although this means more typing than x = a + b * c, it will make your code easier to debug and maintain. One popular convention is Hungarian notation where the variable name is prefixed with its type. e.g. for integer variables, intRowCounter; strings: strUserName. It doesn't matter what your naming convention is, but be sure that it is consistent and that your variable names are descriptive. (See Warnings below)
Organize your code.

Use visual structure to indicate code struture. i.e. indent a code block that sits within a conditional (if,else,...) or a loop (for,while,...) Also try putting spaces between a variable name and an operator such as addition, subtraction, multiplication, division, and even the equal sign (myVariable = 2 + 2). As well as making the code more visually elegant, it makes it much easier to see the program flow at a glance. (See tips on indentation below)
Test.

Start by testing it with inputs that you would typically expect. Then try inputs that are possible but less common. This will flush out any hidden bugs. There is an art to testing and you will gradually build up your skills with practice.
Write your tests to always include the following:

Extremes: zero and max for positive values, empty string, null for every parameter.
Meaningless values, Jibberish. Even if you don't think someone with half a brain might input that, test your software against it.
Wrong values. Zero in a parameter that will be used in a division, negative when positive is expected or a square root will be calculated. Something that is not a number when the input type is a string, and it will be parsed for numeric value.

Practice. Practice. Practice.

Be prepared for change.

In a realistic working environment, requirements change. However, the clearer you are at the start about the requirements and the clearer your implementation plan, the less likely those changes will be down to misunderstanding or "Ah, I hadn't thought of that" scenarios.

You can take an active role in improving clarity of thinking by presenting your requirements document or your implementation plans before coding to ensure that what you are planning to create is actually what's been asked for.
Structure the project as a series of milestones with a demo for each block. Approach the programming one milestone at a time - the less you need to think about at any moment, the more likely you will think clearly.
Start simple and work towards complexity.

When programming something complex, it helps to get the simpler building blocks in place and working properly first.
For example, let's say you want to create an evolving shape on screen that follows the mouse and where the degree of shape change depends on mouse speed.

Start by displaying a square and get it to follow the mouse, i.e. solve movement tracking on its own.
Then make the size of the square relate to mouse speed, i.e. solve speed-to-evolution tracking on its own.
Finally create the actual shapes you want to work with and put the three components together.
This approach naturally lends itself to modular code writing, where each component is in its own self-contained block. This is very useful for code reuse (e.g. you want to just use the mouse tracking in a new project), and makes for easier debugging and maintenance.
 
© 2009 Hitting codes Stealthily. All Rights Reserved | Powered by Blogger
Design by psdvibe | Bloggerized By LawnyDesigns