Sunday, September 12, 2010

Java simplified




Java programs are either Applets or Applications. The first one runs under an
HTML environment in a browser window, the later runs standalone like we
are used to thinking about how programs run. Most of the commands and
features available to one are available to the other. Obviously Applets, since
they are running on someone's computer anywhere in the world must have
some restrictions (e.g.. cannot modify files on someone's computer)

Most of the standard commands and control statements in Java are quite
similar to those of other languages. Assignment statements, if statements,
for-next statements, Do - While statements, etc. are easy to understand and
pretty similar in most languages. However , using these statements to
accomplish some logical end is the first stumbling block that beginning
programmers need to overcome.

Understanding the various data types both primitive and non primitive is
of major importance to Java programmers. Primitive types are pretty obvious
and used in most languages on an everyday basis. Such types are :
int long short double float char Boolean etc.

Non primitive types are referred to as reference variables because they point
to memory locations that contain the values. Strings etc.

In addition to the above another important learning step is how to organize
our program so that it will work. Concepts such as top-down, or styles
that use a modular approach are quite popular. Modular means to divide
the logic into sections of code and then call each section as it is needed.
Think of modules are being small programs. These small programs can
also be called function or even METHODS. Remember that when you use
them , their purpose is to perform some task. Understanding the concept of
Methods is critical to appreciating the power of Java. In addition to Methods
performing tasks (just like a program, they can receive values (to play with)
and return results (if needed).

So in short programs are groups of Methods and variables interacting. In Java
we will refer to this grouping as CLASSES. So instead of writing programs
we are really writing CLASSES. And each of our Classes contains
METHODS (or member function) to perform specific tasks.

Now the real power explained. People have written many CLASSES already.
These CLASSES perform a wide range of tasks for you , so you in essence
do not have to reinvent the wheel every time you need something. These
CLASSES are bundled together into PACKAGES , which you import into
your program (at the top). Common packages are java.awt or java.lang or
java.applet etc. New CLASSES are being developed all the time. Bundled
into packages and made available to the programmer. (java.swing is an
example from our textbook). So in short you are not writing programs, you
are writing classes that can use existing classes to help them achieve their goals

When your class needs something that another class has to offer you can
normally do one of two things after the import of the package containing
the class:
1.. directly refer to a member function in that class to perform the task
String name = JOptionPane.showInputDialog("enter your name " )
2.. create an OBJECT of that class and then refer to the object along
with the member function so the task can be performed
Button red = new Button();
.... red.setBounds (1,2,44,23);

In summary the full power of java and object oriented programming is
realized when our class creates objects of existing classes which were
created from other classes. Classes can inherit the capabilities and power
of the classes from which they were derived and so on and so on.
So not only are we not reinventing the wheel , we are pulling a wheel of
the shelf that is already fine tuned and using that to go forward to design
yet even better classes to solve problems.

When designing our own classes there are way to allow access to certain
methods and variables and not allow access to others. In other words we
allow enough access to realize its power but not enough to mess it up.
I will spend more time on these and other concepts later.

Sorting Arrays

// Interchange sort high to low
// assume n[] is 10 big and it contains 10 numbers and is 'int'
// compares each element with each one under it
// visualize it as 9 sorts on an array that is getting smaller
int g,r,c;
for ( r=0; r <=8; r++)
   {
     for ( g=r+1;g<=9;g++)
        {
            if ( n[r]  < n[g] )
               {
                   c=n[r];                // these 3 statements swap values
                   n[r] = n[g];          // in the 2 cells being compared 
                   n[g] = c;
                }
        }  
   } 

// bubble sort on the same array above
// compares pairs of elements during a pass
// an element can only move up one spot each pass
// finished if nothing was swapped or at most n-1 passes

int sw,r,c,g;
do
   {
   sw = 0; 
   for ( r=0; r <=8; r++)
      {
        g=r+1;
        if (n[r] <  n[g] )
          {
             c=n[r];              // next 3 statements swap 2 values
             n[r] = n[g];
             n[g] = c;
             sw=1;
          }
      }
    }  while ( sw = 1 }

// QUICK SORT is coming soon
// Sorting using pointer technique is coming

Variable Types

Variable Types

Since Variables stand for items that the program is manipulating
or playing with,  such as:
                        whole numbers       (small  normal   large)
                        fractional numbers        (small   large)
                        single characters
                        groups of characters as a unit
                         true / false conditions
 it is only logical to assume that JAVA gives the programmer  the

 capability to declare and use Variables that can hold each of the

above data types.  Remember that values ( or data ) being put into
Variables is the heart or basis of programming. 

Variables MUST be declared before they can be used in a program.

Common Variable types are as follows:
   short                     ... small integers
   int                          ... normal integers
   long                       ... big integers
   float                       ... small fractional numbers
   double                   ... big fractional numbers 
   char                       ... single character values   e.g.,     M    or   F
   string                     ... groups of characters viewed as a unit
   Boolean                 ... logical  TRUE  or  FALSE  values 

In object oriented and more sophisticated programs today, the
programmer can declare his or her own data type ( later )

Specifics concerning Java Data TYpes
NUMERIC (all numbers are signed)

short integer (16 bit number) --> i.e. 32,768
int integer (32 bit number) --> i.e. 2 billion or 2 x 109
long integer (64 bit number) --> i.e. 9 x 1018
float real (32 bit decimal) --> i.e. 1.4 x 10-45 to 3.4 x 1038
double real (64 bit decimal) --> i.e. 4.9 x 10-324 to 1.7 x 10308
  CHARACTER / STRING

char a unicode (2-byte) char
the String class handles multi-character strings (strings are unicode, as well). Strings are said to be immutable: updates to a String cause the new string to be created in memory (and pointed to by the String class). However the old string is not actually "updated", rather, its reference is lost (and it becomes available for garbage collection).  
BOOLEAN

boolean (with values true, false)  
DATE

the Date class measures the milliseconds since Jan 1, 1970 and provides supporting functions to interpret and manipulate date/times  
ARRAYS

arrays hold multiple instances of a data type or object

Programming Tuturials

visit this website :Programming Tutorial

click this for advance learning

Sorting an Array

Hello programmers.. my students.. i want to create a program in java that sort a number in descending order.

here is a sample program...
public class Sorting
{
    public static void main(String args[])
    {
        int arr[]={0,1,2,3,4,5,6,7,8,9,10};
        int temp=0;
       
        for(int i=0; i<10;i++)
        {
            for(int j=0; j<10; j++)
            {
                if(arr[j]
                {
                    temp=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=temp;
                }
            }
        }
       
        for(int x=0; x<10; x++)
        {
            System.out.println(arr[x]+ " ");
        }
    }
}


i hope you learn more in my blog,.... Think it Code it

Programming Tuturials

hello programmers.... hello students... java programmers.. visit this website to study advannce.

http://webpages.shepherd.edu/JROMANO/arrays.htm

Array Definition and samples

An array is multiple storage locations under one name. For now
we will talk about one and two dimensional arrays.

All the primitive data types that we have learned and used concerning
regular variables also apply to arrays.

int num = new int; // declare / initialize an integer variable

int nums[] = new int [10]; // declare and allocate space for 10 integer
// numbers. The 10 elements have the same
// name but each one will be referred to
// with a different subscript or position
// in the array. Thos array contains 10
// integer locations, number 0 thru 9

Anything that you can do with an integer variable, you can do with
each element of an integer array. But you must indicate which element
you are referring to.

e.g.. nums[0]=100;
nums[5]=200; etc.
JOptionPane.showMessageDialog(null,"answer is "+nums[2]);

Loops make working with arrays very easy.

for example:
int tot=100;
int n=0;
do
{
nums[n] = tot;
n=n+1;
} while (n <= 9 );

Remember that all rules that apply to variables of a specific type
also apply to each element of an array of that type.

Two dimensional arrays adhere to all the rules noted above, except
any reference to a two dimensional array must have 2 subscripts.

int numbers[] [] = new int [5] [10];

The above array has 5 rows and 10 columns.
The rows are numbered 0 thru 4, and the columns are numbered 0 thru 9.


Any reference to the array will have 2 subscripts and the first one will be
the row and the second one will be the column.

e.g.. numbers[1][3] = 100;

Loops are very useful with two dimensional arrays just like one
dimensional arrays, but a lot of the times nested loops are used
because of the nature of two dimensional arrays.

e.g.. int tot=10;
for(int a=0;a<=4;a++)
{
for(int b=0;b<=9;b++)
{
numbers[a][b] = tot;
tot=tot+10;
}
}

.
arrays can be declared but still need to be explicitly initialized
int [] dataPoints; // declares the array of integer
dataPoints = new int[10] ; // allocates array size (now usable)
dataPoints = new int[numPoints]; // allocates array size with size specified by a run-time variable
an array can be re-initialized (with the same data type) at runtime, at which point old array entries are discarded
Java standard arrays cannot are not dynamically resizable like arrays in Visual Basic or PowerBuilder (i.e. where the language can dynamically extend the array, if needed). Java throws an exception if an out-of-bounds array index is referenced. Java.util's Vector class provides dynamic array-like capability (a Vector can store a collection of any type of object). The standard Vector class does not, however, limit data to a specific data type (data type limitation capability has to be built in a Vector extension).