Showing posts with label source code. Show all posts
Showing posts with label source code. Show all posts

Saturday, 30 April 2011

Sorting an array using selection sort (java)

public static void main (String [] args) {
int n;
Scanner sc = new Scanner (System.in);
System.out.print ("Enter how many elements");
n = sc.nextInt();
int [] a = new int [n];

//input array
for (int i=0; i<n; i++){
System.out.print("Enter element " +(i+1) + " ");
a[i] = sc.nextInt();
}

//sorting
for (int i=0; i<n-1; i++) {
for (int j=i+1; j<n; i++) {
if(a[i]>a[j]){
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}

//output
for (int i=0; i<n; i++){
System.out.println(a[i]);
}

}

Finding smallest and largest number in an array (java)

public static void main(String[] args) {

int[] numbers = new int[10];
int x = 0;
int max,min ;
while (x < numbers.length) {
int random = (int) (Math.random() * 10);
numbers[x] = random;
x = x + 1;
}
x = 0;
while (x <numbers.length) {
System.out.println("Value of numbers[" +x+ "] is " +numbers[x]);
x = x + 1;
}

max = numbers[0];
min = numbers[0];
for (x=0;x<numbers.length;x++){
if(numbers[x]>max){
max=numbers[x];
}
else if(numbers[x]<min){
min=numbers[x];
}

}
System.out.println("Largest number is " +max);
System.out.println("Smallest number is " +min);


}

Factorial in java

Factorial of number using iteration

public int factorial(int var) {
if ((var) > 1) {
fact = var * factorial(var - 1);
return fact;
}

else {
return 1;
}
}


 

Factorial of number using recursion


public int factorial(int var) {
if ((var) > 1) {
fact = var * factorial(var - 1);
return fact;
}

else {
return 1;
}

Multiplying 2 matrices in java

See following code:

//Input number rows and columns in 2 matrices
Scanner sc = new Scanner (System.in);
int r1,c1;
System.out.print("Enter how many rows");
r1 = sc.nextInt();
System.out.print("Enter how many cols");
c1 = sc.nextInt();
int r2,c2;
System.out.print("Enter how many rows");
r2 = sc.nextInt();
System.out.print("Enter how many cols");
c2 = sc.nextInt();
if(c1 != r2){
System.out.println("Can't mult");
System.exit(1);
}
int m1[][] = new int[r1][c1];

//input m1
for (int i=0; i<r1; i++){
for (int j=0; j<c1; j++){
System.out.print("Enter element" + (i+1)+","+(j+1));
m1[i][j] = sc.nextInt();
}
}

int m2[][] = new int[r2][c2];

//input m2
for (int i=0; i<r2; i++){
for (int j=0; j<c2; j++){
System.out.print("Enter element" + (i+1)+","+(j+1));
m2[i][j] = sc.nextInt();
}
}

int m3[][] = new int[r2][c2];

//Matrix multiplication
for (int i=0; i<r1; i++){
for (int j=0; j<c2; j++){
for (int k=0; k<c1; k++){
m3[i][j]+=m1[i][k]*m2[k][j];
}
}
}

//Output m3
for (int p=0; p<r1; p++){
for (int j=0; j<c2; j++){
System.out.print(m3[p][j]+"\t");
}
System.out.println();

}

Friday, 22 April 2011

Java source file format

A Java source file has the following elements in this specific order.
  • An optional package statement. All classes and interfaces defined in the file belong to this package. If the package statement is not specified, the classes defined in the file belong to a default package. An example of a package statement is -
    package testpackage;
  • Zero or more import statements. The import statement makes any classes defined in the specified package directly available. For example if a Java source file has a statement importing the class "java.class.Button", then a class in the file may use Button class directly without providing the names of the package which defines the Button class. Some examples of import statement are -
    import java.awt.*; // All classes in the awt package are imported.
    import java.applet.Applet;
  • Any number of class and interface definitions may follow the optional package and import statements.
If a file has all three of the above constructs, they must come in the specific order of package statement, one or more import statements, followed by any number of class or interface definitions. Also all the above three constructs are optional. So an empty file is a legal Java file.

Sunday, 17 April 2011

Making your Java Code Privileged?

The java system code that is part of the JDK is considered God and has all the maximum privileges. For example it can read a system property by default. To easily understand it is better to consider java Applets. An Applet cannot read a system property by default because it belongs to different CodeSource and not in same domain as system code. Recall that the system code has all privileges.

Then what do you need to do for Applet to get that privilege? You need to explicitly grant those security privileges by creating a policy file. In that policy you specify what are all the privileges you are granting.

There is another option also. It is opposite of the above. You say that this code doesn’t require any security policy and it is privileged to do the same (anything) as system code. Do you smell something evil here? This is a risky thing to do. Giving away the security is OS dependent. “Privileged code + malicious user + hole in OS” will be a worst thing to tackle.

Therefore you need to keep the code block as minimum as possible, for which you are going to give privilege. You might require this in the following scenarios :


  • To read a file
  • To read a system property
  • To create a network connection to the local machine
  • To get direct access to files that contain fonts

Making you code privileged -
anyMethod() {
...other java code here...
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
// put the privileged code here, example:
System.loadLibrary("awt");
return null; // in our scenario nothing to return
}
});
...other code continues...
}

AccessController API explains more about java privileged code and examples.