UBJSTRAINING
UBJSTRAINING
Java Training
CONTENTS
1.Software Requirements
6.Access Modifiers
7.Methods
9.Polymorphism concepts
11 Inheritance
12 Composition
13.Control Statements
14.Wrapper Classes
17.File handling.
18.Collection(ArrayList,LinkedList,HashMap)
19.Nested Classes
1.Software Requirements
Softwares to install:
1.eclipse-jee-galileo-SR2-win32
2. jdk-6u3-windows-i586-p
Step 1:
File/New/Other/Java/JavaProject/Next/ProjectName(java training)
5
Step 2:
PRESS Next button and set path for src files(Eg javatraining/src)
Step 3:
6
Libraries/jreSystemLibrary[javaSE 1.6]
Add Library/JreSystemLibrary
Create a file
Src/New/File/Filename
Java source files and bin contains class files generated by the
compiler on compilation of the source files.
F3---------Open declaration
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.
Properties of Class/Object
1.Filename ------------Mandatory
2.Access Modifier-----Mandatory
3.Package
4.Methods
14
5.Classname------------Mandatory
Important
CHECK POINTS:
Defination of Methods:
Example:
Access Modifiers
2.Private(Only by you)
4.Default(Concept of package)
COMPILATION
Javac <*.java>
Options
EXECUTION
Java *.class
a.Library
c.package
Step 2:Design
Step 3:Coding
Step 4:Testing
CHECK POINT:
IF A VARIABLE IS DECLARED FINAL THEN WILL IT BE AUTOMATICALLY
STATIC?
HERE
18
HER
Example:-
Class father{
Public House;
Protected V1;
}
}
-Son1 can access protected variable V1.
19
Cases:
Son 1 extends Father is Right
Son 2 extends Father is Right
Cases:
Son1 extends Father is wrong
Son 2 extends Son1 is wrong
Overloading
It is the process of using the same function name with variable number
of parameters .
20
Interfaces
Properties of interfaces are as follows:
Key points:
1.Interfaces extend other interfaces
2.classes extend classes
22
Examples:
Binding
1.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
23
1. /* */
2. /** **/
3. //
Compositions
ASSIGNMENT
1.Write a Calculator Program
Sum()
Sub()
Mult()
Div()
24
Use Extends
Try Debugging
Note Error
Solution to the above assignment as executed by uddalak banerjee
,Employee id=60527 ,grade =”A0” ,date of joining 5th aug ,2010 is as
follows:
25
OUTPUT DISPLAYED:
//18.08.2010
Control Statements
1 .if else
26
2 switch
3.while
4. do while
5 for
Operators:
Unary: Eg ++,--
Binary:Eg +,-,*,\
27
Ternary:?
ABSTRACT
abstract class a{
public int sum(int a,int b);
}
Data Types
Int
Float
Double
Char
Long
29
Boolean
Byte
String
Wrapper Classes
Data Transfer takes place as objects only.
Integer iobj=Integer.parseInt(INUM);
Autoboxing
It was used to reduce overhead involved in type conversion.
30
[Available in java 5]
TRY-CATCH
Q.Can Main method be used in another class than abstract class when it is
already declared public?
Class Example:
19.08.2010
32
Q.identify errors :
Abstract class A{
Float f=a+b;
System.out.println(f);
Ans:
Abstract class A{
Float f=a+b;
System.out.println(f);
My Exception(String S){
Assignment no 2
Q3.Try implementing
Integer i=null;
Integer j=10;
System.out.println(i+j);
Public class A{
Float f=a+b;
System.out.Println(f);
PACKAGES:
36
Throws
Catch(IOException e){
Throw newMyexception(“Hello”);
Return a+b;
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(){
38
Garbage collection
Finalize{
Use(finally)
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{
}
}
Class Outerclass{
Class Superclass{
}
Static class Test{
Int i;}}
Way to access
System.out.println(Outerclass.Test.i);
List
1.ArrayList
2.LinkedList
3.Vector
Map
41
1.HashMap
2.HashTable
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 values
Ans 1:
Ans 2:
43
Ans 3:
44
TOMCAT CONFIG
Bin/startup.bat/CreateShortcut
Now go to Start>Run>cmd
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).
<!—target tag is used to define the various activities that take place subsequently.The first
activity that is performed during creation of a .war file is that all previous instances of
build and dist directories for the project is deleted.
-- >
- <target name="clean">
<delete dir="dist" />
<delete dir="build" />
</target>
<!--
===================================================================
-->
-<!--
The Classpath needs to be set for the build file.This is done be
specifying the fileset tag .jar files were also included by the include tag
to the fileset.
--> - <!--
===================================================================
--> - <path id="build.classpath">
- <fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</path>
- <!--
===================================================================
-->
- <!--
Prepares the build directory .depends=”clean” implies that the prepare
block is to be executed only when clean block has been executed properly
.In the prepare block we created the file structure necessary for a web
project .A web project essentially contains a lib file ,classes file and
the web.xml file under the WEBINF folder which had been already discussed
previously in the structuring of web projects section.So in this section we
prepare the directory structure by making directories through the mkdir tag
and specifying the location where it is to be created.
-->
- <!--
===================================================================
-->
- <target name="prepare" depends="clean">
<mkdir dir="dist" />
<mkdir dir="${build.dir}/WEB-INF/lib" />
<mkdir dir="${build.dir}/WEB-INF/classes" />
<mkdir dir="${build.dir}/jsp" />
</target>
<!--
===================================================================
-->
<!--
Compiles the source code to build directory.Compile block depends on
prepare blocks execution.So only when the prepare block has been correctly
executed the compile block is to be executed.Compilation of the file
requires the java compiler(javac) which again requires a source and
destination directories which are specified by srcdir and destdir
-->
<!--
===================================================================
-->
- <target name="compile" depends="prepare" description="Compiles the Service class
files">
- <javac srcdir="${src.dir}" destdir="${build.dir}/WEB-INF/classes" debug="true"
deprecation="${deprecation}" optimize="${optimize}">
<classpath refid="build.classpath" />
</javac>
- <copy todir="${build.dir}/WEB-INF/lib" includeemptydirs="yes" overwrite="yes">
<fileset dir="${lib.dir}" />
</copy>
- <copy todir="${build.dir}/jsp" includeemptydirs="yes" overwrite="yes">
<fileset dir="${basedir}\WebContent\jsp" />
</copy>
<copy file="${webinf.dir}\web.xml" todir="${build.dir}\WEB-INF" />
</target>
- <!--
===================================================================
-->
- <!--
Prepares war .prepare war block of code is executed only when compile block
has been correctly executed(Hence we used depends=”compile”).War files base
directory is the current build directory which we just created and the
destination directory is the dist.dir which we had just created and this is
the place where the .war is to be placed after it is created.Web xml
reference is the position of the file web.xml which is needed to specify
the components of the project.
--> <!--
===================================================================
-->
- <target name="prepare-war" depends="compile" description="Creates the WAR
File">
<war basedir="${build.dir}/" destfile="${dist.dir}/${app.name}.war"
webxml="${webinf.dir}/web.xml" />
</target>
<!--
===================================================================
-->
<!--
Deploys to Tomcat Server.The deploy block is dependent on the prepare war b
lock of code.To execute the deploy block the correct execution of prepare
war block is necessary.In this block we copy the .war file just created in
the dist directory to the tomcat.deploy directory.
-->
<!--
===================================================================
-->
- <target name="deploy" depends="prepare-war" description="Deploys to Tomcat
Server">
<copy file="${dist.dir}\${app.name}.war" todir="${tomcat.deploy.dir}" />
</target>
</project>
SERVLET
Servlet is used to trigger different classes.Jsp is used to mainly to
create View/Presentation Layer.
Step 1:
First a project is created by File/new/Others/Dynamic Web Project.
Step 2:
52
Press NEXT
Step 3:
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.
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
54
Step 8:
55
Step 9:
Make the build.xml file as already prescribed in .war creation.Deploy it
using ant build.
56
Step 10:
Execute the application and use web browser to access the servlet.