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);

 }
}

       
 

Saturday, 27 July 2013

Source code for project developed in core Java " RAILWAY RESERVATION SYSTEM "

/*This is a java project source code for "RAILWAY RESERVATION MANAGEMENT SYSTEM" developed in core java technology */

 I thorougly tested the program myself and it works well ,after all if you need any  help then contact me my email is written above bbye ..
                                                                                      ------by gaurav kumar yadav


email:-gaurav10610@gmail.comif you like this code then you can subscribe me on facebook link:-

and subscribe my blog:-www.javaprogrammingstuff.blogspot.com


To download complete source code of the project click here.. DOWNLOAD


Thursday, 25 July 2013

Deterministic finite automata ( DFA ) implemented using Java....

 

This is a java program to simulate the DFA( deterministic finite automata ) developed using J2SE . It works for any general dfa that you have specified through five tuples during the execution of the program. 

To know that how to give input to the program please click here

here is the code  :-


       

import java.io.*;
import java.util.*;
public class Dfasimulator {

 public int[][] Transition = new int[20][20];
 public int alphaset, stateset;
 public ArrayList < Character > alphabet;
 public Set < Integer > finalstates;

 public void builtdfa() throws Exception {

  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  System.out.println("please enter the no. of alphabets in the alphabet set of DFA");
  alphaset = Integer.parseInt(br.readLine());
  //System.out.println(alphaset);
  System.out.println("please enter the no. of states in the set of DFA");
  stateset = Integer.parseInt(br.readLine());

  System.out.println("please enter all the alphabets of the DFA simultaneosly");
  String alphabets = br.readLine();

  alphabet = new ArrayList < Character > ();
  for (int i = 0; i < alphabets.length(); i++) {
   alphabet.add(alphabets.charAt(i));

  }
  //System.out.println(alphabet);

  for (int s = 0; s < stateset; s++) {
   System.out.println("enter the row entries of the transition table");
   for (int r = 0; r < alphaset; r++) {
    int int1 = Integer.parseInt(br.readLine());
    Transition[s][r] = int1;
   }
  }

  finalstates = new HashSet < Integer > ();
  System.out.println("please enter the final states of the DFA\n");
  System.out.println("when you are done of giving the inputs enter -2 to stop feeding final states");

  int int2 = Integer.parseInt(br.readLine());

  while (int2 != -2) {
   finalstates.add(int2);
   int2 = Integer.parseInt(br.readLine());
  }
  System.out.println(finalstates);
  DfaTest();
 }

 public void DfaTest() throws Exception {
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  int state;
  System.out.println("enter the starting state");
  state = Integer.parseInt(br.readLine());

  System.out.println("give input to the DFA\n");
  System.out.println("end your input with # character\n");
  String input = br.readLine();
  int index = 0;

  while (input.charAt(index) != '#') {
   char char1 = input.charAt(index);
   int index1 = alphabet.indexOf(char1);
   state = Transition[state][index1];
   index++;
  }
  System.out.println("\nfinal state after giving the input is " + state);
  if (finalstates.contains(state))
   System.out.print("your input string is accepted by the DFA");
  else
   System.out.println("the input string is rejected by  the DFA");
 }

 public static void main(String args[]) throws Exception {
  Dfasimulator raw = new Dfasimulator();
  raw.builtdfa();
  //System.out.println(raw.alphaset);
 }
}