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

 }
}