Thursday, March 25, 2010

Operators - This operation is not painful

Operators – An operator is a symbol or letter, which makes the computer perform specific operation in a programming statement.

Assignment Operator =Assigns a value to a variable or property.
variable = value
variable : Any variable or any writable property.
value : Any numeric or string literal, constant, or expression.

Remarks
The name on the left side of the equal sign can be a simple scalar variableor an element of an array. Properties on the left side of the equal sign can only be those properties that are
writable at run time.

Arithmetic Operator - Operators that perform numeric calculations known as arithmetic operators are: + (addition), – (subtraction), * (multiplication), / (division) and % (modulus).

Relational Operator - An operator that manipulates numeric and other values to produce a logical result. The relational operators are <, <=, >, >=, = = and !=.

Unary Operator - The unary assignment operators, use two operand instead of one, for the increment (++) and decrement (– –) operators. Its also call
increment/decrement operator. a++, a--, a+=10.

Unary-Operator : one of & * + – ~ !

Ternary Operator – Can be used to replace certain of if – else-if statement. Its require three operands to work on. It represent by the ? and : symbols. Expression1?Expression2:Expression3. e.g. tax = salary>10000?salary*0.05 : 0; (if variable salary stored more than 10000 then tax variable will calculated 5% of salary otherwise tax will be).

Logical Operator - An operator that produces a logical result (true or false); sometimes called a Boolean operator. The logical operators are used for expression grouping, NOT or ! (negation), AND (Creates a Boolean behavior that represents the logical AND of the given behaviors. The behavior's value is true when both b1 and b2 are true; otherwise, it is false. OR (Creates a Boolean behavior that represents the logical OR of the given behaviors. The behavior's value is true when either b1 or b2 are true; otherwise it isfalse).

Binary Operator – This operator do the binary operations between two or more variables. Also known as bits operator. The bitwise operators perform bitwise-AND (&), bitwise-exclusive-OR (^), and bitwise-inclusive-OR (|) operations. E.g. 101 & 100 = 100 (Multiplication). 101 ! 100 = 101 (Addition). 101 ^100 = 001(If one bit is 0 and the other bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0). Right shift  >>; Left shift << (The value of a right-shift expression e1 >> e2 is e1 / 2e2, and the value of a left-shift expression e1 << e2 is e1 * 2e2).

One thing I can assure you that these operators will never ditch you. So, use these and if want anything can leave a comment or mail me back.

!!!Once more time has come to say you  Tofa, see you next week.!!!

Thursday, March 18, 2010

Built-in-Functions (String) – Enjoy it!

Let us plunge to another episode of functions, I know it is boring; at the same time it is most handy when you want to finish a program with lots of mathematical formula. Last week I have gone through a few numerical functions and the way functions help us for converting one data type to other. Today mainly I will discuss about string function, which only valid to string type data. String handling is not an easy job and you will find many functions available for this, but often while you write a program, you will be asked to write your own functions only and prohibited from using some of the string function. So, how to do this I will come to this when we talk about programming. String functions in Java return value is string but some return integer also. In java java.lang is default package so you do not need any extra package to be import but in C/C++ you require "string.h".

Note : Str - is the String argument you have to pass and Num is a Number of character and pos is the position.

DescriptionBasicC/C++Java
Find the length of a string value
len(Str$)

strlen(Str)

Str.length()
Extract a character from the said string from given
position

mid$(Str$,pos,num)

str(pos)

Str.charAt(pos)
Check equality between two string and return a
Boolean value/

Str1.equals(Stri2)
Check equality between two string ignoring he case
difference and return a Boolean value.

Str1.equalsIgnorecase(Str2)
Compare between two string( relevant to the ASCII
values of each character), if both equal it return 0, if String1 is
smaller than String2 then it will return -1 and if String1 is bigger
than String2 then it will return 1.

strcmp(Str1,Str2)
strcmpi(Str1,Str2)
(strcmpi() - for ignoring
case)

Str1.compareTo(Str2)
Extract all the string from given position till end.

String.substring(int)
Extract all the string from starting position up to
left of end position.

mid$(Str$,pos,num)
left$(Str$,num)
right$(Str$,num)
Using any loop from 0 position till
length of the string, we can extract character by character
Str1.substring(int,int) -
Join String2 to the right of String1Str1$+Str2$strcat(Str1,Str2)
Str1.concat(Str1)
This can be done by:
String1=String1+ String2.
Returns the index within this string of the first
occurrence of the specified character.

Str.indexOf(char/string)
Returns the index within this string of the first
occurrence of the specified character, starting the search at the
specified index.

Str.indexOf(char/string,
int)
Returns the index within this string of the last
occurrence of the specified character.

Str.lastIndexof(char/String)
Tests if this string starts with the specified
prefix.

Str.startswith(String)
Tests if this string ends with the specified suffix.
Str.endsWith(String)
Removes white space from both ends of this string.
Str.trim()
Returns a new string resulting from replacing all
occurrences of oldChar in this string with newChar.

Str.replace(char, char)
Str.replace(Str1, Str2)
Converts this String to lowercase / Converts this
String to upper case.
strlwr(Str) / strupr(Str)Str.toLowerCase() / Str.toUpperCase()
Converts this string to a new character array.Str.toCharArray()
In java we also having StringBuffer, so giving you some idea of this class-

A string buffer implements a changeable sequence of characters. String buffers are safe for use by multiple threads. The methods are
synchronized where necessary so that all the operations on any particular
instance behave as if they occur in some serial order. String buffers are used by the compiler to
implement the binary string concatenation operator +. For example, the code:

x = "a" + "4" +"c";

is compiled to the equivalent of:

x = new StringBuffer().append("a").append(4).append("c").toString()

Functions under StringBuffer
DescriptionJava
Constructs a string buffer with no
characters in it and an initial capacity specified by the length
argument.

StringBuffer()

Constructs a string buffer with no
characters in it and an initial capacity of 16 characters.

StringBuffer(int)
Constructs a string buffer so that it
represents the same sequence of characters as the string argument.

StringBuffer(String)
Appends the string representation of the
boolean argument to the string buffer.

append(boolean)
Appends the string representation of the
char argument to this string buffer.

append(char)
Appends the string representation of the
char array argument to this string buffer.

append(char[])
Appends the string representation of a
subarray of the char array argument to this string buffer.
append(char[], int, int)
Appends the string representation of the
int, float or double argument to this string buffer.

append(int) /append(float) /append(double)
Returns the current capacity of the
String buffer.

capacity()
Returns the character at a specific
index in this string buffer.

charAt(int)
Inserts the string representation of the
char argument into this string buffer.

insert(int, char) **
Inserts the string representation of the
char array argument into this string buffer.

insert(int, char[])
Returns the length (character count) of
this string buffer.

length()
Characters are copied from this string
buffer into the destination character array.

getChars(int, int, char[], int)
Sets the length of this String buffer.
setLength(int)
The character at the specified index of
this string buffer is set to ch.
setCharAt(int, char)
The character sequence contained in this string
buffer is replaced by the reverse of the sequence.
reverse()
Converts to a string representing the data in this
string buffer.
toString()
** Insert can be used as append for numeric too.

Here is a example, how we can use string buffer
System.out.println("StringBuffer insert and append example!");

StringBuffer sb = new StringBuffer(0);

System.out.println(sb.insert(0, "vinod"));

int len = sb.length();

System.out.println(sb.insert(len, "Deepak"));

System.out.println(sb.insert(6, "Raj"));

System.out.println(sb.append("Mohit"));



Database packages also provide some date and database functions, while writing about database I certainly cover those functions. I also remind you that if you feel anything left out then can suggest me, your suggestion will be always welcome.
!!!I know most of the students are busy with their respective examination and hardly have time to join this function, so Aburz for all.!!!

Thursday, March 11, 2010

Built-in-Functions (Maths) – Enjoy it!

Readymade with zero price tag and unlimited access, that is how I can describe a built-in-function. All programming languages having certain amount of built-in-function, which written as subroutine or function to solve a certain requirement, while writing a program you can, call these functions by sending arguments that required or with no arguments.

It is not that we cannot solve these, but when it is available, we just use it. Built-in-functions come in different types depending upon data types – Numeric, Date, String, Database etc. I will cover this topic in two weeks and with a few which we use frequently.

Numeric or math function to solve a mathematic routine like square root and other. Many math functions in Java return value is double type but you can take help of  type casting to get it in integer. In java java.lang is default package so you do not need any extra package to be import but in C or C++, you have to include "math.h" but for min() and max() in C/C++ you require
"stdlib.h" or often "complex.h"
Note : Num - is the numeric argument you have to pass.

Description

Basic

C & C++

Java

Returns the square root of a double value.

sqr(num)

sqrt(num)

Math.sqrt(num)

Returns the absolute value of a double value.

abs(num)

abs(num)

Math.abs(num)

Returns of value of the first argument raised to the power of the second
argument.

pow(num,num)

Math.pow(num,num)

Returns the smaller of two Numeric values.

min(num,num)

Math.min(num,num)

Returns the larger of two Numeric values.

max(num,num)

Math.max(num,num)

Returns the trigonometric sine, cosine and tangent of an angle where
argument should be in radian.

sin(num)
cos(num)
tan(num)

sin(num)
cos(num)
tan(num)

Math.sin(num)
Math.cos(num)
Math.tan(num)

Returns the exponential number  (i.e., 2.718...) raised to the
power of a double value.

exp(num)

exp(num)

Math.exp(num)

Returns the smallest (closest to negative infinity) double value that is
not less than the argument and is equal to a mathematical integer. 
ceil(12.5) = 13,
ceil(-12.5) = -12

cint(num)

ceil(num)

Math.ceil(num)

Returns the largest (closest to positive infinity) double value that is
not greater than the argument and is equal to a mathematical integer.
floor(12.5) = 12,
floor(-12.5) = -13.

fix(num)

floor(num)

Math.floor(num)

Returns a random number between 0.0 and 1.0

rnd(num)

random()

Math.random()

Return a double number that represents the closest integer to the double
parameter. rint(2323.43) =23.0, rint(2323.83) =23.0

 
Conversion - while writing a program we often have to change a data type to another. In C/C++, you need to include "stdlib.h". Here some example

Description

Basic

C & C++

Java

Integer to String

str$(num)

itoa(num,string,10) *

Integer.toString(num)

float to String
 -
do -

fcvt(num,ndigit,&dec,&sign)

Float.toString(num)

double to String
 -
do -

Double.toString(num)

String to Integer

val(string)

Integer.parseInt(String)

String to Float
 -
do -

Float.parseInt(String)

String to Double
 -
do -

Double.parseInt(String)
* 10 is the radix or base value, indicate that String value should be converted in Decimal. Binary (2), Octal (8) and in Hexadecimal (16).

Now these days Fortran and Pascal can be seen anywhere in the scenario, but some engineering Colleges having Fortran in their 3rd semester, so few Mathematical function for these programming languages are given below :

Description

Fortran

Pascal

Returns the square root of a double value.

sqrt(num)

sqrt(num)

Returns the absolute value of a double value.

abs(num)

abs(num)

Returns of value of the first argument raised to the power of the second
argument.

sqr(num,num)

Returns the trigonometric sine, cosine and tangent of an angle where
argument should be in radian.

sin(num)
cos(num)
tan(num)

sin(num)
cos(num)
tan(num)

Returns the exponential number  (i.e., 2.718...) raised to the
power of a double value.

exp(num)

exp(num)

This returns a real number whose value is the integer part of the real
value x. For example, Int(8.3), give you 8

int(num)

Int(num)

Modulus eg. mod(8,5) will give you remainder 3.

mod(num,num)

Next week coming with String functions. One thing I like to tell you, I often write my own packages for certain things and call my home made built-in functions, as I always like the challenge, but give you a tip too -
!!!Do not take too many challenges while appearing for exam, as exam itself is too challenging, happy finally time to say you Allez ciao!!!

Thursday, March 4, 2010

Methods or User Defined function- Sleek and beautiful

Methods in many programming languages are actually the Functions or Subroutines, are often referred to as 'methods'. Different programming languages provide different idea how a User Defined Function should invoked. Some programming language instead of UDF it is called as Subroutine. These are both refer to as Methods or User defined functions (UDF).

Function - a function is a sequence of commands or programming code that returns a value. A function that is available through a simple reference and specification of arguments in a given higher-level programming language. Also known as built-in procedure; intrinsic procedure; standard function.

Subroutine - a SUB is a sequence of commands or programming code, but it does NOT return a value. Subroutine used in Visual Basic, Visual Foxpro.

In Object Oriented Programming languages - A method is an object's abilities. Methods or functions are the modules from which java program is built. Method can be declared with -

access-specifier, modifier, type, method-name (parameter list), where access specifer is optional. Methods also known as functions. access-specifier – private/ public/protected.

Note that Interface method visibility is public by default. You do not need to specify the access modifier it will default to public. For clarity it is considered a good practice to put the

public keyword. The same way all member variables defined in the Interface by default will become static final once inherited in a class.

Access modifiers
You surely would have noticed by now, the words public, protected and private at the beginning of class's method declarations used, these keywords are called the access modifiers.

protected keyword is a modifier that can be used in the declaration of methods or variables. A protected variable or method is only visible within its class, within its subclasses, or within the class package.

private keyword is a modifier that can be used in the declaration of methods or variables. Using the private modifier in the declaration for either of these types hides the methods and variables so they cannot be directly referenced outside of the class they're declared in. One exception to this rule is that private methods or variables declared within a class can also be used by inner classes.

public keyword is a modifier that can be used in the declaration of classes and interfaces. The public keyword can also be used as a modifier in the declaration of methods and variables. Classes or interfaces declared as public are visible everywhere. Methods and variables declared as public are visible everywhere their corresponding classes are visible.

(Note that the protected, public, private keyword cannot be used in the declaration of local variables.)

type – Type is compulsory for a method declaration. Type declaration indicates that what types of values going to be returned.

void (no return) or int / float / double / Boolean / char / String –(if a function return a value it not compulsory to accept a parameter form caller method).

parameter list are variables or objects that receive the value of argument.

Function Call by value – When a simple type passed to a method, as a copy of the argument is being made and passed to the function is known as call by value, any changes made to the parameter inside the function do not affect the original. A functions parameter list also known as signature.

void test()
{
int a=10,b=20;
int s=sum(a, b);
}

Function call by reference – When an object, address of a variable or a pointer is passed as an argument to a method it is called call by reference. A reference to an argument is passed as parameter, the value of the argument is not passed. This method is called call by reference in java.

class testing
{
int a, b;
void test()
{
a=10,b=20;
testing t=new testing();
int s=sum(t);
}
}

Pure function – Pure function or accessor methods return information to the caller about a state of an object without changing the state.

Impure function – Pure function or mutator methods return information to the caller about a state of an object by changing the state.

actual parameter—the actual value that is passed into the method by a caller. Actual arguments (those supplied by the caller) are evaluated. Caller – sum(a, b)

formal parameter—the identifier used in a method to stand for the value that is passed into the method by a caller. Receiver – int sum(int x, int y)

Message passing - "The process by which an object sends data to another object or asks the other object to invoke a method. Also known to some programming languages as interfacing

static - The static keyword is used as a modifier for methods and variables. When the static keyword appears in a method or variable declaration, it means that there will be only one copy of the method or variable that each class object may reference, regardless of the number of instances of the containing class that are created.

Examples
Basic

1 DEF fnf (t) = 1.5 * t ^ 2 + 3.2 * t ^ 2 + 2.3 * x + 4.1 :'Function
10 CLS
20 x = 1
30 WHILE x < 2.5 40 y = fnf(x) : ' Function calling 50 PRINT y 60 x = x + .5 70 WEND 80 END

C / C++
//main program
void main()
{
int c;
c=sum(a,b);
printf(“%d”,c);
}

//sum functions
int sum(int a,int b)
{
return a+b;
}
Java //main program
public class
{
public static void main(String args[])
{
int c;
c=sum(a,b);
System.out.println(c);
}

//sum functions
int sum(int a,int b)
{
return a+b;
}

Hope you have a helluva of time attending this function, next week come back with Built-in Functions.

!!!These functions are really very heavily built, that’s why very helpful too, till then Sizobonana!!!