Thursday, 24 September 2015

S9P2

Write a program which generates random integers and stores them in a file named “rand.dat”. The program then reads the integers from the file and displays on the screen.

Code:
 import java.io.*;  
 import java.util.*;  
 class S9P2_Random  
 {  
      public static void main(String args[])  
      {  
           FileWriter FW = null;  
           BufferedWriter bfwr = null;  
           File inFile = new File("rand.dat");  
           try  
           {  
                int rand_int;  
                FW = new FileWriter(inFile);  
                bfwr = new BufferedWriter(FW);  
                Random generate_rand = new Random();  
                System.out.println("\nGenerating & Storing Random integers from 0 to 99..\n");  
                for(int i=0; i<10; i++)  
                {  
                     rand_int = generate_rand.nextInt(100);  
                     System.out.print(rand_int+"\t");  
                     bfwr.write(rand_int+"\r\n");  
                }  
           }  
           catch(IOException e)  
           {  
                System.out.println(e.getMessage());  
           }  
           finally  
           {  
                try  
                {  
                     bfwr.close();  
                }  
                catch(IOException e){ }  
           }  
           try  
           {  
                System.out.println("\nRetrieving Random integers stored in file 'rand.dat'..");  
                Scanner scanner = new Scanner(inFile);  
                while(scanner.hasNextInt())  
                {  
                     System.out.println(scanner.nextInt()+"\t");  
                }  
           }  
           catch(NullPointerException e)  
           {  
                System.out.println(e.getMessage());  
           }  
           catch(FileNotFoundException e)  
           {  
                System.out.println(e.getMessage());  
           }            
      }  
 }  

Output:

S9P1

Write a program that takes two files names (source and destination) as command line argument .Copy source file’s content to destination file. Use character stream class. Also do same using byte stream and buffer stream.

Using Character Stream:

Code:

 import java.io.*;  
 class S9P3_io_copy_chars  
 {  
      public static void main(String args[])  
      {  
           String f_name1 = args[0];  
           String f_name2 = args[1];  
           FileReader Infile = null;  
           FileWriter Outfile = null;  
           int ReadC;  
           try  
           {  
                Infile = new FileReader(f_name1);  
                Outfile = new FileWriter(f_name2);  
                while((ReadC = Infile.read()) != -1)  
                {  
                     System.out.print((char)ReadC);  
                     Outfile.write(ReadC);  
                }  
           }  
           catch(FileNotFoundException e)  
           {  
                System.out.println("File Not Found!");  
           }  
           catch(IOException e)  
           {  
                System.out.println(e.getMessage());  
           }  
           finally  
           {  
                try  
                {  
                     Infile.close();  
                     Outfile.close();  
                }  
                catch(IOException e)  
                {  
                     System.out.println(e.getMessage());  
                }        
           }  
      }  
 }  

Output:




Using Byte Stream:

Code:

 import java.io.*;  
 class S9P3_io_copy_bytes  
 {  
      public static void main(String args[])  
      {  
           String f_name1 = args[0];  
           String f_name2 = args[1];  
           FileInputStream Infile = null;  
           FileOutputStream Outfile = null;  
           byte ReadB;  
           try  
           {  
                Infile = new FileInputStream(f_name1);  
                Outfile = new FileOutputStream(f_name2);  
                do  
                {  
                     ReadB = (byte)Infile.read();  
                     System.out.print((char)ReadB);  
                     Outfile.write(ReadB);  
                }  
                while(ReadB != -1);  
           }  
           catch(FileNotFoundException e)  
           {  
                System.out.println("File Not Found!");  
           }  
           catch(IOException e)  
           {  
                System.out.println(e.getMessage());  
           }  
           finally  
           {  
                try  
                {  
                     Infile.close();  
                     Outfile.close();  
                }  
                catch(IOException e)  
                {  
                     System.out.println(e.getMessage());  
                }        
           }  
      }  
 }  

Output:




Using Buffer Stream:

Code:

 import java.io.*;  
 class S9P3_io_copy_buffer  
 {  
      public static void main(String args[])  
      {  
           String f_name1 = args[0];  
           String f_name2 = args[1];  
           BufferedReader Infile = null;  
           BufferedWriter Outfile = null;  
           int ReadBuff;  
           try  
           {  
                Infile = new BufferedReader(new FileReader(f_name1));  
                Outfile = new BufferedWriter(new FileWriter(f_name2));  
                while((ReadBuff = Infile.read()) != -1)  
                {  
                     System.out.print((char)ReadBuff);  
                     Outfile.write(ReadBuff);  
                }  
           }  
           catch(FileNotFoundException e)  
           {  
                System.out.println("File Not Found!");  
           }  
           catch(IOException e)  
           {  
                System.out.println(e.getMessage());  
           }  
           finally  
           {  
                try  
                {  
                     Infile.close();  
                     Outfile.close();  
                }  
                catch(IOException e)  
                {  
                     System.out.println(e.getMessage());  
                }        
           }  
      }  
 }  

Output:

Saturday, 5 September 2015

S8P2

Write the thread program using Runnable interface.

Code:
 class MyThread implements Runnable  
 {  
      public void run()  
      {  
           for(int i=0; i<10; i++)  
           {  
                System.out.println("NewThread: "+(i+1));  
           }  
           System.out.println("End of NewThread");  
      }  
 }  
 class S8P2_Runnable  
 {  
      public static void main(String args[])  
      {  
           MyThread runn = new MyThread();  
           Thread NewThread = new Thread(runn);  
           NewThread.start();  
           System.out.println("\nEnd of main Thread");  
      }  
 }  


Output:


S8P1

The program to creates and run the following three threads. The first thread prints the letter ‘a’ 100 times. The second thread prints the letter ‘b’ 100 times. The third thread prints the integer 1 to 100.

Code:

 class A extends Thread  
 {  
      public void run()  
      {  
           for(int i=0; i<100; i++)  
           {  
                System.out.print("a\t");  
           }  
      }  
 }  
 class B extends Thread  
 {  
      public void run()  
      {  
           for(int i=0; i<100; i++)  
           {  
                System.out.print("b\t");  
           }  
      }  
 }  
 class C extends Thread  
 {  
      public void run()  
      {  
           for(int i=0; i<100; i++)  
           {  
                System.out.print((i+1)+"\t");  
           }  
      }  
 }  
 class S8P1_Thread   
 {  
      public static void main(String args[])  
      {  
           new A().start();       
           new B().start();  
           new C().start();  
      }  
 }  


Output:

Friday, 4 September 2015

S6P3

Create a package “employee” and define a Class Employee having three data members, name, emp_num, and gender and two methods- input_data and show_data. Inherit class SalariedEmployee from this class and keep it in package “employee”. Add new variable salary and methods allowance (if female hra=0.1* salary else 0.09* salary. DA= 0.05*salary) and increment (salary= salary+0.01 * salary). Calculate gross salary in main class defined in the same package.


Code:

Package 'employee':
 package employee;  
 class Employee  
 {  
      public int emp_numb;  
      public String name;  
      public String gender;  
      public Employee(int e, String n, String g)  
      {  
           input_data(e,n,g);  
      }  
      public void input_data(int e1, String n1, String g1)  
      {  
           emp_numb = e1;  
           name = n1.toUpperCase();  
           gender = g1.toUpperCase();  
           show_data();  
      }  
      public void show_data()  
      {  
           System.out.println("---------------------------------------------------");  
           System.out.println("Emp_num: "+emp_numb+"\tName: "+name+"\tGender: "+gender);  
      }  
 }  
 public class SalariedEmployee extends Employee  
 {  
      public double salary,HRA,DA;  
      public SalariedEmployee(int e3, String n3, String g3, double s)  
      {  
           super(e3,n3,g3);  
           salary = s;  
      }  
      public double allowance()  
      {  
           DA = (0.05)*salary;  
           if(gender.equals("FEMALE"))  
           {  
                HRA = (0.1)*salary;  
           }  
           else  
           {  
                HRA = (0.09)*salary;  
           }  
           return (HRA+DA);  
      }  
      public double increment()  
      {  
           salary = salary + (0.1*salary);  
           return salary;  
      }  
 }  


Class S6P3_Employee_Package:
 import employee.SalariedEmployee;  
 class S6P3_Employee_Package  
 {  
      public static void main(String args[])  
      {  
           SalariedEmployee emp1 = new SalariedEmployee(1,"ABCD","male",30000);  
           System.out.println("\nGross Salary for "+emp1.name+ ": "+(emp1.salary+emp1.allowance()+emp1.increment()));       
           SalariedEmployee emp2 = new SalariedEmployee(2,"EFGH","female",30000);  
           System.out.println("\nGross Salary for "+emp1.name+ ": "+(emp1.salary+emp1.allowance()+emp1.increment()));       
      }  
 }  


Output:

Wednesday, 2 September 2015

S7P2

Define an Exception called “NoMatchException” that is thrown when a string is not equal to “India”. Write a program that uses this exception.





Code:
 class NoMatchException extends Exception  
 {  
      String m;  
      NoMatchException(String message)  
      {  
           m = message;  
      }  
      void printmsg()  
      {  
           System.out.println(m);  
      }  
 }  
 class S7P2_NoMatch  
 {  
      public static void main(String args[])  
      {  
           if(args.length!=1)  
           {  
                System.out.println("\nEnter only one String argument");  
           }  
           else  
           {  
                try  
                {  
                     if(args[0].compareTo("India") == 0)  
                     {  
                          System.out.println("\nString is equal to 'India'.");  
                     }  
                     else  
                     {  
                          throw new NoMatchException("Arguments is not equal to 'India'.");  
                     }  
                }  
                catch(NoMatchException e)  
                {  
                     System.out.println("\nException Caught: ");  
                     e.printmsg();  
                }  
           }  
      }  
 }  

Output: