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:

Monday, 31 August 2015

S7P1

WAP using try catch block. User should enter two command line arguments. If only one argument is entered then exception should be caught. In case of two command line arguments, if fist is divided by second and if second command line argument is 0 then catch the appropriate exception.


Code:
 class NewException extends Exception  
 {  
      String m;  
      NewException(String message)  
      {  
           m = message;  
      }  
      void printmsg()  
      {  
           System.out.println(m);  
      }  
 }  
 class S7P1_Exception  
 {  
      public static void main(String args[])  
      {  
           int num[] = new int[args.length];  
           try  
           {  
                if(args.length<2)  
                {  
                     throw new NewException("Require atleast 2 command line arguments.");  
                }  
                for(int i=0; i<args.length; i++)  
                {  
                     num[i] = Integer.parseInt(args[i]);  
                }  
                int a = (int)num[0];            
                int b = (int)num[1];            
                double div = a/b;  
                System.out.println("\nDivision successful. No exceptions found.\n"+a+"/"+b+" = "+div);  
           }  
           catch(NewException e)  
           {  
                System.out.println("\nException Caught: ");  
                e.printmsg();  
           }  
           catch(NumberFormatException e2)  
           {  
                System.out.println("\nException Caught: ");  
                System.out.println("Require Integer arguments.");  
           }  
           catch(ArithmeticException e3)  
           {  
                System.out.println("\nException Caught: ");  
                System.out.println("Division by Zero.");  
           }  
      }  
 }  

Output:




Thursday, 27 August 2015

S6P2

Create an interface Shape2D which declares a getArea() method. Point 3D contains coordinates of a point. The abstract class Shape declares abstract display() method and is extended by Circle class. it implements the Shape2D interface. The Shapes class instantiates this class and exercises its methods.


Code:

 interface Shape2D  
 {  
      void getArea();  
 }  
 abstract class Shape  
 {  
      abstract void display();  
 }  
 class Point3D  
 {  
      double x,y,z;  
      Point3D(double x,double y,double z)  
      {  
           this.x = x;  
           this.y = y;  
           this.z = z;  
      }  
 }  
 class Circle extends Shape implements Shape2D  
 {  
      Point3D CenterPoint, OtherPoint;  
      double area;  
      Circle(Point3D P1, Point3D P2)  
      {  
           CenterPoint = P1;  
           OtherPoint = P2;  
      }  
      public void getArea()  
      {       
           double d1 = CenterPoint.x - OtherPoint.x;  
           double d2 = CenterPoint.y - OtherPoint.y;  
           double d = (d1*d1) + (d2*d2);  
           double radius = Math.sqrt(d);  
           area = Math.PI*radius*radius;  
      }  
      public void display()  
      {  
           System.out.println("\nGiven points-> Center Point: ("+CenterPoint.x+", "+CenterPoint.y+", "+CenterPoint.z+") , Other Point: ("+OtherPoint.x+", "+OtherPoint.y+", "+OtherPoint.z+")\nArea for a circle having given points: "+area+" __\n");  
      }  
 }  
 class S6P2_Shapes_Class  
 {  
      public static void main(String args[])  
      {  
           Point3D point1 = new Point3D(7,4,6);  
           Point3D point2 = new Point3D(5,2,8);  
           Circle obj = new Circle(point1 , point2);  
           obj.getArea();  
           obj.display();  
      }  
 }  


Output:

Saturday, 8 August 2015

S6P1

Write an abstract class shape, which defines abstract method area. Derive class circle from shape. It has data member radius and implementation for area function. Derive class Triangle from shape. It has data members height, base and implementation for area function. Derive class Square from shape. It has data member side and implementation for area function. In main class, use dynamic method dispatch in order to call correct version of method.


Code:
 abstract class Shape  
 {  
      abstract void area();  
 }  
 class Circle extends Shape  
 {  
      double radius;  
      void area()  
      {  
           System.out.println("\nArea of Circle: "+(3.14*radius*radius));  
      }  
 }  
 class Triangle extends Shape  
 {  
      double height,base;  
      void area()  
      {  
           System.out.println("Area of Triangle: "+((height*base)/2));  
      }  
 }  
 class Square extends Shape  
 {  
      double side;  
      void area()  
      {  
           System.out.println("Area of Square: "+(side*2));  
      }  
 }  
 class S6P1_Dynemic_Dispatch  
 {  
      public static void main(String args[])  
      {  
           Circle C1 = new Circle();  
           C1.radius = 4;  
           Triangle T1 = new Triangle();  
           T1.height = 5; T1.base = 11.4;  
           Square S1 = new Square();  
           S1.side = 23.5;  
           Shape S;  
           S = C1;  
           S.area();  
           S = T1;  
           S.area();  
           S = S1;  
           S.area();            
      }  
 }  


Output:

S5P2

WAP that illustrates method overriding. Class A3 is extended by Class B3. Each of these classes defines a hello(string s) method that outputs the string “A3: Hello From -” or “B3: Hello From -” respectively. Use the concept Dynamic Method Dispatch and keyword super.


Code:
 class A3  
 {  
      String S;  
      A3(String s)  
      {  
           hello(s);  
      }  
      public void hello(String s)  
      {  
           S = s;  
           System.out.println("A3 : Hello From "+S);  
      }  
 }  
 class B3 extends A3  
 {  
      B3(String s)  
      {  
           super(s);  
      }  
      public void hello(String s)  
      {  
           S = s;  
           System.out.println("B3 : Hello From "+S);  
      }  
 }  
 class S5P2_Override  
 {  
      public static void main(String args[])  
      {  
           A3 a = new A3("DD");  
           A3 b = new B3("DD");  
      }  
 }  


Output:


S5P1

Write a program which defines base class Employee having three data members, namely name[30], emp_numb and gender and two methods namely input_data() and show_data(). Derive a class SalariedEmployee from Employee and adds a new data member, namely salary. It also adds two member methods, namely allowance (if gender is female HRA=0.1 *salary else 0.09* salary. DA= 0.05*salary) and increment (salary = salary+0.1*salary). Display the gross salary in main class. (Tip: Use super to call base class’s constructor).


Code:
 class Employee  
 {  
      int emp_numb;  
      String name;  
      String gender;  
      Employee(int e, String n, String g)  
      {  
           input_data(e,n,g);  
      }  
      void input_data(int e1, String n1, String g1)  
      {  
           emp_numb = e1;  
           name = n1.toUpperCase();  
           gender = g1.toUpperCase();  
           show_data();  
      }  
      void show_data()  
      {  
           System.out.println("---------------------------------------------------");  
           System.out.println("Emp_num: "+emp_numb+"\tName: "+name+"\tGender: "+gender);  
      }  
 }  
 class SalariedEmployee extends Employee  
 {  
      double salary,HRA,DA;  
      SalariedEmployee(int e3, String n3, String g3, double s)  
      {  
           super(e3,n3,g3);  
           salary = s;  
      }  
      double allowance()  
      {  
           DA = (0.05)*salary;  
           if(gender.equals("FEMALE"))  
           {  
                HRA = (0.1)*salary;  
           }  
           else  
           {  
                HRA = (0.09)*salary;  
           }  
           return (HRA+DA);  
      }  
      double increment()  
      {  
           salary = salary + (0.1*salary);  
           return salary;  
      }  
 }  
 class S5P1_Employee  
 {  
      public static void main(String args[])  
      {  
           SalariedEmployee emp1 = new SalariedEmployee(1,"Dhruv","male",30000);  
           System.out.println("\nGross Salary for "+emp1.name+ ": "+(emp1.salary+emp1.allowance()+emp1.increment()));            
      }  
 }  

Output:

S4P1

Write a program that illustrates interface inheritance. Interface P is extended by P1 And P2. Interface P12 inherits from both P1 and P2.Each interface declares one constant and one method. Class Q implements P12.Instantiate Q and invokes each of its methods. Each method displays one of the constants.

Code:

 interface p   
 {  
      int p=0;  
      void display_p();  
 }  
 interface p1 extends p  
 {  
      int p1=1;  
      void display_p1();  
 }  
 interface p2 extends p  
 {  
      int p2=2;  
      void display_p2();  
 }  
 interface p12 extends p1,p2  
 {  
      int p12=12;  
      void display_p12();  
 }  
 class Q implements p12  
 {  
      int q=3;  
      public void display_p()  
      {  
           System.out.println("\nInterface P, Constant in P: "+p);  
      }  
      public void display_p1()  
      {  
           System.out.println("Interface P1 Extends P, Constant in P1: "+p1);  
      }  
      public void display_p2()  
      {  
           System.out.println("Interface P2 Extends P, Constant in P2: "+p2);  
      }  
      public void display_p12()  
      {  
           System.out.println("Interface P12 Extends P1 & P2, Constant in P12: "+p12);  
      }  
      void display_q()  
      {  
           System.out.println("Class Q Implements P12, Constant in Q: "+q);  
      }  
 }  
 class S4P1_Interface  
 {  
      public static void main(String args[])  
      {  
           Q object = new Q();  
           object.display_p();  
           object.display_p1();  
           object.display_p2();  
           object.display_p12();  
           object.display_q();            
      }  
 }  

Output:

Tuesday, 4 August 2015

S3P3

It is required to compute SPI (semester performance index) of n students of your college for their registered subjects in a semester. Declare a class called student having following data members: id_no , no_of_subjects_registered, subject_code , subject_credits, grade_obtained and spi.
- Define constructor and calculate_spi methods.
- Define main to instantiate an array for objects of class student to process data of n students to be given as command line arguments.


 class student  
 {  
      int id_no, no_of_subjects_registered, subject_credits;   
      float spi;  
      String subject_code,grade_obtained;  
      student(int i, int nsub, int sc, String code, String g)  
      {  
           id_no = i;  
           no_of_subjects_registered = nsub;  
           subject_credits = sc;  
           subject_code = code;  
           grade_obtained = g;  
      }  
      void calculate_spi()  
      {  
           int go=0;  
           switch(grade_obtained)  
           {  
                case "AA": go = 10; break;  
                case "AB": go = 9; break;  
                case "BB": go = 8; break;  
                case "BC": go = 7; break;  
                case "CC": go = 6; break;  
                case "CD": go = 5; break;                 
                case "DD": go = 4; break;  
                case "FF": go = 00; break;  
                default : System.out.println("\nInvalid Grade Value.."+grade_obtained); break;  
           }  
           int total = no_of_subjects_registered*subject_credits;  
           spi = (total*go)/total;  
           System.out.println("\nID Number: "+id_no+", SPI: "+spi);  
      }  
 }  
 class S3P3_spi  
 {  
      public static void main(String args[])  
      {  
           int n_student = 3;  
           student s_obj[] = new student[n_student];  
           s_obj[0] = new student(1, 4, 10, "27374", "AA");  
           s_obj[1] = new student(2, 4, 10, "27374", "CC");  
           s_obj[2] = new student(3, 4, 10, "27374", "BC");  
           for(int i=0; i<n_student; i++)  
           {  
                s_obj[i].calculate_spi();  
           }  
      }  
 }  

Output:

   
 E:\Java>javac S3P3_spi.java  
   
 E:\Java>java S3P3_spi  
   
 ID Number: 1, SPI: 10.0  
   
 ID Number: 2, SPI: 6.0  
   
 ID Number: 3, SPI: 7.0  
   
 E:\Java>  
   

Monday, 3 August 2015

S3P2

Describe abstract class called Shape which has three subclasses say Triangle, Rectangle, Circle. Define one method area() in the abstract class and override this area() in these three subclasses to calculate for specific object i.e. area() of Triangle subclass should calculate area of triangle etc. Same for Rectangle and Circle.


 abstract class Shape  
 {  
      int l,b,r;  
      float pi = 3.14f;  
      Shape()  
      {  
           l = 5;  
           b = 10;  
           r = 12;  
      }  
      void area()  
      {  
           System.out.println("\nThis method will be overriden..");  
      }  
 }  
 class triangle extends Shape  
 {  
      void area()  
      {  
           System.out.println("\nArea of Trtingle: "+(l*b)/2);  
      }  
 }  
 class rectangle extends Shape  
 {  
      void area()  
      {  
           System.out.println("\nArea of Rectangle: "+(l*b));  
      }  
 }  
 class circle extends Shape  
 {  
      void area()  
      {  
           System.out.println("\nArea of Circle: "+(pi*r*r));  
      }  
 }  
 class S3P2_abstract_class  
 {  
      public static void main(String args[])  
      {  
           triangle obj1 = new triangle();  
           rectangle obj2 = new rectangle();  
           circle obj3 = new circle();  
           System.out.println("\nl="+obj1.l+", b="+obj1.b+", r="+obj1.r+", pi=3.14");  
           Shape obj;  
           obj = obj1;  
           obj.area();  
           obj = obj2;  
           obj.area();  
           obj = obj3;  
           obj.area();  
      }  
 }  

Output:

   
 E:\Java>javac S3P2_abstract_class.java  
   
 E:\Java>java S3P2_abstract_class  
   
 l=5, b=10, r=12, pi=3.14  
   
 Area of Trtingle: 25  
   
 Area of Rectangle: 50  
   
 Area of Circle: 452.16  
   
 E:\Java>  
   

S3P1

Write a Java Program to find Factorial of a Given Number using Command Line Argument.


 class S3P1_cmd_factorial  
 {  
      public static void main(String args[])  
      {  
           int n = Integer.parseInt(args[0]), fact=1;  
           for(int i=n; i>1; i--)  
           {  
                fact = fact*i;  
           }  
           System.out.println("\nFactorial of "+n+" is "+fact);  
      }  
 }  

Output:

   
 E:\Java>javac S3P1_cmd_factorial.java  
   
 E:\Java>java S3P1_cmd_factorial 5  
   
 Factorial of 5 is 120  
   
 E:\Java>  
   

Saturday, 1 August 2015

S2P3

Write a Java Program to sort City Names in Ascending Order using Command Line Argument.


 class S2P3_Ascend_City  
 {  
      public static void main(String cities[])  
      {  
           for(int i=0; i<cities.length; i++)  
           {       
                cities[i] = cities[i].toUpperCase();  
                for(int j=i+1; j<cities.length; j++)  
                {  
                     cities[j] = cities[j].toUpperCase();  
                     if(cities[i].compareTo(cities[j]) > 0)  
                     {  
                          String temp= new String();  
                          temp = cities[i];  
                          cities[i] = cities[j];  
                          cities[j] = temp;  
                     }  
                }  
                System.out.println(cities[i]);  
           }            
      }  
 }  

Output:

 E:\Java>javac S2P3_Ascend_City.java  
 E:\Java>java S2P3_Ascend_City Delhi Chennai Ahmedabad Banglore Mumbai Hyderabad  
 Shrinagar Jaipur Nagpur Agra Shillong  
 AGRA  
 AHMEDABAD  
 BANGLORE  
 CHENNAI  
 DELHI  
 HYDERABAD  
 JAIPUR  
 MUMBAI  
 NAGPUR  
 SHILLONG  
 SHRINAGAR  
 E:\Java>  

S2P2

Write a program to find the average of n numbers stored in an Array.


 class S2P2_avg  
 {  
      public static void main(String args[])  
      {  
           int a[] = {12,32,4,56,97,78,89,78,91,56,114};  
           float total = 0;  
           for(int i=0; i<a.length; i++)  
           {  
                total = total + a[i];  
           }  
           System.out.println("Average of given values is "+(total/a.length));                 
      }  
 }  

Output:

   
 E:\Java>javac S2P2_avg.java  
   
 E:\Java>java S2P2_avg  
 Average of given values is 64.27273  
   
 E:\Java>  
   

S2P1

Write a Java program that displays all the Java Features using Command line Argument.


 class S2P1_cmd_line_args  
 {  
      public static void main(String args[])  
      {  
           System.out.println("\nJava Features: \n");  
           for(int i=0; i<args.length; i++)  
           {  
                System.out.println((i+1)+") Java is "+args[i]+".");  
           }       
      }  
 }  

Output:

   
 E:\Java>javac S2P1_cmd_line_args.java  
   
 E:\Java>java S2P1_cmd_line_args Compiled_and_Interpreted Platform_Independant Ob  
 ject_Oriented Distributed High_performance_language Dynemic_and_extensible  
   
 Java Features:  
   
 1) Java is Compiled_and_Interpreted.  
 2) Java is Platform_Independant.  
 3) Java is Object_Oriented.  
 4) Java is Distributed.  
 5) Java is High_performance_language.  
 6) Java is Dynemic_and_extensible.  
   
 E:\Java>  
   
   

S1P3

Write a program to print the Fibonacci series.


 class S1P3_fibonaci  
 {  
      public static void main(String args[])  
      {  
           int a=1,b=1,c=a+b;  
           System.out.println("\nFirst 10 Numbers of Fibonaci Series\n---------------------------------");  
           for(int i=0; i<10; i++)  
           {                 
                if(i<=1)  
                {  
                     System.out.print(a+" ");  
                }  
                else  
                {  
                     System.out.print(c+" ");  
                     a = b; b= c; c = a+b;  
                }       
           }  
           System.out.println("\n");  
      }  
 }  

Output:

   
 E:\Java>javac S1P3_fibonaci.java  
   
 E:\Java>java S1P3_fibonaci  
   
 First 10 Numbers of Fibonaci Series  
 ---------------------------------  
 1 1 2 3 5 8 13 21 34 55  
   
   
 E:\Java>  
   
   

S1P2

Write a program to find whether the number is prime or not.


 class S1P2_Prime  
 {  
      public static void main(String args[])  
      {  
           int num = 17,flag=1;  
           for(int i=2;i<=num/2;i++)  
           {  
                if(num%i==0)  
                {  
                     flag=0;  
                     break;  
                }  
           }  
           if(flag == 1)  
           {  
                System.out.println(num + " is Prime Number");  
           }  
           else  
           {  
                System.out.println(num + " is not Prime Number");  
           }            
      }  
 }  

Output:

   
 E:\Java>javac S1P2_Prime.java  
   
 E:\Java>java S1P2_Prime  
 17 is Prime Number  
   
 E:\Java>  
   

S1P1

Write a program to display "Welcome To Java World".


 class hello  
 {  
      public static void main(String args[])  
      {  
           System.out.println("Welcome To Java World.");  
      }  
 }  

Output:

   
 E:\Java>javac hello.java  
   
 E:\Java>java hello  
 Welcome To Java World.  
   
 E:\Java>  
   

Tuesday, 7 July 2015

Getting Ready!

How to set up your computer to begin programming with Java?


Step 1 : Download & Install JDK (Java Development Kit)

Step 2 : Setting System Variables

1. Go to Control Panel of your computer, select System Properties from the menu and select Advanced Settings.





2. Click on Environment Variables and you this window will appear.


3. Under the System Variables section find PATH variable. If you find the PATH variable in the list then go to Step 4 otherwise skip to Step 5.

4Click on Edit button below the list and you will be shown Edit System Variable window. There will be some paths already added in Variable Value field. To add new path for jdk that you have installed already, you have to put a ';' (semicolon) and then path of your jdk's bin directory in Variable Value field.
(Usually it is C:\Program Files\Java\jdk1.8.0_05\bin where, jdk version may vary)


5. If the PATH variable is not listed under the System Variables, you can create new variable by clicking on New button below the list. Set "Path" as Variable Name and set the path of your bin folder of jdk in Variable Value field.
(Usually it is C:\Program Files\Java\jdk1.8.0_05\bin where, jdk version may vary)



Step 3 : Creating and Executing a Java file

1. You can create a java program using various software or using Notepad. Create a sample program for java as shown below. Here created file using Notepad and Saved it as "Test.java".
(NOTE: Name of your File must be same as the Class name which contains the Main method)

2. Now open command prompt. (To start command prompt press Windows button + R with your keyboard. A Run box will be appeared, type cmd in it and hit enter) In command prompt window, type "javac Test.java". 'javac' command compiles the java program and if you don't find any errors in your program, you will be taken to the same directory. Now type 'java Test'. Note that for compiling a program you have to write file name with java extension whereas for executing it you have to write only the file name with 'java' command.