Tuesday, July 19, 2011

Sorting (Linear)

import java.util.Scanner;
public class sortTest
{
//find the minimum of two numbers
public static int findMinimum(double[] A, int i)
{
int j, min = i;

for(j=i+1; j if(A[j] < A[min]) min = j;
return min;



}

//swap A[i] and A[j]
public static void swap(double[] A, int i, int j)
{
double temp = A[i];
A[i] = A[j];
A[j] = temp;
}

//selection sort
public static void selectionSort(double[] A)
{
int i , j;
for ( i = 0; i {
int min = findMinimum(A,i);
swap(A,i,min);
}
}


//prints the sorted array
public static void printAllSorted(double[] A)
{
for(int i=0; i {
System.out.println(A[i]);
}
System.out.println(" ");
}

//prints all the elements smaller than A[j]
public static void printSmaller(double[] A, int j)
{
System.out.println(" ");
for(int i=0; i {
System.out.println(A[i]);
}
System.out.println(" ");
}

//searchs for key
//returns -1 if key not found, index of A[], if found
public static int linearSearch(double[] A, int key)
{
for(int i = 0; i {
if(A[i] == key)
return i;
}
return -1;
}



//main function
public static void main(String[] args)
{
double[] A = new double [10];
Scanner scn = new Scanner(System.in);
int key;

System.out.println(" ");
System.out.println(" ");

for ( int i=0; i {
System.out.println("Enter a number "+i);
A[i] = scn.nextDouble();
}

System.out.println(" ");

selectionSort(A);
printAllSorted(A);

System.out.println(" ");

System.out.println("enter the numer you want to search : ");
key = scn.nextInt();

int j = linearSearch(A,key);

if( j ==-1 )
{
System.out.println(" ");
System.out.println("Key not Found");
}
else
printSmaller(A,j);
}

}

Monday, July 18, 2011

02. How To Write JAVA Programmes

1. Basic Java Program


Code :

// Hello world program in JAVA

class hello
{
public static void main(String args[]) // this is the main method
{
System.out.println("Hello World \n");
}
}

Preview : 


2. JAVA Translation

Java Translation Process 
Java Source Code -> (Java Compiler) -> Java Byte Code -> (Byte Code Interpreter) -> Machine Language -> output

Java Source Code : .java file
Java Byte Code    : .class file

3. Comments & Conventions

// - Single line comment
/*...*/ - block comment ( multiple lines )
  • Java is case sensitive

Code :
public class secondFile
{
public static void main(String[] args)
{
System.out.print(" Java is a very portable language ");
System.out.println(" and can run in different tpes of computers. ");
System.out.println(" this is next line!!!! ");
}
}

Preview : 





4. Keywords


abstractcontinuefornewswitch
assert***defaultgoto*packagesynchronized
booleandoifprivatethis
breakdoubleimplementsprotectedthrow
byteelseimportpublicthrows
caseenum****instanceofreturntransient
catchextendsintshorttry
charfinalinterfacestaticvoid
classfinallylongstrictfp**volatile
const*floatnativesuperwhile


*not used
**added in 1.2
***added in 1.4
****added in 5.0


source for the keywords : http://download.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html



5. Identifiers 

Identifiers are Case senstive.
Names given for variables , classes and methods

VALID : employeeName, Emp_number, $Emp_salary
INVALID : employee name, #emp, class
"employeeName" method is called the camel method.


6. Data Types

Primitive Data Types:
Data TypeDefault Value (for fields)
byte0
short0
int0
long0L
float0.0f
double0.0d
char'\u0000'
String (or any object)  null
booleanfalse

Refernce Data Types: 

Array
Class
Code :
public class tempConv
{
public static void main(String[] args)
{
float celsius = 100;
float fahrenheit ;
//convert to Fahrenheit equivalent
fahrenheit = 32 + ((9 * celsius) / 5 );
System.out.println("celsius temprature");
System.out.println("  " + celsius);
System.out.println("Equivalent Fahrenheit Temperature ");
System.out.println("  " + fahrenheit);
}
}


Preview : 



01. Introduction

Introduced in 1991 by Sun Microsystems, Inc.

Purely an object oriented language and has features from both C++ and SmallTalk.

Can Develop :
  • Console Applications
  • Web Applications
  • GUI Applications
Make sure to install the Java Development Kit and Notepad++ v5.9.2 first of all.

Java Development Kit (JDK - Java SE) : http://java.sun.com
Notepad++ v5.9.2 : http://notepad-plus-plus.org/