on Leave a Comment

What is the World Wide Web (WWW)?

The World Wide Web is a repository of information linked together through hyperlinks all over the world. As Internet have many services and World Wide Web is one of them. The World Wide Web supports special document that are formatted in HTML. Tim Berners-Lee, an English scientist, invents WWW in 1989. Berners-Lee developed hyperlinks to link web pages. User can easily access these web pages through WWW repository.

The WWW is distributed client server service, in which a client access web pages by web browser. These webpages is stored on server. The web browser sends requests to the server for specific web page using URL. After receiving the request, the server sends requested web pages and then these web pages is displayed on browser screen, it is a short description. 

on Leave a Comment

Packages in Java

Package is a way of grouping a variety of classes and interfaces together. In java, package is a way to organize set of related classes and interfaces. In simple words, a package is similar to different folders in our computer. We keep images in one folder, songs in other folder and so on. So, we keep set of related documents in one folder. There can be two classes in two different package can have same name as two files having same name in two different folders in our computer.

In java, we have ability to reuse code by extending classes and implementing interfaces. But if we have to reuse the code written in another program without copying the code. This can be achieved by using packages. Java provides a class library for use in our own application called API or Application Programming Interface. This API provides large number of classes grouped into different packages according to their functionality.

Advantages of Package in Java

(1) While making a large project, we have many classes and interfaces, so it is required to group set of related classes and interfaces into packages. It provides better organisation and improves efficiency.

(2) We can put two classes of same name in two different package, so it avoids name collision.

(3) Java package provides reusability of code by using different classes and interfaces in different packages.

(4) We can't directly access the code of classes and interfaces, so it provides access protection.

Types of Package in Java

Built-in package

Java already provides many different packages like java.lang.*, java.io.* known as built-in packages.

User defined package

We can also create our own package known as user defined package.

Example of Java Package

package showMe;

public class Show{
 public void disp(){
  System.out.println("Hello");
 }
}

import showMe.Show;

public class Demo{
 public static void main(String[] agrs){
  Show obj = new Show();
  obj.disp();
 }
}

Output:

Hello

In this example, first we create a package namely "showme" using package keyword. We define a class called "Show" and this class containing only one method called "disp". While putting a class to a package, the class must be of public access modifier.

We can create another class called "Demo", this class imports "showMe" using import keyword. In Demo class, we create an object of "Show" class of "showMe" package and called a method called "disp".

To Compile: javac -d . Show.java
Then again, javac Demo.java
To run Demo class: java Demo

Example 2:

We can create a class inside a package while importing another package.

package Package2;
import showMe;

class Example{
 public static void main(String[] args){
 Show obj = new Show();
 obj.disp();
 }
}

In this program, first we write package declaration and then we import another package.

It is important to know that if we write package declaration statement in our program, then it must be the first statement of program.

Example 3:

If you don't want to write import statement, then you can use fully qualified name.

Greatest.java

package Package1;
public class Greatest{
  public void checkGreatest(int a, int b){
    if(a>b)
   System.out.println(a+" is greater than "+b);
 else
   System.out.println(b+" is greater than "+a); 
  }
}

Example.java

class Example{
 public static void main(String[] args){
 Package1.Greatest obj = new Package1.Greatest();
 obj.checkGreatest(10, 20);
 }
}

Output:

20 is greater than 10

Sub package in Java

We can create a package inside another package. For example, java has also sub packages like lang, io, net etc.

Example:

Show.java

package Package1.SubPackage1;
public class Show{
 public void disp(){
  System.out.println("Welcome");
 }
}

Example.java

import Package1.SubPackage1.*;
class Example{
 public static void main(String[] args){
 Show obj = new Show();
 obj.disp();
 }
}

Output:

Welcome








on Leave a Comment

Java Program to Check Whether Two Strings are Anagram or Not

This java program checks whether two strings are anagram or not. Anagram means producing a new word using rearranging of letters of a word, length of letters must be same in both new and previous words. In this java program, user will enter two strings and the code of checking anagram runs and show whether both strings are anagram or not. 

Java Program Check Anagram or Not

Example 1:

In this program, first we check the length of both strings, if length is same then runs the if blocks, otherwise run the else block. In else block, print "Strings are not Anagram". And in the if block, we take first letter of first string and compare it with all letters of second string. If the letter not matches, print "Not Anagram" and exit the program. After checking first letter of first string, check another letter. When all letter string 1 matches with string 2, prints "Strings are Anagram".

import java.util.Scanner;
public class CheckAnagram{
	public static void main(String[] args) {
		String str1, str2;
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter First String: ");
		str1 = sc.next();
		System.out.print("Enter Second String: ");
		str2 = sc.next();

		int i, j, found=0;
		if(str1.length() == str2.length()){
			for(i=0; i<str1.length(); i++)
			{
				for(j=0; j<str1.length(); j++){
					if(str1.charAt(i) == str2.charAt(j)){
						found =1;
					}
				}
				if(found==0){
					System.out.println("Strings are not Anagram");
					System.exit(0);
				}		
			}
			System.out.println("Strings are Anagram");	
		}
		else{
			System.out.println("Strings are not Anagram");
		}	
	}
}

Output:

Enter First String: triangle
Enter Second String: integral
Strings are Anagram

Enter First String: alpha
Enter Second String: beta
Strings are not Anagram

Example 2:

In this program, first we check the length of both strings and if length is same if block runs, otherwise else block runs. In else block, print "Strings are not Anagram". And in if block, sort both array and directly compares string 1 with string 2. If both strings matches print "Strings are Anagram", otherwise print "String are not Anagram".

import java.util.Scanner;
import java.util.Arrays;
public class CheckAnagram{
	public static void main(String[] args) {
		String str1, str2;
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter First String: ");
		str1 = sc.next();
		System.out.print("Enter Second String: ");
		str2 = sc.next();
        
        char c1[] = str1.toCharArray();
        char c2[] = str2.toCharArray();
        str2 = str2.replaceAll("\\s", "");
        Arrays.sort(c1);
        Arrays.sort(c2);
		if(str1.length() == str2.length()){
			if(Arrays.equals(c1, c2))
				System.out.println("Strings are Anagram");
			else
				System.out.println("Strings are not Anagram");
		}
		else{
			System.out.println("Strings are not Anagram");
		}	
	}
}

Output:

Enter First String: triangle
Enter Second String: integral
Strings are Anagram

Enter First String: alpha
Enter Second String: beta
Strings are not Anagram




on Leave a Comment

Left Rotation of Array in Java

In this article, w will see how to rotate elements of array on left  hand side.

First, we ask the user to enter length of array and number of left rotation. For example, if user enters 3 left rotation for [1, 2, 3, 4, 5] then output will be [4, 5, 1, 2, 3]. Length and number of left rotation can't be a floating point number.

Steps:

Here is length of array and is number of left rotation of array.

- Store the first array element in temp variable.
- Shift the array elements on left side by

for(i=0; i<n-1; i++){
   arr[i] = arr[i+1];
}

- Then assign the temp value to arr[n-1]
- Repeat above steps r times.

Java Program to Left Rotation of Array

import java.util.Scanner;
public class LeftRotation{
 public static void main(String[] args){
  int temp, i, r, j, n;
  Scanner sc = new Scanner(System.in);
  System.out.print("Enter Length of Array: ");
  n = sc.nextInt();
  System.out.print("Enter Number of Left Rotation Array: ");
  r = sc.nextInt();
  int[] arr = new int[n];
  System.out.print("Enter Array Elements: ");
  for(i=0; i<n; i++)
   arr[i] = sc.nextInt();
  for(j=0; j<r;j++){
   temp = arr[0];
   for(i=0; i<n-1; i++){
    arr[i] = arr[i+1];
   }
   arr[n-1]=temp;
  }
  System.out.print("Elements are ");
  
  for(int a:arr){
  System.out.print(a+" ");
  }
  
 }
}

Output:

Enter Length of Array: 5
Enter Number of Left Rotation Array: 3
Enter Array Elements: 1 2 3 4 5
Elements are 4 5 1 2 3