marq

Dr. Charles Simonyi is the Father of Modern Microsoft Excel                                           JavaScript was originally developed by Brendan Eich of Netscape under the name Mocha, later LiveScript, and finally renamed to JavaScript.                                           The word "Biology" is firstly used by Lamarck and Treviranus                                           Hippocrates (460-370 bc) is known as father of medicine.                                           Galene, 130-200 is known as father of Experimental Physology                                           Aristotle (384-322 BC) is known as Father of Zoology because he wrote the construction and behavior of different animals in his book "Historia animalium"                                           Theophrastus(370-285 BC) is known as father of Botany because he wrote about 500 different plants in his book "Historia Plantarum".                                           John Resig is known as Father of Jquery -                                          HTML is a markup language which is use to design web pages. It was invented in 1990 by Tim Berners-Lee.                                                                The Google was founded by Larry Page and Sergey Brin.                                                                Rasmus Lerdorf was the original creator of PHP. It was first released in 1995.                                                               Facebook was founded by Mark Zuckerberg                                                               Bjarne Stroustrup, creator of C++.                                                                Dennis Ritchie creator of C                                                                                                                              James Gosling, also known as the "Father of Java"                                          At 11.44%, Bihar is India's fastest growing state                                          Father of HTML -Tim Berners Lee                                          orkut was created by Orkut Büyükkökten, a Turkish software engineer                    Photoshop: It came about after Thomas Knoll, a PhD student at the University of Michigan created a program to display grayscale images on a monochrome monitor which at the time was called 'Display'.

Packages

A package is a collection of related classes.

It helps Organize your classes into a folder structure and make it easy to locate and use them.

More importantly,It helps improve re-usability.

Syntax:-

package <package_name>;


The following video takes you through the steps of creating a package.


Assignment 1:To create a package

Step 1) Copy the following code into an editor

package p1;
class c1{
public void m1(){
System.out.println("Method m1 of Class c1");
}
public static void main(String args[]){
c1 obj = new c1();
obj.m1();
}
}



Step 2) Save the file as Demo.java. Compile the file as, javac – d . Demo.java



Step 3) Run the code as java p1.c1



Assignment 2) To create a sub-package



Step1) Copy the following code into an editor


package p1.p2;
class c2
{
public void m2(){
System.out.println("Method m2 of Class c2");
}
public static void main(String args[]){
c2 obj = new c2();
obj.m2();
}
}





Step 2) Save the file as Demo2.java. Compile the file as javac – d . Demo2.java




Step 3) Run the code as java p1.p2.c2



importing a package



To create an object of a class (bundled in a package), in your code, you have to use its fully qualified name.

Ex.


java.awt.event.actionListner object = new java.awt.event.actionListner();


But , it could become tedious to type in the long dot-separated package path name for every class you want to use. Instead it is recommended you use the import statement.


Syntax

import <package_name>;

Once imported , you can use the class without mentioning its fully qualified name.

Ex:
import java.awt.event.*; // * signifies all classes in this package
import javax.swing.JFrame // here only the JFrame class is imported
//Usage
JFrame f = new JFrame; // without fully qualified name.



Assignment 3: To import package

Step 1) Copy the code into an editor.

// Using packages created in earlier assignment
package p3;
import p1.*; //imports classes only in package p1 and NOT in the sub-package p2

class c3{
public void m3(){
System.out.println("Method m3 of Class c3");
}
public static void main(String args[]){
c1 obj1 = new c1();
obj1.m1();
p1.p2.c2 obj2 = new p1.p2.c2();
obj2.m2();
}
}



Step 2) Save the file as Demo2.java . Compile the file using the command javac –d .Demo2.java



Step3) Execute the code using the command java p3.c3



Packages - points to note:


To avoid naming conflicts packages are given names of the domain name of the company in reverse Ex : com.guru99. com.microsoft, com.infosys etc.
When a package name is not specified , a class is into the default package (the current working directory) and the package itself is given no name. Hence you were able to execute assignments earlier.
While creating a package, care should be taken that the statement for creating package must be written before any other import statements


// not allowed
import package p1.*;
package p3;


//correct syntax
package p3;
import package p1.*;

No comments:

Post a Comment