Lab Manual: Java Lab

DEPARTMENT OF INFORMATION TECHNOLOGY

LAB MANUAL

Name:

Course Code: IT-506

Course: JAVA TECHNOLOGY

Session:

Prepared By

List of Programs

Experiment No. / Aim / Date of Submission / Signature & Remarks
1.  /

Installation of J2SDK

2.  / Write a program to show Scope of Variables.
3.  / Write a program to show Concept of CLASS in JAVA.
4.  /

Write a program to show How Exception Handling is in JAVA.

5.  / Write a program to show Polymorphism.
6.  / Write a program to show use and Advantages of CONTRUCTOR.
7.  / Write a program to show Interfacing between two classes.
8.  / Write a program to Add a Class to a Package.
9.  / Write a program to show Life Cycle of a Thread.
10.  / Write a program to demonstrate AWT.
11.  / Write a Program to show Data Base connectivity Using JAVA.
12.  / Write a Program to show “HELLO JAVA ” in Explorer using Applet
13.  / Write a Program to show Connectivity using JDBC.
14.  / Write a program to demonstrate multithreading using Java.
15.  / Write a program to demonstrate applet life cycle.

Experiment-1

1.  Objective: Installation of J2SDK.

2.  Algorithm:

Steps for Installation:

Download and install Java for Windows computer?

This article applies to:

·  Platform(s):Windows 8, Windows 7, Vista, Windows XP, Windows 2000, Windows 2003, Windows 2008 Server

·  Java version(s):6.0, 7.0

The procedure to install Java broadly consists of:

1.  Download and Install

2.  Test Installation

Windows System Requirements
Note: Installing Java requires that you can gain administrator access to Windows on your computer.

Download and Install

It is recommended; before you proceed with online installation you may want to disable your Internet firewall. In some cases the default firewall settings are set to reject all automatic or online installations such as the Java online installation. If the firewall is not configured appropriately it may stall the download/install operation of Java under certain conditions. Refer to your specific Internet firewall manual for instructions on how to disable your Internet Firewall.

·  Go to theManual downloadpage

·  Click onWindows Online

·  The File Download dialog box appears prompting you to run or save the download file

·  To run the installer, clickRun.

·  To save the file for later installation, clickSave.
Choose the folder location and save the file to your local system.
Tip: Save the file to a known location on your computer, for example, to your desktop.
Double-click on the saved file to start the installation process.

·  The installation process starts. Click theInstallbutton to accept the license terms and to continue with the installation.


·  Oracle has partnered with companies that offer various products. The installer may present you with option to install these programs when you install Java. After ensuring that the desired programs are selected, click theNextbutton to continue the installation.


·  A few brief dialogs confirm the last steps of the installation process; clickCloseon the last dialog. This will complete Java installation process.

3. Required Software/ Software Tool:-

·  Platform(s):Windows 8, Windows 7, Vista, Windows XP, Windows 2000,

Windows 2003, Windows 2008 Server

·  Java version(s):6.0, 7.0 (jdk1.6,jdk1.7)

·  Editor :- Net Beans IDE 6.9.1

·  Database:- Oracle9i / Mysql

·  Web server:- Apache Tomcat 5.5

Experiment-2

1. Objective: Write a program to show Scope of Variables.

2. Explanation:-

Types of Variable

There are three types of variables in java
·  local variable
·  instance variable
·  static variable
Local Variable: A variable that is declared inside the method is called local variable.
Instance Variable: A variable that is declared inside the class but outside the method is called instance variable. It is not declared as static.
Static variable: A variable that is declared as static is called static variable. It cannot be local.

3. Algorithm:

1.  Create a class A

2.  Declare instance variable and initialize it.

3.  Declare static variable and initialize it.

4.  Declare a local variable within a method.

5.  Print all the variables value.

4. Required Software/ Software Tool:-

·  Platform(s):Windows 8, Windows 7, Vista, Windows XP, Windows 2000, Windows 2003, Windows 2008 Server

·  Java version(s):6.0, 7.0 (jdk1.6,jdk1.7)

·  Editor :- Net Beans IDE 6.9.1

·  Database:- Oracle9i / Mysql

·  Web server:- Apache Tomcat 5.5

5. Coding:-

6.Output:-

Experiment-3

1. Objective: Write a program to show Concept of CLASS in JAVA.

2. Explanation-program to find sum of two numbers using class and object.

3. Algorithm:-

Step 1: Start

Step 2: Define a class sum

Step 3: Define methods getdata and sum

Step 4: Define clsmain

Step 5: Read the two numbers.

Step 6: Call getdata and sum

Step 7: Print the sum

Step 8: Stop

4. Required Software/ Software Tool:-

·  Platform(s):Windows 8, Windows 7, Vista, Windows XP, Windows 2000,

Windows 2003, Windows 2008 Server

·  Java version(s):6.0, 7.0 (jdk1.6,jdk1.7)

·  Editor :- Net Beans IDE 6.9.1

·  Database:- Oracle9i / Mysql

·  Web server:- Apache Tomcat 5.5

5. Coding:

6. Output:

Experiment-4

1. Objective: Write a program to show How Exception Handling is in JAVA.

2. Explanation:- The exception handling is one of the powerful mechanism provided in java. It provides the mechanism to handle the runtime errors so that normal flow of the application can be maintained. In this page, we will know about exception, its type and the difference between checked and unchecked exceptions.

Types of Exception:

There are mainly two types of exceptions: checked and unchecked where error is considered as unchecked exception. The sun micro system says there are three types of exceptions:
1.  Checked Exception
2.  Unchecked Exception
3.  Error

Five keywords used in Exception handling:

1.  try
2.  catch
3.  finally
4.  throw
5.  throws

3. Algorithm:-

1.  Create class Excep.

2.  create the main class

3.  Write the statement within the try block which can generate exception.

4.  Create the catch block to handle the exception and also pass the arguments for the Exception.

5.  Use finally at the last.

4. Required Software/ Software Tool:-

·  Platform(s):Windows 8, Windows 7, Vista, Windows XP, Windows 2000,

Windows 2003, Windows 2008 Server

·  Java version(s):6.0, 7.0 (jdk1.6,jdk1.7)

·  Editor :- Net Beans IDE 6.9.1

·  Database:- Oracle9i / Mysql

·  Web server:- Apache Tomcat 5.5

5. Coding:

6. Output:

Experiment-5

1.  Objective: Write a program to show Polymorphism.

2.  Explanation:- Runtime polymorphism or Dynamic Method Dispatch is a process in which a call to an overridden method is resolved at runtime rather than compile-time. In this process, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable. Let's first understand the upcasting before Runtime Polymorphism.

3.  Algorithm:

1. Start the program.

2. Create an abstract class with class name Shape.

3. Create a constructor with arguments and declare variables dim1, dim2 and PI.

4. Declare an abstract method area() inside the class.

5. Create the classes Rectangle, Triangle, Circle, and Ellipse to find the area.

6.  Define abstract method area() inside the subclasses and call the constructor of class Shape using super keyword.

7. In main(), create the objects for all classes and pass values to fields of constructors.

8. Create a reference variable figuref for abstract class.

9. Using reference variable of class Shape, call the method area() of all subclasses

10. Print the areas for all shapes.

11. Stop the program

4. Required Software/ Software Tool:-

·  Platform(s):Windows 8, Windows 7, Vista, Windows XP, Windows 2000,

Windows 2003, Windows 2008 Server

·  Java version(s):6.0, 7.0 (jdk1.6,jdk1.7)

·  Editor :- Net Beans IDE 6.9.1

·  Database:- Oracle9i / Mysql

·  Web server:- Apache Tomcat 5.5

5 Coding:

6.  OutPut:

Experiment - 6

1. Objective: - Write a program to show use and Advantages of CONTRUCTOR.

2. Explanation: - Constructor is a special type of method that is used to initialize the object. Constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor.

Rules for creating constructor

There are basically two rules defined for the constructor.

1.  Constructor name must be same as its class name

2.  Constructor must have no explicit return type

Types of constructors

There are two types of constructors:

1.  default constructor (no-arg constructor)

2.  parameterized constructor

3. Algorithm: -

1: Start the program.

2: Create a class with a class name Complex Number.

3: Create a constructor with the arguments a and b with integer data type.

4: Define a method getcomplexvalue( ) to get the values of a and b.

5: Define static method named as addition, subtraction, multiplication to perform

particular function defined inside the method.

6: Create an object for the class Complex Number an d pass the values in the

argument list.

7: Call the method by using object to get the values.

8: Print the result.

9: Stop the program.

4. Required Software/ Software Tool:-

·  Platform(s):Windows 8, Windows 7, Vista, Windows XP, Windows 2000,

Windows 2003, Windows 2008 Server

·  Java version(s):6.0, 7.0 (jdk1.6,jdk1.7)

·  Editor :- Net Beans IDE 6.9.1

·  Database:- Oracle9i / Mysql

·  Web server:- Apache Tomcat 5.5

5. Coding:-

6. Output:

Experiment - 7

1. Objective: Write a program to show Interfacing between two classes.

2. Explanation: An interface is a blueprint of a class. It has static constants and abstract methods. The interface is a mechanism to achieve fully abstraction in java. There can be only abstract methods in the interface. It is used to achieve fully abstraction and multiple inheritances in Java. Interface also represents IS-A relationship. It cannot be instantiated just like abstract class.

There are mainly three reasons to use interface. They are given below.

·  It is used to achieve fully abstraction.

·  By interface, we can support the functionality of multiple inheritance.

·  It can be used to achieve loose coupling.

3. Algorithm:-

1. Create an interface

2. Create class

3. Implements interface in class

4. Create main method

5. Use the methods of interface and implement it in class.

6. Create the reference of interface and initialize it with object of class.

7. Call the overridden methods in main method using reference.

4. Required Software/ Software Tool:-

·  Platform(s):Windows 8, Windows 7, Vista, Windows XP, Windows 2000, Windows 2003, Windows 2008 Server

·  Java version(s):6.0, 7.0 (jdk1.6,jdk1.7)

·  Editor :- Net Beans IDE 6.9.1

·  Database:- Oracle9i / Mysql

·  Web server:- Apache Tomcat 5.5

5. Coding:-

Output:

Experiment-8

1. Objective: Write a program to Add a Class to a Package.

2. Explanation: A package is a group of similar types of classes, interfaces and sub- packages. Package can be categorized in two form, built-in package and user-defined package. There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc. Here, we will have the detailed learning of creating and using user-defined packages.

Advantage of Package

Package is used to categorize the classes and interfaces so that they can be easily maintained.

·  Package provids access protection.

·  Package removes naming collision.

3. Algorithm:-

1. Package keyword is used to create a package.

2. Create class

3. Compile the Package (if not using IDE):-

If you are not using any IDE, you need to follow the syntax given below:

javac-ddirectoryjavafilename

For example

javac-d.Simple.java

The -d switch specifies the destination where to put the generated class file. You can use any directory name like /home (in case of Linux), d:/abc (in case of windows) etc. If you want to keep the package within the same directory, you can use. (dot).

3.  How to run the Package (if not using IDE)-:

You need to use fully qualified name e.g. mypack.Simple etc to run the class.

To Compile: javac -d . Simple.java
To Run: java mypack.Simple

Output: Welcome to package

The -d is a switch that tells the compiler where to put the class file i.e. it represents destination. The represents the current folder.

4. Access package from another package?

There are three ways to access the package from outside the package.
1.  import package.*;
2.  import package.classname;
3.  fully qualified name.

3. Required Software/ Software Tool:-

·  Platform(s):Windows 8, Windows 7, Vista, Windows XP, Windows 2000,Windows 2003, Windows 2008 Server

·  Java version(s):6.0, 7.0 (jdk1.6,jdk1.7)

·  Editor :- Net Beans IDE 6.9.1

·  Database:- Oracle9i / Mysql

·  Web server:- Apache Tomcat 5.5

5. Coding:-

6. Output:-

Experiment-9

1. Objective: Write a program to show Life Cycle of a Thread.

2. Explanation:-

Multithreading is a process of executing multiple threads simultaneously. Thread is basically a lightweight subprocess, a smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking. But we use multithreading than multiprocessing because threads share a common memory area. They don't allocate separate memory area so save memory, and context-switching between the threads takes less time than processes. Multithreading is mostly used in games, animation etc. A thread can be in one of the five states in the thread. According to sun, there is only 4 states new, runnable, non-runnable and terminated. There is no running state. But for better understanding the threads, we are explaining it in the 5 states. The life cycle of the thread is controlled by JVM. The thread states are as follows:
A thread can be in one of the five states in the thread. According to sun, there is only 4 states new, runnable, non-runnable and terminated. There is no running state. But for better understanding the threads, we are explaining it in the 5 states. The life cycle of the thread is controlled by JVM. The thread states are as follows:
1.  New
2.  Runnable
3.  Running
4.  Non-Runnable (Blocked)
5.  Terminated

3. Algorithm: