Java Programs

 

­­­­­                                      * STRING SORT*

 

 

import java.util.Arrays;

public class GFG

 {

        public static String sortString(String inputString)

        {

                char tempArray[] = inputString.toCharArray();

                 Arrays.sort(tempArray);

                   return new String(tempArray);

        }

        public static void main(String[ ] args)

        {

              String   inputString = "geeksforgeeks";

              String   outputString = sortString(inputString);

             System.out.println("Input String : " + inputString);

             System.out.println("Output String : " + outputString);

       }

}

 

 

OUTPUT:  

                            Input String :   geeksforgeekss

                            Output String : eeeefggkkorss

 

 

 

 

 

 

 

 

 

 

 

*MATRIX   MULTIPLICATION*

import  java.lang.*;

import  java.io.*;

class multiplication

{

      static int N = 4;

      static void multiply(int mat1[ ][ ],int mat2[ ][ ], int  result[ ][ ])

      {      

            int i, j, k;

            for (i = 0; i < N; i++)               

            {

                    for (j = 0; j < N; j++)

                   {

                          result[i][j] = 0;

                         for (k = 0; k < N; k++)

                         result[i][j] += mat1[i][k]  * mat2[k][j];

                    }

            }

       }

      public static void main(String[ ] args)

      {

            int mat1[ ][ ] = { { 1, 1, 1, 1 },

                                    { 2, 2, 2, 2 },

                                    { 3, 3, 3, 3 },

                                    { 4, 4, 4, 4 } };

        int mat2[ ][ ] = { { 1, 1, 1, 1 },

                                    { 2, 2, 2, 2 },

                                    { 3, 3, 3, 3 },

                                   { 4, 4, 4, 4 } };

          int  result[ ][ ] = new int[N][N];

          int  i, j;

          multiply(mat1, mat2, res);

          System.out.println("Result matrix"  + " is ");

          for (i = 0; i < N; i++)

 

 

 

 

           {

                     for (j = 0; j < N; j++)

                     System.out.print(res[i][j] + " ");

                     System.out.println( );

             }

      }

}

 

 

OUTPUT:     

                         Result matrix is

                          10   10   10  10

                         20   20   20   20

                         30   30   30   30

                         40   40   40   40

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

*CONSTRUCTOR*

 

import java.io.*;

public class Constructor

{

    double width,height,depth;

    Constructor( )

    {

       System.out.println(“Constructor without parameter”);

       width=10;

       height=10;

       depth=10;

    }

    Constructor(int a, int b,int c)

    {

        System.out.println(“Constructor with parameter”);

        width=a;

        height=b;

        depth=c;

        double volume( )

        {

            return  width * height * depth;

        }

    }

    class ConstructorDemo

    {

       public static void main(String args[ ])

       {

          Constructor Con = new Constructor( );

          double  vol;

          vol= Con.volume( );

          System.out.println(“Volume is “+vol);

          Constructor Con1 = new Constructor(8,11,9);

           vol=Con1.volume( );

           System.out.println(“Volume is “+vol);

       }

    }

 

 

OUTPUT:   

                          Constructor without parameter

 

                         Volume is 1000

 

                          Constructor with parameter

 

                          Volume is 792

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

*METHOD OVERLOADING*

 

import  java.lang.*;

import  java.io.*;

class Shape

{

    static final double PI = Math.PI;

    void Area(float a)

    {

        float A = a * a;

        System.out.println("Area of the Square: " + A);

    }

    void Area(double a)

   {

       double A = PI * a * a;

        System.out.println("Area of the Circle: " + A);

    }

    void Area(int a, int b)

    {

        int A = a * b;

        System.out.println("Area of the Rectangle: " + A);

    }

}

 class overloading

{

    public static void main(String[] args)

    {

        Shape obj = new Shape();

        obj.Area(10.5);

        obj.Area(3);

        obj.Area(5, 4);

    }

}

 

 

 

 

 

OUTPUT:   

 

               Area of the Circle: 346.36059005827474
               Area of the Square: 9.0
              Area of the Rectangle: 20

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

*METHOD OVERRIDING*

 

import java.io.*;

class Parent

{

     void show( )

    {

       System.out.println("Parent's show( )");

     }

}

class Child extends Parent

{

    void show( )

    {

        System.out.println("Child's show( )");

    }

}

class Main

{

      public static void main(String[ ] args)

      {

            Parent obj1 = new Parent( );

            obj1.show( );

           Parent obj2 = new Child( );

           obj2.show( );

    }

}

 

OUTPUT:   

                         Parent's show( )

 

                         Child's show( )

 

 

 

MULTIPLE INHERITANCE*

 

import java.io.*;

interface Backend

{

  public void connectServer( );

}

class Frontend

{

     public void responsive(String str)

     {

        System.out.println(str + " can also be used as frontend.");

     }

}

class Language extends Frontend implements Backend

 {

      String language = "Java";

      public void connectServer( )

      {

         System.out.println(language + " can be used as backend

                                                                                           language.");

      }

      public static void main(String[ ] args)

     {

           Language java = new Language( );

           java.connectServer( );

           java.responsive(java.language);

     }

 

}

 

OUTPUT: 

                         Java can be used as backend language

                         Java    can also be used as frontend       

 

 

*EXCEPTION  HANDLING*

 

import java.io.*;s

import java.lang.*;

class Main

{

   public static void main(String args[])

   {

      int val1, val2;

      try

          {

         System.out.println("Try Block:: Start");

         val1 = 0;

         val2 = 25 / val1;

         System.out.println(val2);

         System.out.println("Try Block:: End");

      }

      catch (ArithmeticException e)

      {

         

        System.out.println("ArithmeticException :: Divide by Zero!!");   

      }

      System.out.println("Outside try-catch:: Rest of the code.");

   }

}

 

OUTPUT:   

 

                          Try Block:: Start

 

                         ArithmeticException :: Divide by Zero!!

 

                        Outside try-catch:: Rest of the code.         

 

 

 

*MULTITHREADING *

 

import java.io.*;

class ABC implements Runnable 

{ 

        public void run( ) 

       { 

           try 

          { 

                 Thread.sleep(100); 

          } 

         catch (InterruptedException ie) 

        { 

                  ie.printStackTrace( ); 

        } 

        System.out.println("The state of thread t1 while it invoked the   

                method join( ) on thread t2 -"+ ThreadState.t1.getState( )); 

         try 

        { 

               Thread.sleep(200); 

        } 

        catch (InterruptedException ie) 

       { 

           ie.printStackTrace( ); 

       }    

   } 

} 

  public class ThreadState implements Runnable 

{ 

         public static Thread t1; 

         public static ThreadState obj; 

        public static void main(String args[ ]) 

       { 

            obj = new ThreadState( ); 

             t1 = new Thread(obj); 

 

 

   System.out.println("The state of thread t1 after spawning it - " +

                                                           t1.getState( )); 

   t1.start( ); 

   System.out.println("The state of thread t1 after invoking the

                         method start( ) on it - " + t1.getState( )); 

      } 

  public void run( ) 

{ 

        ABC myObj = new ABC(); 

        Thread t2 = new Thread(myObj); 

        System.out.println("The state of thread t2 after spawning it - "+      

                                                                          t2.getState( ));     

        t2.start( ); 

        System.out.println("the state of thread t2 after calling the

                    method    start( ) on it - " + t2.getState( )); 

        try 

       { 

              Thread.sleep(200); 

       } 

       catch (InterruptedException ie) 

       { 

           ie.printStackTrace( ); 

       }   

       System.out.println("The state of thread t2 after invoking the   

                 method    sleep( ) on it - "+ t2.getState( ) );                                    

       try 

       { 

            t2.join( ); 

       } 

       catch (InterruptedException ie) 

      { 

              ie.printStackTrace( ); 

       } 

 

     

 System.out.println("The state of thread t2 when it has completed it's

                                   execution - " + t2.getState( )); 

 

    } 

 

}

 

OUTPUT:  

 

The state of thread t1 after spawning it - NEW
The state of thread t1 after invoking the method start() on it - RUNNABLE
The state of thread t2 after spawning it - NEW
the state of thread t2 after calling the method start() on it - RUNNABLE
The state of thread t1 while it invoked the method join() on thread t2 -TIMED_WAITING
The state of thread t2 after invoking the method sleep() on it - TIMED_WAITING
The state of thread t2 when it has completed it's execution - TERMINATED

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

*CREATING A BOOK STRUCTURE *

 

import java.io.*;

class Book

{

    private String title;

    private String author;

    private int pages;

    private String publisher;

    private int year;

    public Book(String title, String author, int pages, String publisher,

                                                                                              int year)

    {

        this.title = title;

        this.author = author;

        this.pages = pages;

        this.publisher = publisher;

        this.year = year;

    }

    public Book(Book book)

    {

        this.title = book.title;

        this.author = book.author;

        this.pages = book.pages;

        this.publisher = book.publisher;

        this.year = book.year;

    }

    public String getTitle( )

    {

        return title;

    }

    public void setAuthor(String author)

    {

        this.author = author;

    }

     public boolean equals(Object o)

     {

        if (!(o instanceof Book))

        {

            return false;

        }

        Book other = (Book) o;

        if (this.title.equals(other.title) &&

            this.author.equals(other.author) &&

            this.publisher.equals(other.publisher) &&

            this.pages == other.pages && this.year == other.year)

            {

               return true;

            }

            return false;

    }

      public String toString( )

      {

        return String.format("%-10s %s\n", "Title:", title) +

            String.format("%-10s %s\n", "Author:", author) +

            String.format("%-10s %d\n", "Pages:", pages) +

            String.format("%-10s %s\n", "Publisher:", publisher) +

            String.format("%-10s %d", "Year:", year);

      }

}

public class Main

{

    public static void main(String[ ] args)

    {

        Book first = new Book("The Book", "Famous Writer", 199, "Big

                                                    Book Publisher", 1984);

        Book second = new Book("The Second Book", "Less F. Writer",

                                                 249, "The Book Publishers", 1999);

        System.out.println("First book");

        System.out.println(first + "\n");

        System.out.println("Second book");

        System.out.println(second + "\n");

        System.out.println("Title of the first book: " + first.getTitle( ) +

                                                                                  "\n");

        Book firstCopy = new Book(first);

        System.out.println("Copy of the first book:");

        System.out.println(firstCopy + "\n");

        System.out.println("Copy equals first book: " +

                                              firstCopy.equals(first));

        System.out.println("Second book equals first book: " +

                                                second.equals(first));

        System.out.println( );

        System.out.println("Change writer of the copy to :'Most F.

                                                                                 Writer'");

        firstCopy.setAuthor("Most F. Writer");

        System.out.println(firstCopy + "\n");

        System.out.println("Copy equals first book: " +  

                                                       firstCopy.equals(first));

            }

}

OUTPUT:

                                     First book
                                     Title:  The Book
                                     Author:    Famous Writer
                                     Pages:     199
                                     Publisher: Big Book Publisher
                                     Year:      1984
 
                                     Second book
                                     Title:   The Second Book
                                     Author:    Less F. Writer
                                     Pages:     249
                                     Publisher: The Book Publishers
                                     Year:      1999
 
 
 
 
                                
 
                                    Title of the first book: The Book
                                    Copy of the first book:
                                    Title:     The Book
                                    Author:    Famous Writer
                                    Pages:     199
                                    Publisher: Big Book Publisher
                                    Year:      1984
 
                                   Copy equals first book: true
                                   Second book equals first book: false
 
                                  Change writer of the copy to :'Most F. Writer'
                                  Title:     The Book
                                  Author:    Most F. Writer
                                  Pages:     199
                                  Publisher: Big Book Publisher
                                  Year:      1984
 
                                 Copy equals first book: false

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

*JDBC  CONNECTION *

import java.sql.*;

 public class GFG

{

    public static void main(String arg[ssss])

    {

        Connection connection = null;

         try {

            Class.forName("com.mysql.cj.jdbc.Driver");

            connection = DriverManager.getConnection(   

                                          "jdbc:mysql://localhost:3306/mydb",

                                                           "mydbuser", "mydbuser");

            Statement statement;

            statement = connection.createStatement );

            ResultSet resultSet;

            resultSet = statement.executeQuery(   

                                                                "select * from designation");

            int code;

            String title;

            while (resultSet.next( ))

            {

                code = resultSet.getInt("code");

                title = resultSet.getString("title").trim();

                System.out.println("Code : " + code  + " Title : " + title);

            }

            resultSet.close( ),  statement.close( ),connection.close( );

        }

        catch (Exception exception)

        {

            System.out.println(exception);

        }

    }

}}

OUTPUT:

 

   java.lang.sClassNotFoundException:  com.mysql.cj.jdbc.Driver       

Comments

Popular posts from this blog

digital marketing ppt-u1

SOFTWARE

cn lab

Computer Operations and Performing - D L Unit-1-1

DBMS Degree Lab Records

DS-Record-mca-04) write a program for evaluating a given postfix expression using stack.

Unit 2: Foundations of Ownership, Security Related Concepts in Blockchain

Unit-1 Foundations of Software Systems and Blockchain

Access the Internet to Browse Infromation & E-Mail Operation- D L Unit-2-1

6)what are the various service of internet and protocols ICT-unit-1