Lab Manual Java Civil
Lab Manual Java Civil
DEPARTMENT OF INFORMATION
TECHNOLOGY
LAB MANUAL
Programme
: BE
Semester
:III
Course Code
: Civil-305
Subject Name
: Computer programming(Java)
Prepared By:
Approved By:
Index
S.No.
Contents
1.
Syllabus
2.
3.
Viva Questions
4.
Grading Sheet
Page No.
Syllabus
CE- 306 Computer Programming
UNIT-I
Basic Java Features - C++ Vs JAVA, JAVA virtual machine, Constant & Variables, Data
Types, Class, Methods, Objects, Strings and Arrays, Type Casting, Operators, Precedence
relations, Control Statements, Exception Handling, File and Streams, Visibility, Constructors,
Operator and Methods Overloading, Static Members, Inheritance: Polymorphism, Abstract
methods and Classes
UNITII
Java Collective Frame Work - Data Structures: Introduction, Type-Wrapper Classes for
Primitive Types, Dynamic Memory Allocation, Linked List, Stack, Queues, Trees,
Generics: Introduction, Overloading Generic Methods, Generic Classes, Collections: Interface
Collection and Class Collections, Lists, Array List and Iterator, Linked List, Vector.
Collections Algorithms: Algorithm sorts, Algorithm shuffle, Algorithms reverse, fill, copy, max
and min Algorithm binary Search, Algorithms add All, Stack Class of Package java. Util, Class
Priority Queue and Interface Queue, Maps, Properties Class, Un-modifiable Collections.
UNITIII
Advance Java Features - Multithreading: Thread States, Priorities and Thread
Scheduling, Life Cycle of a Thread, Thread Synchronization, Creating and Executing Threads,
Multithreading with GUI, Monitors and Monitor Locks. Networking: Manipulating URLs, Reading
a file on a Web Server, Socket programming, Security and the Network, RMI, Networking,
Accessing Databases with JDBC: Relational Database, SQL, MySQL, Oracle
UNITIV
Advance Java Technologies - Servlets: Overview and Architecture, Setting Up the
Apache Tomcat Server, Handling HTTP get Requests, Deploying a web Application, Multitier
Applications, Using JDBC from a Servlet, Java Server Pages (JSP): Overview, First JSP
Example, Implicit Objects, Scripting, Standard Actions, Directives, Multimedia: Applets and
Application: Loading, Displaying and Scaling Images, Animating a Series of Images, Loading
and playing Audio clips
UNITV
Advance Web/Internet Programming (Overview): J2ME, J2EE, EJB, XML.
References books
1. Deitel & Deitel, JAVA, How to Program; PHI, Pearson.
2. E. Balaguruswamy, Programming In Java; TMH Publications
3. The Complete Reference: Herbert Schildt, TMH
4. Peter Norton, Peter Norton Guide To Java Programming, Techmedia.
5. Merlin Hughes, et al; Java Network Programming , Manning Publications/Prentice Hall
1.Write the importance of object oriented programming. Mention the features of JAVA
Java is a programming language created by James Gosling from Sun Microsystems (Sun) in
1991. The first publicly available version of Java (Java 1.0) was released in 1995.
Sun Microsystems was acquired by the Oracle Corporation in 2010. Oracle has now the
steermanship for Java.
Over time new enhanced versions of Java have been released. The current version of Java is
Java 1.7 which is also known as Java 7.
From the Java programming language the Java platform evolved. The Java platform allows
software developers to write program code in other languages than the Java programming
language which still runs on the Java virtual machine. The Java platform is usually
associated with the Java virtual machine and the Java core libraries.
Java has the following properties:
Platform independent: Java programs use the Java virtual machine as abstraction and
do not access the operating system directly. This makes Java programs highly
portable. A Java program (which is standard complaint and follows certain rules) can
run unmodified on all supported platforms, e.g., Windows or Linux.
Object-orientated programming language: Except the primitive data types, all
elements in Java are objects.
Interpreted and compiled language: Java source code is transferred into the bytecode
format which does not depend on the target platform. These bytecode instructions will
be interpreted by the Java Virtual machine (JVM). The JVM contains a so called
Hotspot-Compiler which translates performance critical bytecode instructions into
native code instructions.
Automatic memory management: Java manages the memory allocation and deallocation for creating new objects. The program does not have direct access to the
memory. The so-called garbage collector automatically deletes objects to which no
active pointer exists.
//Long to String
String s=Long.toString(longvalue);
//Float to String
String s=Float.toString(floatvalue);
//String to Integer
String s=7;
int i=Integer.valueOf(s).intValue();
(or)
int i = Integer.parseInt(s);
//String to Double
double
a=Double.valueOf(s).doubleValue();
//String to Long
long lng=Long.valueOf(s).longValue();
(or)
long lng=Long.parseLong(s);
//String to Float
float f=Float.valueOf(s).floatValue();
(or)
String s=+a;
//Character to Integer
char c=9;
int i=(char)c;
//Double to String
String s=Double.toString(doublevalue);
//String to Character
String s=welcome;
char c=(char)s;
String mainSubject;
int salary;
String type; // Primary or Secondary School
teacher
Teacher(String fName, String lName, String
sub, int slry, String sType)
{
super(fName,lName);
mainSubject = sub;
salary = slry;
type = sType;
}
void Display()
{
super.Display();
System.out.println("Main Subject : " +
mainSubject);
System.out.println("Salary : " + salary);
System.out.println("Type : " + type);
}}
class InheritanceDemo
{
public static void main(String args[])
{
Person pObj = new
Person("Rayan","Miller");
Student sObj = new
Student("Jacob","Smith",1,"1 - B","Roma");
Teacher tObj = new
Teacher("Daniel","Martin","English","6000","Prim
ary Teacher");
System.out.println("Person :");
pObj.Display();
System.out.println("");
System.out.println("Student :");
sObj.Display();
System.out.println("");
System.out.println("Teacher :");
tObj.Display();
}
}
8. Polymorpism In Java
class overLoading {
public static void main(String[] args)
{
functionOverload obj = new functionOverload();
obj.add(1,2);
obj.add(\"Life at \", \"?\");
obj.add(11.5, 22.5);
}
}
class functionOverload {
/*
* void add(int a, int b) // 1 - A method with two parameters {
*
* int sum = a + b; System.out.println(\"Sum of a+b is \"+sum);
*
*}
*/
void add(int a, int b, int c)
int sum = a + b + c;
System.out.println(\"Sum of a+b+c is \"+sum);
}
void add(double a, double b)
double sum = a + b;
System.out.println(\"Sum of a+b is \"+sum);
}
void add(String s1, String s2)
{
String s = s1 + s2;
System.out.println(s);
}
}
10.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcConnection {
public static void main(String a[]){
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.
getConnection("jdbc:oracle:thin:@<hostname>:<port num>:<DB name>"
,"user","password");
Statement stmt = con.createStatement();
System.out.println("Created DB Connection....");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Introduction: Applet is java program that can be embedded into HTML pages. Java applets runs on the java
enables web browsers such as mozila and internet explorer. Applet is designed to run remotely on the client
browser, so there are some restrictions on it. Applet can't access system resources on the local computer.
Applets
are
used
to
make
the
web
site
more
dynamic
and
entertaining.
Advantages of Applet:
Applets are cross platform and can run on Windows, Mac OS and Linux platform
Applets can work all the version of Java Plugin
Applets runs in a sandbox, so the user does not need to trust the code, so it can work without security
approval
Applets are supported by most web browsers
Applets are cached in most web browsers, so will be quick to load when returning to a web page User
can also have full access to the machine if user allows
Disadvantages of Java Applet:
Life cycle of applet is governed by browser. There are 5 methods in the life cycle of an applet All these
methods are optional
init( ) : Called when the applet is first created, to perform first-time
initialization of the applet
start( ): Called every time the applet moves into sight on the Web browser, to allow the applet to start up its
normal operations (especially those that are shut off by stop( )). Also called after init( ).
paint( ) : Part of the base class Component (three levels of inheritance up). Called as part of an update( ) to
perform special painting on the canvas of an applet.
stop( ) : Called every time the applet moves out of sight on the Web browser, to allow the applet to shut off
expensive operations. Also called right before destroy( ).
destroy( ): Called when the applet is being unloaded from the page, to perform final release of resources
when the applet is no longer used
Detailed Explanation:
init () method: The life cycle of an applet is begin on that time when the applet is first loaded into the
browser and called the init() method. The init() method is called only one time in the life cycle on an applet.
The init() method is basically called to read the PARAM tag in the html file. The init () method retrieve the
passed parameter through the PARAM tag of html file using get Parameter() method All the initialization such
as initialization of variables and the objects like image, sound file are loaded in the init () method .After the
initialization of the init() method user can interact with the Applet and mostly applet contains the init()
method.
Start () method: The start method of an applet is called after the initialization method init(). This method
may be called multiples time when the Applet needs to be started or restarted. For Example if the user wants
to return to the Applet, in this situation the start Method() of an Applet will be called by the web browser and
the user will be back on the applet. In the start method user can interact within the applet.
Stop () method: The stop() method can be called multiple times in the life cycle of applet like the start ()
method. Or should be called at least one time. There is only miner difference between the start() method and
stop () method. For example the stop() method is called by the web browser on that time When the user leaves
one applet to go another applet and the start() method is called on that time when the user wants to go back
into the first program or Applet.
destroy() method: The destroy() method is called only one time in the life cycle of Applet like init() method.
This method is called only on that time when the browser needs to Shut down.
Run this applet and view the messages in the console window.
import java.awt.*;
import java.applet.Applet;
public class LifeCycle extends Applet
{
public void init()
{
showStatus("This is Init");
for(int i=0;i<100000000;i++);
}
public void start()
{
showStatus("This is Start");
for(int i=0;i<100000000;i++);
}
public void paint(Graphics g)
{
showStatus("This is Paint");
for(int i=0;i<1000000;i++);
}
public void stop()
{
showStatus("This is Stop");
for(int i=0;i<1000000;i++);
}
public void destroy()
{
showStatus("This is Destroy");
for(int i=0;i<1000000;i++);
}
}
/*
VIVA QUESTIONS
A: Object is a runtime entity and its state is stored in fields and behavior is shown via methods. Methods
operate on an object's internal state and serve as the primary mechanism for object-to-object communication.
Q:11 Define class?
A: A class is a blue print from which individual objects are created. A class can contain fields and methods to
describe the behavior of an object.
Q:12 What kind of variables a class can consist of?
A: A class consist of Local variable, instance variables and class variables.
Q: 13 What is a Local Variable
A: Variables defined inside methods, constructors or blocks are called local variables. The variable will be
declared and initialized within the method and it will be destroyed when the method has completed.
Q:14 What is a Instance Variable
A: Instance variables are variables within a class but outside any method. These variables are instantiated
when the class is loaded.
Q: 15 What is a Class Variable
A: These are variables declared with in a class, outside any method, with the static keyword.
Q:16 What is Singleton class?
A: Singleton class control object creation, limiting the number to one but allowing the flexibility to create
more objects if the situation changes.
Q:17 What do you mean by Constructor?
A: Constructor gets invoked when a new object is created. Every class has a constructor. If we do not
explicitly write a constructor for a class the java compiler builds a default constructor for that class.
Q: 18List the three steps for creating an Object for a class?
A: An Object is first declared, then instantiated and then it is initialized.
Q: 19What is the default value of byte datatype in Java?
A: Default value of byte datatype is 0.
Q: 20 What is the default value of float and double datatype in Java?
A: Default value of float and double datatype in different as compared to C/C++. For float its 0.0f and for
double its 0.0d.
Q: 21 How finally used under Exception Handling?
A: The finally keyword is used to create a block of code that follows a try block. A finally block of code
always executes, whether or not an exception has occurred.
Q:22 What things should be kept in mind while creating your own exceptions in Java?
A: While creating your own exception:
You want to write a runtime exception, you need to extend the RuntimeException class.
A: JAR files is Java Archive fles and it aggregates many files into one. It holds Java classes in a library. JAR
files are built on ZIP file format and have .jar file extension.
Q:39 What is a WAR file?
A: This is Web Archive File and used to store XML, java classes, and JavaServer pages. which is used to
distribute a collection of JavaServer Pages, Java Servlets, Java classes, XML files, static Web pages etc.
Q:40 Define JIT compiler?
A: It improves the runtime performance of computer programs based on bytecode.