UBJSTRAINING
-
Upload
uddalak-banerjee -
Category
Documents
-
view
216 -
download
0
Transcript of UBJSTRAINING
-
8/8/2019 UBJSTRAINING
1/57
0
Saturday, 11 September 2010
Java Training
A pdf file composed by Uddalak Banerjee ,Emp Id -60527 ,Grade
A0,Date of joining-5th
aug,2010 based on Java-training at Birlasoft
conducted by Mr Abhishek Shahi .
-
8/8/2019 UBJSTRAINING
2/57
1
Saturday, 11 September 2010
CONTENTS
1.Software Requirements
2.Steps to Install Eclipse
3.How to open a project file
4.How to configure build path
5.Sample Java program.
6.Access Modifiers
7.Methods
8.Compilation and Exception Handling using try-catch-finally.
9.Polymorphism concepts
10 Abstract Classes and interfaces
11 Inheritance
12 Composition
13.How to Debug a file in Eclipse.
-
8/8/2019 UBJSTRAINING
3/57
2
Saturday, 11 September 2010
13.Control Statements
14.Wrapper Classes
15.Creating new Exceptions
16.Static blocks,anonymous classes.
17.File handling.
18.Collection(ArrayList,LinkedList,HashMap)
19.Nested Classes
20 Tomcat Server installation and working
21.How to create .War file
22.How to create Servlet
-
8/8/2019 UBJSTRAINING
4/57
3
Saturday, 11 September 2010
1.Software Requirements
Softwares to install:
1.eclipse-jee-galileo-SR2-win32
2.jdk-6u3-windows-i586-p
3.apache tomcat 6.0.20.zip
2.Steps To Install Eclipse
Step 1. Cut Eclipse-jee-galileo-SR2-win32 and paste in host folder
Step 2. Install jdk-6u3
Step 3.Create shortcut on desktop
Note:Preferred Setting of workspace based on disk space
availability can be done by manually fixing the path at the time of
installation.
3.HOW TO OPEN A PROJECT FILE
-
8/8/2019 UBJSTRAINING
5/57
4
Opening a Project File
Step 1:
File/New/Other/Java/JavaProject/Next/ProjectName(java training)
-
8/8/2019 UBJSTRAINING
6/57
5
Jre is set to Use execution JRE(default)/
Step 2:
PRESS Next button and set path for src files(Eg javatraining/src)
And class files (Eg: javatraining/bin)
Step 3:
-
8/8/2019 UBJSTRAINING
7/57
6
Libraries/jreSystemLibrary[javaSE 1.6]
/Addjar(If libraries are available in own project)
PRESS FINISH AND YES
4.How to configure build path
Setting of build path:
-
8/8/2019 UBJSTRAINING
8/57
7
RightClick Project/Buildpath/Configure buildpaths/Source.
Add Library/JreSystemLibrary
Java Uses Optimisation
Create a file
Src/New/File/Filename
Note:Name Start with Capital Letter
-
8/8/2019 UBJSTRAINING
9/57
8
-
8/8/2019 UBJSTRAINING
10/57
9
FILE STRUCTURE studied so far:
For any project we have a project name and followed by two
folders one is source and the other bin.Source folder contains
Java source files and bin contains class files generated by the
compiler on compilation of the source files.
A SAMPLE JAVA program:
-
8/8/2019 UBJSTRAINING
11/57
10
The output of the above code comes out to be:
Right Click/RunAs/java application
-
8/8/2019 UBJSTRAINING
12/57
11
Debugging a java file/project
Keys that are used for debugging are :
F5----------used to get into the code
F6----------Debugging-executing each statement and moving to the
next
F3---------Open declaration
Alt+left----Go back at debugging time.
To open the debugging perspective right click on the code and
select java application.
First creating a java application uddalak3.java which contains a
display method , a constructor and a main method under the class
named uddalak3.
-
8/8/2019 UBJSTRAINING
13/57
12
Now while debugging the code we first open the debug perspective
which looks as follows:
The dots which are clearly evident are the break points.
Use of f3 key gives the following scenario:
Suppose we press f3 on the uddalak3.display() method we get the
to the display method definition with the name display()
highlighted.
-
8/8/2019 UBJSTRAINING
14/57
13
OOPS(Object Oriented Programming Structure)
Three basic concepts of polymorphism were 1.encapsulation
2.inheritance and 3.polymorphism.
Use of inheritance is to enhance reusability of code.
Use of abstraction is data hiding.
Use of polymorphism is for data security(over riding) and flexibility
in code usage based on need(over loading)
Properties of Class/Object
1.Filename ------------Mandatory
2.Access Modifier-----Mandatory
3.Package
4.Methods
-
8/8/2019 UBJSTRAINING
15/57
14
Saturday, 11 September 2010
5.Classname------------Mandatory
Important
1.There can be multiple classes in a program but one of themhas to be of same name as file name(It should be public)
2.Whatever class has main method the file should be of thesame name (as the class containing main())
3.There could not be more than one public class in the file.CHECK POINTS:
Q1.If 2 classes without access modifier is used what will happen?
Ans:It would produce an error.
Defination of Methods:
Methodname( parameters)
Example:
Public void main (String args[])
Access Modifiers
1.Public(Anyone can access)
2.Private(Only by you)
3.Protected(Somehow associated to you to access your data)
4.Default(Concept of package)
*Default cannot be accessed outside the host folder
#In Methods there is no concept called Default.
-
8/8/2019 UBJSTRAINING
16/57
15
Saturday, 11 September 2010
#Without Object incase of main jvm creates an object.
COMPILATION
Javac
Options
-D----- destination of class file to be specified at command prompt.
EXECUTION
Java *.class
JAVA VIRTUAL MACHINE
1.JVM(Java Virtual Machine)
2.JRE(Group of class files required to execute a program)
3.API(Application Programming Interface)
#States the a program is to be written.It is collection of jar files.
a.Library
b.Jar(java archive) It is a zip file
c.package
JVM is an O.S on top of another O.S
-
8/8/2019 UBJSTRAINING
17/57
-
8/8/2019 UBJSTRAINING
18/57
17
Properties of final keyword:::
1.FINAL cannot be inherited .
2.Final method cannot be overridden.
3.But final can be called by some other method.
Final variables are constants.Constants------------1.static and 2.final
CHECK POINT:
IF A VARIABLE IS DECLARED FINAL THEN WILL IT BE AUTOMATICALLY
STATIC?
HERE
-
8/8/2019 UBJSTRAINING
19/57
18
HER
HERE,FATHER IS THE PARENT/BASE CLASS
SON1,SON2 ARE THE CHILD/DERIVED
Keywords needed are:1. implements and 2.extends
Example:-Class father{
Public House;
Protected V1;
}
Class Son1 extends Father {
}
-Son1 can access protected variable V1.
-
8/8/2019 UBJSTRAINING
20/57
19
Cases:
Son 1 extends Father is Right
Son 2 extends Father is Right
Cases:
Son1 extends Father is wrongSon 2 extends Son1 is wrong
OverloadingIt is the process of using the same function name with variable number
of parameters .
-
8/8/2019 UBJSTRAINING
21/57
20
Polymorphism involves two basic concepts:
1.Overriding+Overloading
2.Static +Runtime Binding
Interfaces
Properties of interfaces are as follows:
1.Variables are static and final
2.public
3.METHODS without implementation
-
8/8/2019 UBJSTRAINING
22/57
21
Key points:
1.Interfaces extend other interfaces
2.classes extend classes
-
8/8/2019 UBJSTRAINING
23/57
22
3.classes implement one or more interfaces at the same time but
extend only one class at a time.
Examples:
Binding1.static [ Example: child1 objclass1= new child1();]
2.runtime[ Example: objclass1=objParent;]
Compositions
-Without extending ,inheriting direct use.
-protected members cannot be used.
#Interface to be created in separate file
Marker Interfaces
THESE interfaces have no body.
Eg : public interface Root{
}
Comments
-
8/8/2019 UBJSTRAINING
24/57
-
8/8/2019 UBJSTRAINING
25/57
24
Calculate()in calculator class.
Use Extends
Try Debugging
Note ErrorSolution to the above assignment as executed by uddalak banerjee
,Employee id=60527 ,grade =A0 ,date of joining 5th
aug ,2010 is as
follows:
-
8/8/2019 UBJSTRAINING
26/57
25
OUTPUT DISPLAYED:
//18.08.2010
Control Statements
1 .if else
-
8/8/2019 UBJSTRAINING
27/57
26
2 switch
3.while
4. do while
5 for
Syntax of the above control statements is demonstrated as below:
For each statements
These are statements used for handling arrays.
Eg:
for(int a:arr)
{
System.out.println(a);}
Operators:
Unary: Eg ++,--
Binary:Eg +,-,*,\
-
8/8/2019 UBJSTRAINING
28/57
27
Ternary:?
ABSTRACT
abstract class a{
public int sum(int a,int b);}
Properties of abstact classes:
1.In interfaces all methods are abstract
2.All extending classes of abstract classes must give body to all the
abstract methods
3.We may not have abstract methods in an abstract class hence we
use abstract classes when we already have interfaces.
HOW TO UNDERSTAND WHETHER TO USE AN INTERFACE OR AN
ABSTRACT CLASSES
-
8/8/2019 UBJSTRAINING
29/57
28
Suppose we need to implement the above scenario of automobile
industry in java.In that case we define car as an interface.The
reason behind using car as an interface is that car contains a few
basic things like 4 wheels ,an engine,popularshaft(for power
transmission),chasse etc.Now, while Maruti ,Ford and Hyundai all
build cars of different engine capacity and with different chassedesign and popular shaft sizes and shapes each of these
companies(classes) essentially implement the basic features like 4
wheels ,engine,popularshaft,chasse etc.So we define the contracts
in the interface which essentially contains the basic ingrediants
which are later given body by the sub classes which implement the
interface(car).
Data TypesInt
Float
Double
Char
Long
-
8/8/2019 UBJSTRAINING
30/57
29
Boolean
Byte
String
Wrapper ClassesData Transfer takes place as objects only.
Uses of Wrapper classes
1.Wrapper Classes are used in case of type casting
2.Provides objects necessary for data transfer.
Use:
int INUM=10;
Integer iobj=Integer.parseInt(INUM);
All Datatypes have wrapper classes.
Long.intValue()----converts it into integer value.
Autoboxing
It was used to reduce overhead involved in type conversion.
-
8/8/2019 UBJSTRAINING
31/57
30
[Available in java 5]
All exceptions created from runtime exceptions are unchecked
exceptions.
-
8/8/2019 UBJSTRAINING
32/57
31
TRY-CATCH
For SQL code SQLexception must be caught.
Something to muse over:
Q.Can Main method be used as a part of abstract class?
Q.Can Main method be used in another class than abstract class when it is
already declared public?
Class Example:
Abstract public class AbstractTest{
Public int sum(int a ,int b){}
Abstract public float calcInterest(int p,int r,int t);
}
19.08.2010
-
8/8/2019 UBJSTRAINING
33/57
32
Saturday, 11 September 2010
Q.identify errors :
Abstract class A{
Public sum(int a ,Integer b)
{
Float f=a+b;
System.out.println(f);
}
}
Ans:
Abstract class A{
Public void sum(int a ,Integer b)
{
Float f=a+b;
System.out.println(f);
}
}
Creating Exceptions of your own
public class MyException extends Exception{
My Exception(String S){
Super (s);/*calls constructor of base class
}
Assignment no 2
-
8/8/2019 UBJSTRAINING
34/57
33
Q1. Create static block example.
Q2.Create new exception.
Q3.Try implementing
Integer i=null;
Integer j=10;
System.out.println(i+j);
Q4.Try implementing constructor calling based on following:
Q5.Implement the following code and note the error:
Public class A{
Public void int1(int a,Integer b)
Float f=a+b;
System.out.Println(f);
}
Q6. Create an exception try passing a String
-
8/8/2019 UBJSTRAINING
35/57
34
Q7.Try Finally without catch in a try block
Q8.Try File Reader example.
Solution to the assignment is as follows:(as implemented by me UDDALAK
BANERJEE EMP NO 60527)
-
8/8/2019 UBJSTRAINING
36/57
35
PACKAGES:
-
8/8/2019 UBJSTRAINING
37/57
36
Throws
Catch(IOException e){
Throw newMyexception(Hello);
/*creates new instance of our MyException class*/
}
Throw blocks are written within catch block.
For every new Exception there has to be a try catch block .
Public int sum(int a,int b){
Return a+b;
}
Public int div(int a,int b) throws Exception
{
Return a/b;/*can throw exception so it has to be accompanied by a try catch
exception*/
-
8/8/2019 UBJSTRAINING
38/57
37
}
*Throws keyword is used to delegate responsibility of catching the
exception to calling code.
*A TRY BLOCK CAN HAVE MULTIPLE CATCH BLOCKS .
HIERARCHY IS LOWEST TO UPPER (Eg.we start with the lowest exception like
divide by zero exception then follow it up with its bigger parent exception
Arithmetic Exception and Then Exception .)
FINALLY
Syntax:
Try{
}
Catch(Exception e)
{
}
finally(){
-
8/8/2019 UBJSTRAINING
39/57
38
Saturday, 11 September 2010
/*this block is always executed irrespective of whether catch exceptions
match or not*/
Garbage collection
By initialising Null we make an object eligible for garbage collection.
Finalize{
// called when object is ready for garbage collection
//it may or may not run immediately.
}
Use(finally)
1. To release handle after usage(Deadlock prevention)2. Operations which must be executed always.
IO
Standard Outputs:
Console
Printer device
File
Database
StandardInputs:
mouse
keyboard
scanner
**Object is foremost parent of any class in Java
//20.08.2010
Nested Classes
Public Outerclass{
Class SuperClass{
}
}
-
8/8/2019 UBJSTRAINING
40/57
39
Incase of methods if a class is created within a method then the scope
of the class is limited to that method.
Class Outerclass{
Class Superclass{
}
Static class Test{
Int i;}}
Way to access
System.out.println(Outerclass.Test.i);
Anonymous Inner Class
Anonymous class scope is in method itself.
It is not mandatory to create interface to use anonymous classes.We
can create base classes and extend it so as to use anonymous classes.
-
8/8/2019 UBJSTRAINING
41/57
40
List1.ArrayList
2.LinkedList
3.Vector
Map
-
8/8/2019 UBJSTRAINING
42/57
41
Saturday, 11 September 2010
1.HashMap
2.HashTable
Advantages of Linked List
1.Random memory allocation can be done by Linked List2. Modification is fast
DisAdvantages(LinkedList)
1.Traversal is slow
Synchronized method
One process can access a resource then simultaneously another
process cannot access the same resource.
Note:Synchronized method should be small block of code
Synchronize{ }
Points to note:
#HashTable is synchronized but HashMap is not.
#In a Map of values are stored in the form of ArrayList then one key
value may point to multiple values.
#Vectors are no more used .Preferably ArrayList and LinkedLists are
used.
Assignments :
1. Create List of 10 valuesRemove 5
thelement and 8
thelement
Add new value 5th
and change 10th
element
2.Try Employee Example
3.Create a class which has certain parameters:
Int day;----------------------------------------display Topics(day);
String Training Name;----------------------display Training(day);
List Topics;-------------------------------------add Training(); removeTraining();
-
8/8/2019 UBJSTRAINING
43/57
42
The answers to the assignment as executed by uddalak banerjee
empid=60527 ,grade=a0,date of joining =5th
aug, 2010
Ans 1:
Ans 2:
-
8/8/2019 UBJSTRAINING
44/57
43
Ans 3:
-
8/8/2019 UBJSTRAINING
45/57
44
TOMCAT CONFIG
Bin/startup.bat/CreateShortcut
Now go to Start>Run>cmd
-
8/8/2019 UBJSTRAINING
46/57
45
And then repeat the following steps (in the present case tomcat 6 has
been installed in d drive within ubj folder .SO accordingly the path is
traced as follows:
Set Path
Once we had tried out all the steps we find the error message as
displayed.Neither the JAVA_HOME .....(as displayed).
Rightclick on Mycomputer and go to System properties tab
-
8/8/2019 UBJSTRAINING
47/57
46
Click on environment variables\Add new Variable\JAVA_HOME
Set the path of jdk as installed in the computer there as value.
In this case the path of jdk installed in my computer was ---
C:\Program Files\Java\jdk1.6.0_03
-
8/8/2019 UBJSTRAINING
48/57
47
WEB APPLICATION STRUCTURE:
-
8/8/2019 UBJSTRAINING
49/57
48
STEPS REQUIRED TO CREATE WEB PROJECTS
File\New\Other\Web\Dynamic Web Project\Proj Name
HOW TO CREATE WAR FILE:
WHICH IN THIS CASE IS TRAININGAPPLICATION and a default tag which indicates the execution of which part of the code
The projectname.war file is to be created.Base directory indicates the directory wherethe project is present .Similarly all the subdirectories necessary for the execution for the
project are provided with corresponding paths in the form of tag.=======================================================================
-->
-
-
8/8/2019 UBJSTRAINING
50/57
49
Saturday, 11 September 2010
activity that is performed during creation of a .war file is that all previous instances ofbuild and dist directories for the project is deleted.
-- >
-
- - -
-
-
-
-
-
-
8/8/2019 UBJSTRAINING
51/57
50
Saturday, 11 September 2010
===================================================================
-->
--
-
-
-
-
-
-
-
8/8/2019 UBJSTRAINING
52/57
51
SERVLET
Servlet is used to trigger different classes.Jsp is used to mainly to
create View/Presentation Layer.
METHODS OF SIGNIFICANCE WHILE IMPLEMENTING A SERVLET:
SendErrordoPut
doDelete
forward
include
sendRedirect
Other important topics to be covered are
printContent
Contenttype
Creation of a web project is demonstrated as below:
Step 1:
First a project is created by File/new/Others/Dynamic Web Project.
Step 2:
-
8/8/2019 UBJSTRAINING
53/57
52
Then a project name is given to the project which in our instance is
uddalakweb
Press NEXT
Step 3:
Select the default output folder as build1\classes.Press Finish.
Step 4:
Create a folder named classes under the WebContent\WEB_INF.
Step 5:
Create folders named build and dist under the uddalakweb
project.
Note:build and dist folders are created for the purpose of .war file
creation.
-
8/8/2019 UBJSTRAINING
54/57
53
Step 6:
Create a Jsp folder under the WebContent folder.
Note:This folder is used to store the jsp files.
Step 7:
Create a servlet by src/CreateServlet/package/Servlet
-
8/8/2019 UBJSTRAINING
55/57
54
The new servlet looks like:
Step 8:
-
8/8/2019 UBJSTRAINING
56/57
55
Copy servlet-api.jar from tomcat6/lib folder and paste it in
WEB_INF/lib
Step 9:
Make the build.xml file as already prescribed in .war creation.Deploy it
using ant build.
-
8/8/2019 UBJSTRAINING
57/57
56
Step 10:
Execute the application and use web browser to access the servlet.
NOTE:Everytime on producing any change in the application .Build theapplication again and remove the old .war file manually .