Friday, 11 October 2013

Reversing a string using stack in java....

Reversing a string using stack in java....


       

here is the code..
import java.util.*;
import java.io.*;
public class Reverse {
 public static void main(String args[]) throws Exception {
  String name = "gaurav";
  int k = name.length();
  Stack ab = new Stack();

  char[] name1 = new char[k];

  for (int i = 0; i < k; i++)
   ab.push(new Character(name.charAt(i)));

  for (int j = 0; j < k; j++)
   name1[j] = ((Character) ab.pop()).charValue();

  for (char s: name1)
   System.out.print(s);

 }
}

       
 

Saturday, 5 October 2013

Quicksort implemented using Java......

/*Java program for sorting an array using quicksort ...*/

here is the code ...


       

import java.io.*;
public class Quick {

 public static void Quicksort(int[] arr, int lower, int upper) {
  int i = 0;
  if (lower < upper) {
   i = split(arr, lower, upper);
   Quicksort(arr, lower, i - 1);
   Quicksort(arr, i + 1, upper);
  }
 }

 public static int split(int[] arr, int lower, int upper) {
  int t = 0;
  int p = lower + 1;
  int q = upper;
  try {
   while (p <= q) {

    while (arr[p] < arr[lower])
     p++;

    while (arr[q] > arr[lower])
     q--;
    if (q > p) {
     t = arr[q];
     arr[q] = arr[p];
     arr[p] = t;
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  t = arr[q];
  arr[q] = arr[lower];
  arr[lower] = t;
  return q;

 }


 public static void main(String args[]) {
  int[] arr = {
   6,
   7,
   9,
   3,
   5,
   10,
   34,
   13
  };
  int lower = 0, upper = arr.length - 1;
  Quicksort(arr, lower, upper);

  for (int k: arr)
   System.out.println(k);

 }
}

       
 

Saturday, 7 September 2013

java program to check whether a string is made up of unique characters or not ..

/*java program to check whether a string is made up of unique characters or not ..*/


       

import java.io.*;
public class P11 {
 public static void main(String args[]) {
  boolean[] ch = new boolean[256];

  String name = "gaurv##";

  for (int i = 0, val; i < name.length(); i++) {
   val = name.charAt(i);
   if (ch[val]) {
    System.out.println("string is not unique");
    return;
   } else {
    ch[val] = true;
   }
  }
  System.out.println("string is unique");
 }
}

       
 

Check whether two strings are anagrams or not using java....

/*java program to check whether two strings are anagrams */


       

import java.io.*;
public class P14 {
 public static void main(String args[]) {
  char chr[] = {
   'g',
   'a',
   'u',
   'r',
   'a',
   'v',
   's'
  };
  char chr1[] = {
   'v',
   'a',
   'g',
   'a',
   'u',
   'r',
   'k'
  };
  int[] boo = new int[256];
  if (chr.length != chr1.length) {
   System.out.println("strings are not anagrams");
   return;
  }

  for (int i = 0, val; i < chr.length; i++) {
   val = chr[i];
   boo[val] = boo[val] + 1;
  }


  try {
   for (int k = 0, val1; k < chr1.length; k++) {
    val1 = chr1[k];
    if (boo[val1] != 0) {
     boo[val1] = boo[val1] - 1;
    } else {
     System.out.println("strings are not anagrams");
     return;
    }
   }
  } catch (Exception e) {
   System.out.println("error found");
  }
  System.out.println("strings are anagrams ");
 }
}

       
 

java program to replace the spaces in a string by '%20'.

Here is the code ....


       

public class P15 {
 public static void main(String args[]) {
  String name = "gaurav kumar yadav";
  int val = 0;
  for (int i = 0; i < name.length(); i++)
   if (name.charAt(i) == ' ') val = val + 1;

  char[] chr = new char[name.length() + 2 * val];
  for (int l = 0, s = 0; l < name.length(); l++) {
   if (name.charAt(l) != ' ') {
    chr[s] = name.charAt(l);
    s++;
   } else {
    chr[s] = '%';
    chr[++s] = '2';
    chr[++s] = '0';
    s++;
   }

  }
  String name1 = new String(chr);
  System.out.println(name1);
 }
}

       
 

Wednesday, 28 August 2013

Java program to implement comparable interface ......

Here is the code ;-....



       

import java.util.*;
class Student implements Comparable {
 String firstname, lastname;
 int StudentId;
 double GPA;
 public Student() {
  System.out.println("this is the unparamaterized constructor");
 }

 public Student(String firstname, String lastname, int StudentId, double GPA) {
  if (firstname == null || lastname == null || StudentId == 0 || GPA == 0.0) {
   throw new IllegalArgumentException();
  }
  this.firstname = firstname;
  this.lastname = lastname;
  this.StudentId = StudentId;
  this.GPA = GPA;
 }

 public String firstname() {
  return firstname;
 }

 public String lastname() {
  return lastname;
 }

 public int StudentId() {
  return StudentId;
 }

 public double GPA() {
  return GPA;
 }

 public int compareTo(Object o) {
  double f = GPA - ((Student) o).GPA;

  if (f == 0.0) {
   return 0;
  } else if (f < 0.0)
   return -1;
  else
   return 1;
 }
}

public class ComparableTest extends Student {
 public static void main(String a[]) {
  TreeSet free = new TreeSet();
  free.add(new Student("gaurav", "yadav", 26, 4.0));
  free.add(new Student("paras", "yadav", 34, 3.0));
  free.add(new Student("suman", "yadav", 65, 9.0));
  free.add(new Student("ram", "saxena", 67, 7.8));
  free.add(new Student("nitasha", "kuntal", 74, 7.9));
  Object[] free2 = free.toArray();
  Student s;
  for (Object obj: free2) {
   s = (Student) obj;
   System.out.printf("name=%s %s id=%d GPA=%.1f\n", s.firstname(), s.lastname(), s.StudentId(), s.GPA());
  }
 }
}

       
 

Determinant and Transpose of a square matrix using Java...

NOTE- Here matrix is input through a file.

here is the code .....


       

import java.io.*;
import java.util.*;
import static java.lang.Math.pow;

public class Read {


 static Scanner input = new Scanner(System.in);

 static int count = 0;
 public static void main(String[] args) throws IOException {
  String fileName = "test.txt";
  File file = new File("test.txt");
  BufferedReader be = new BufferedReader(new FileReader(file));
  String raw;
  while ((raw = be.readLine()) != null)
   count++;

  int[][] transpose = new int[count][count];
  int[][] matrix = new int[count][count];





  String line = "";

  FileInputStream inputStream = new FileInputStream(fileName);
  Scanner scanner = new Scanner(inputStream);
  DataInputStream in = new DataInputStream(inputStream);
  BufferedReader bf = new BufferedReader(new InputStreamReader( in ));

  int lineCount = 0;
  String[] numbers;
  while ((line = bf.readLine()) != null) {
   numbers = line.split(" ");
   for (int i = 0; i < 3; i++) {
    matrix[lineCount][i] = Integer.parseInt(numbers[i]);
   }

   lineCount++;
  }
  bf.close();
  System.out.println("The matrix is:");

  for (int i = 0; i < 3; i++) {
   for (int j = 0; j < 3; j++) {
    System.out.print(matrix[i][j] + " ");
   }
   System.out.println();
  }
  System.out.println();


  for (int k = 0; k < count; k++) {
   for (int k1 = 0; k1 < count; k1++) {
    transpose[k][k1] = matrix[k1][k];

   }
  }

  int i, j = 1, k = count - 1;
  double determinant = 0.0, p;
  for (i = 0; i < count; i++) {
   p = Math.pow(-1.0, i);
   if (i == count - 1)
    k = 1;
   determinant = determinant + (matrix[0][i] * (matrix[1][j] * matrix[2][k] - matrix[2][j] * matrix[1][k])) * p;
   j = 0;

  }
  System.out.println(determinant);

 }
}