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>