VR23 Object Oriented Programming through Java Lab



 
                         Object Oriented Programming through Java Lab 

Course Objectives: 

The aim of this course is to  

  • Practice object oriented programming in the Java programming language 
  • Implement Classes, Objects, Methods, Inheritance, Exception, Runtime            Polymorphism, User defined Exception handling mechanism 
  • Illustrate inheritance, Exception handling mechanism,  JDBC connectivity  
  • Construct Threads, Event Handling, implement packages, Java FX GUI 
Experiments covering the Topics: 
  • Object Oriented Programming fundamentals- data types, control structures 
  • Classes, methods, objects, Inheritance, polymorphism,  
  • Exception handling, Threads, Packages, Interfaces 
  • Files, I/O streams, JavaFX GUI 
Sample Experiments: 

Exercise – 1: 
a) Write a JAVA program to display default value of all primitive data type of JAVA   Program
b) Write a java program that display the roots of a quadratic equation ax2+bx=0. Calculate the discriminate D and basing on value of D, describe the nature of root.  Program
Exercise - 2: 
a) Write a JAVA program to search for an element in a given list of elements using binary search mechanism. Program
b) Write a JAVA program to sort for an element in a given list of elements using bubble sort   Program
c) Write a JAVA program using String Buffer to delete, remove character.   Program
Exercise - 3 :
a) Write a JAVA program to implement class mechanism. Create a class, methods and invoke them inside main method.  Program
b) Write a JAVA program implement method overloading. Program
c) Write a JAVA program to implement constructor. Program
d) Write a JAVA program to implement constructor overloading. Program
Exercise - 4 
a) Write a JAVA program to implement Single Inheritance   Program
b) Write a JAVA program to implement multi level Inheritance   Program

c) Write a JAVA program for abstract class to find areas of different shapes   Program
Exercise - 5 
a) Write a JAVA program give example for “super” keyword.   Program
b) Write a JAVA program to implement Interface. What kind of Inheritance can be achieved?  Program
c) Write a JAVA program that implements Runtime polymorphism  Program
Exercise - 6 
a) Write a JAVA program that describes exception handling mechanism  Program
b) Write a JAVA program Illustrating Multiple catch clauses   Program
c) Write a JAVA program for creation of Java Built-in Exceptions  
d) Write a JAVA program for creation of User Defined Exception  
Exercise - 7 
a) Write a JAVA program that creates threads by extending Thread class. First thread display “Good Morning “every 1 sec, the second thread displays “Hello “every 2 seconds and the third display “Welcome” every 3 seconds, (Repeat the same by implementing Runnable)   
b) Write a program illustrating is Alive and join () 
c) Write a Program illustrating Daemon Threads.   
d) Write a JAVA program Producer Consumer Problem 
Exercise – 8  
a) Write a JAVA program that import and use the user defined packages 
b) Without writing any code, build a GUI that display text in label and image in an Image View (use JavaFX) 
c) Build a Tip Calculator app using several JavaFX components and learn how to respond to user interactions with the GUI 



Exercise - 1 (Basics)


a). Write a JAVA program to display default value of all primitive data type of JAVA


import java.io.*;

class Primitivevalues

{

 static boolean B1;

 static byte T1; 

 static int I1; 

 static float F1;

 static double D1; 

 static char C1;

 public static void main(String args[])

 {


  System.out.println("static  variable  Default values for BYTE:"+T1);

  System.out.println("static  variable  Default values for INT:"+I1);

  System.out.println("static  variable  Default values for FLOAT:"+F1);

  System.out.println("static  variable  Default values for DOUBLE:"+D1);

  System.out.println("static  variable  Default values for CHAR:"+C1);

  System.out.println("static  variable  Default values for BOOLEAN:"+B1);

 }

}


b). Write a java program that display the roots of a quadratic equation ax2+bx=0. Calculate the discriminate D and basing on value of D, describe the nature of root.




import java.io.*;

import static java.lang.Math.sqrt;

import java.util.*;

class Roots

{


  void m1(int a,int b,int c)

 {

  double value=(Math.pow(b,2.0)-4*a*c);

   double discriminant=(double)Math.sqrt(value);

  if(value>0)

  {

   System.out.println("Roots are real numbers");

   double root1=(discriminant-b)/(2*a);

      double root2=(-b-discriminant)/(2*a);

      System.out.println("FirstRoot"+root1+"\nSecond Root"+root2);

  }

  else if(value==0)

  {

   double root1=(discriminant-b)/(2*a);

   System.out.println("Polynomial has one Root"+root1);

  }

  if(value<0)

  {

   System.out.println("Roots are imaginary numbers");

   System.out.println((-b+"i"+"+"+(-1*value))+"/"+(2*a));

   System.out.println((-b+"i"+"-"+(-1*value))+"/"+(2*a));

  }



 }

 public static void main(String [] args)

  {

   int a,b,c;

   System.out.print("Enter a,b,c values:");

   Scanner br=new Scanner(System.in);

    a=br.nextInt();

    b=br.nextInt();

    c=br.nextInt();

    new Roots().m1(a,b,c);

  }

}


Exercise - 2 (Operations, Expressions, Control-flow, Strings)


a). Write a JAVA program to search for an element in a given list of elements using binary search mechanism.


import java.util.Scanner;

import java.io.*;

class Binarys

{

  public static void main(String args[])

   {

      int c,first,last,middle,n,search,array[];

      boolean status=false;

       Scanner s=new Scanner(System.in);

       System.out.println("Enter number of elements:");

       n=s.nextInt();

       array=new int[n];

        System.out.println("Enter"+n+"integer:");

      for(c=0;c<n;c++) 

        array[c]=s.nextInt();

     for (int i = 0; i < array.length; i++)

         {

            for (int j = i + 1; j < array.length; j++)

            {

                if (array[i] > array[j])

                {

                  int  temp = array[i];

                    array[i] = array[j];

                    array[j] = temp;

                }

            }

      }

        System.out.println("Enter value to find:");

        search=s.nextInt();

        first=0;

        last=n-1;

        middle=(first+last)/2;

    for(int i=0;i<n;i++)

     {

        if(first<=last)

         {

             if(array[middle]<search)

                  first=middle+1;

             else if(array[middle]==search)

              {

                  status=true;

              }

           else

            {

                last=middle-1;

            }

                 middle=(first+last)/2;

         }

    }

       if(status==true)

       {

           System.out.println(search+"found at location"+(middle+1));

       }

     else

         System.out.println(search+"is not found in the list");

   }

}

       

b). Write a JAVA program to sort for an element in a given list of elements using bubble sort.


import java.util.Scanner;

public class Bubblesort

{

 public static void main(String[] args)

 {

         Scanner s=new Scanner(System.in);

         System.out.println("enter the size of the array:");

         int size=s.nextInt();

         System.out.println("enter the values into the array:");

         int arr[]=new int[size];

         for(int i=0;i<size;i++)

         {

              arr[i]=s.nextInt();

         }

         sorting(arr);

     }

     public static void sorting(int arr[])

     {

         int n=arr.length;

         int temp=0;

         for(int i=0;i<n;i++)

         {

           for(int j=1;j<(n-i);j++)

          {

            if(arr[j-1]>arr[j])

            {

               temp=arr[j-1];

               arr[j-1]=arr[j];

               arr[j]=temp;

            }

          }

        }

         System.out.println("the sorted arraty is:");

         for(int i=0;i<arr.length;i++)

         {

            System.out.print(arr[i]+"\t");

         }

     }

}

C) Write a JAVA program using StringBufferto delete, remove character.

import java.util.Scanner;
import java.lang.*;
class stringbuffer
{
  public static void main(String args[])
  {
     Scanner s=new Scanner(System.in);
     System.out.println("enter a string you like:");
     String str=s.nextLine();
     StringBuffer b=new StringBuffer(str);
     System.out.println("enter the index you want to delete from:");
      int i1=s.nextInt();
     System.out.println("to:");
     int i2=s.nextInt();
     b.delete(i1,i2);
     System.out.println("after deletion the new buffer is:"+b);
  }
}


 

No comments

File Uploading in PHP

  File Uploading in PHP PHP allow you to upload any type of a file i.e. image, binary or text files.etc..,PHP has one in built global variab...