on Leave a Comment

Java Program to Open Notepad

This java program opens notepad by Runtime class. Notepad is an application comes with Windows operating system. Notepad is text editor. In this program, we create an object of Runtime class and calling exec() method by passing parameter as application name. 

Java Program to Open Notepad

import java.util.*;
import java.io.*;
 
class OpenNotepad {
  public static void main(String[] args) {
   System.out.println("Opening Notepad...");
    Runtime rt = Runtime.getRuntime();
 
    try {
      rt.exec("notepad");
    }
    catch (IOException e) {
      System.out.println(e);
    }   
  }
}

Output:

Opening Notepad...

Java Program to Open Notepad
Notepad




on Leave a Comment

Static and Dynamic Binding in Java

Binding is the association of method call to method body. Binding can be done at compile time and run time. Compile time binding is known as static binding and run time binding is known as dynamic binding.

Static Binding

In static binding, method call is associated with method body at the compile time. When we overload methods, it is important to resolve binding. In case of overloaded methods, binding is resolved at compile time. But we override method, binding cannot be resolved at compile time. Static binding uses only Type information for binding. Binding of methods such as static, private and final is always static binding. Because static, private and final methods cannot be overridden, so they can be only called by object of local class (static method is called by class name). So, binding of static, private and final method needs only Type information.

Example

class ParentClass{
    static void show(){
        System.out.println("Parent Class");
    }
}
public class ChildClass extends ParentClass {
    static void show(){
        System.out.println("Child Class");
    }
    public static void main(String[] args){
        ParentClass obj1 = new ChildClass();
        ParentClass obj2 = new ParentClass();
        obj1.show();
        obj2.show();
    }
}

Output:

Parent Class
Parent Class

Dynamic Binding

Dynamic binding is done at run time. When both parent class and child class defines the same method, in this case object type determines which method to call. And type of object is determined at run time. So, binding of overridden methods is always a dynamic binding.

Example

class ParentClass{
    void show(){
        System.out.println("Parent Class");
    }
}
public class ChildClass extends ParentClass {
    void show(){
        System.out.println("Child Class");
    }
    public static void main(String[] args){
        ParentClass obj1 = new ChildClass();
        ParentClass obj2 = new ParentClass();
        obj1.show();
        obj2.show();
    }
}

Output:

Child Class
Parent Class



on Leave a Comment

Java Program to Find the Largest of Three Numbers

In this article we'll see how to find largest of three numbers in java. 

We will see, Java program to find the largest of three numbers using
(1) If-Else..If Statement
(2) Nested If..Else Statement

Example 1: Java Program to Find the Largest of Three Numbers Using If-Else..If Statement

import java.util.Scanner;
public class LargestOf3{

    public static void main(String[] args) {

        int a, b, c;
        Scanner read = new Scanner(System.in);
        System.out.print("Enter Three Numbers: ");
        a = read.nextInt();
        b = read.nextInt();
        c = read.nextInt();
        
        if( a >= b && a >= c)
            System.out.println(a + " is the largest number.");

        else if (b >= a && b >= c)
            System.out.println(b + " is the largest number.");

        else
            System.out.println(c + " is the largest number.");
    }
}

Output:

Enter Three Numbers: 10 20 30
30 is the largest number.

Example 2: Java Program to Find the Largest of Three Numbers Using Nested If..Else Statement

import java.util.Scanner;
public class LargestOf3{

    public static void main(String[] args) {

        int a, b, c;
        Scanner read = new Scanner(System.in);
        System.out.print("Enter Three Numbers: ");
        a = read.nextInt();
        b = read.nextInt();
        c = read.nextInt();

        if(a >= b) {
            if(a >= c)
                System.out.println(a + " is the largest number.");
            else
                System.out.println(c + " is the largest number.");
        }
        else {
            if(b >= c)
                System.out.println(b + " is the largest number.");
            else
                System.out.println(c + " is the largest number.");
        }
    }
}

Output:

Enter Three Numbers: -10 0 10
10 is the largest number.




on Leave a Comment

Java Program to Add Two Matrices

This java program adds two matrices. First, user enters elements of two matrices, then addition of both matrices is performed using for loop. Finally sum of both matrices is displayed on the screen.

Java Program to Add Two Matrices

import java.util.Scanner;
class AddMatrices
{
 public static void main(String[] args) {
 int a[][] = new int[3][3];
        int b[][] = new int[3][3];
        int c[][] = new int[3][3];
 int i, j;
 Scanner read = new Scanner(System.in);
 System.out.print("Enter the elements of matrix1: ");
 for(i = 0; i<a.length; i++)
 for (j = 0; j<a.length; j++) {
  a[i][j] = read.nextInt();
 }

 System.out.print("\nEnter the elements of matrix2: ");
 for(i = 0; i<b.length; i++)
  for (j = 0; j<b.length; j++) {
   b[i][j] = read.nextInt();
  }

 System.out.println("\nAdding matrix1 and matrix2...");
 for(i = 0; i<b.length; i++)
  for (j = 0; j<b.length; j++) {
   c[i][j] = a[i][j] + b[i][j];
  }

 System.out.println("\nSum of matrix1 and matrix2 is:");
 for(i = 0; i<c.length; i++){
                for(j = 0; j<c.length; j++){
                        System.out.print(c[i][j]+ "\t");
                }
                System.out.println();
        }
 }
}

Output:

Enter the elements of matrix1: 1
2
3
4
5
6
7
8
9

Enter the elements of matrix2: 1
2
3
4
5
6
7
8
9

Adding matrix1 and matrix2...

Sum of matrix1 and matrix2 is:
2       4       6
8       10      12
14      16      18


on Leave a Comment

OSI Model: Presentation Layer and its Functions

Presentation layer is layer 6 of OSI model. Presentation layer concern about the syntax and semantics of information exchanged between systems. Presentation layer is also known as data translate because it translates the data in such a way the receiver will understand the data. After formatting the data, presentation layer passes data to the application layer for further processing and display. There are many other important functions of presentation layer.

Functions of Presentation Layer

Translation: Communicating systems exchange information of different formats such as string, numbers and so on. Presentation layer converts this information into bit stream before transmitting. The presentation layer on receiver converts these data into receiver-dependent format.

Encryption: To ensure privacy, presentation layer perform encryption at transmitter side and decryption at the receiver side.

Compression: The presentation layer performs compression to reduce the bits contained in the information. It reduces the bandwidth of information. Compression is important for transmitting multimedia such as text, audio and video.