GYAN GANGA COLLEGE OF TECHNOLOGY, JABALPUR
COMPUTER SCIENCE & ENGINEERING DEPARTMENT
THIRD SEMESTER
CS306
COMPUTER WORKSHOP
(JAVA TECHNOLOGIES)
LAB MANUAL
List of experiments of JAVA programming
1. Write a program to print “Hello World”.
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 Type Casting in JAVA
5. Write a program to show How Exception Handling is in JAVA
6. Write a Program to show Inheritance
7. Write a program to show Polymorphism
8. Write a program to show Access Specifies (Public, Private, Protected) in JAVA
9. Write a program to show use and Advantages of CONTRUCTOR
10. Write a program to show Interfacing between two classes
11. write a program to demonstrate concept of Thread / Multithreading .
12. Write a program to demonstrate copy constructor in JAVA.
1. Write a program to print “Hello World”.
Java is an object-oriented programming language with its own runtime environment. Java was developed by
Sun Microsystems and released in 1995. java code that runs on one platform does not need to be recompiled to
run on another platform, it’s called write once, run anywhere(WORA).
The Java Virtual Machine is called JVM, is an abstract computing machine or virtual machine interface that
drives the java code. It is the medium which compile Java code to bytecode which get interpret on different
machine and hence it makes it Platform/Operating system independent.
JRE stands for Java Runtime Environment which is used to provide an environment at run time. It contains set
of supporting libraries in combination with core classes and various other files that are used by JVM at run
time.
public class HelloWorld
public static void main(String[] args)
[Link]("Hello World");
}//End of main
}//End of HelloWorld Class
Steps for Saving, compiling and Running a Java
Step1: Save the program With .java Extension.
Step2: Compile the file from DOS prompt by typing javac <filename>.
Step3: Successful Compilation, results in creation of .class containing byte code
Step4 : Execute the file by typing java <filename without extension>
OUTPUT: -
C:/> javac HelloWorld .java
C:/> java HelloWorld
HelloWorld
Viva question :-
What do you mean by byte code.
Meaning of public static void main(String args[])
Meaning of [Link]().
2. Write a program to show Scope of Variables.
The scope of a variable is the part of the program over which the variable name can be referenced. You cannot
refer to a variable before its declaration. You can declare variables in several different places:
• In a class body as class fields. Variables declared here are referred to as class-level variables.
• As parameters of a method or constructor.
• In a method's body or a constructor's body.
• Within a statement block, such as inside a while or for block.
public class VarScopeProg
public static void main(String[] args)
int A=10;
int B=20;
[Link]("Block A: "+A);
[Link]("Block B: "+B);
}//scope of B=20 ends here
int B=30;//scope of B=30 starts here
[Link]("Block A: "+A);
[Link]("Block B: "+B);
}//scope of B=30 ends here
int B=4 0;//scope of B=4 0 starts here
[Link]("Block A: "+A);
[Link]("Block B: "+B);
}
}//scope of all the variables end here
OUTPUT:-
Block A: 10
Block B: 20
Block A: 10
Block B: 30
Block A: 10
Block B: 4 0
Viva question :-
What are global variables?
What are local variables?
3. Write a program to show Concept of CLASS in JAVA
A class is nothing but a blueprint or a template for creating different objects which defines its properties and
behaviors. Java class objects exhibit the properties and behaviors defined by its class. A class can contain fields
and methods to describe the behavior of an object. Methods are nothing but members of a class that provide
a service for an object or perform some business logic. Java fields and member functions names are case
sensitive. Current states of a class’s corresponding object are stored in the object’s instance variables.
Methods define the operations that can be performed in java programming.
class Rectangle
int height;
int width;
void area()
int result=height*width;
[Link](“The area is ”+result);
class classDemo
public static void main(String args[])
Rectangle obj=new Rectangle();
[Link]=10;//setting the attribute values
[Link]=20;//from outside of class
[Link]();//using object method of class is called
OUTPUT:-
The area is 200
Viva question:-
Define class.
What is use of new operator?
What do you mean by Object?
4 . Write a program to show Type Casting in JAVA
W hen you assign value of one data type to another, the tw o types might not be compatible w ith
each other. If the data types are compatible, then Java w ill perform the conversion automatically
know n as Automatic Type Conversion and if not then they need to be casted or converted
explicitly. For example, assigning an int value to a long variable.
W idening or Automatic Type Conversion
W idening conversion takes place w hen tw o data types are automatically converted. This happens
w hen:
The tw o data types are compatible.
W hen w e assign value of a smaller data type to a bigger data type.
For Example, in java the numeric data types are compatible w ith each other but no automatic
conversion is supported from numeric type to char or boolean. Also, char and boolean are not
compatible w ith each other.
public class TypeCastProg
public static void main(String[] args)
double x;
int y;
x=25.50;
[Link]("Value of [double] x: "+x);
[Link]("Conversion of double to integer");
y=(int)x;
[Link]("Value of [integer] y: "+y);
int m;
double n;
m=10;
n=(double)m;
[Link]("Value of [integer] m: "+m);
[Link]("Conversion of integer to double");
[Link]("Value of [double] n: "+n);
OUTPUT:-
Value of [double] x: 25.5
Conversion of double to integer
Value of [integer] y: 25
Value of [integer] m: 10
Conversion of integer to double
Value of [double] n: 10.0
Viva question:-
What do mean by implicit type casting?
What do mean by explicit type casting?
5. Write a program to show How Exception Handling is in JAVA
Exceptions in java are any abnormal, unexpected events or extraordinary conditions that may occur at runtime.
Java exception handling is used to handle error conditions in a program systematically by taking the necessary
action. Exception handlers can be written to catch a specific exception such as Number Format exception, or
an entire group of exceptions by using a generic exception handlers. Any exceptions not specifically handled
within a Java program are caught by the Java run time environment
class ExceptionDemo
public static void main(String args[])
try
{ int a,b;
a=5;
b=a/0;
catch (ArithmeticException e)
[Link]("Divide by Zero\n");
[Link]("...Executed catch statement");
}
}
OUTPUT:-
Divide by Zero
...Executed catch statement
Viva question:-
Define exception.
What do you mean by user defined exception?
What do you mean by system defined exception?
Use of try, catch and finally block in java program.
6. Write a Program to show Inheritance
Inheritance is one of the key features of OOP that allows us to create a new class from an existing class.
Inheritance in Java is a concept that acquires the properties from one class to other classes.
In Java, a class can inherit attributes and methods from another class. The class that inherits the
properties is known as the sub-class or the child class. The class from which the properties are inherited is
known as the superclass or the parent class.
In Inheritance, the properties of the base class are acquired by the derived classes.
class A
int a;
void set _a(int i)
a=i;
void show_a()
[Link]("The value of a= "+a);
class B extends A
{
int b;
void set _b(int i)
b=i;
void show_b()
[Link]("The value of b= "+b);
void mul()
int c;
c=a*b;
[Link](" The value of c= "+c);
class InheritDemo1
public static void main(String args[])
A obj_A=new A();
B obj_B=new B();
obj_B.set _a(10);
obj_B.set _b(20);
obj_B.show_a();
obj_B.show_b();
obj_B.mul();
}
OUTPUT:-
The value of a= 10
The value of b= 20
The value of c= 200
Viva question:-
Why java does not support multiple inheritances?
Use of final keyword?
What do you mean by abstract classes?
7. Write a program to show Polymorphism
Polymorphism in Java is a concept by which we can perform a single action in different ways. Polymorphism is
derived from 2 Greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So
polymorphism means many forms.
There are two types of polymorphism in Java: compile-time polymorphism and runtime polymorphism. We
can perform polymorphism in java by method overloading and method overriding.
Concept- 1 // program to demonstrate method overloading
class Shape
public void area() {
[Link]("Find area ");
public void area(int r) {
[Link]("Circle area = "+3.14 *r*r);
}
public void area(double b, double h) {
[Link]("Triangle area="+0.5*b*h);
public void area(int l, int b) {
[Link]("Rectangle area="+l*b);
public class polymorphismExample_2
public static void main(String[] args)
Shape myShape = new Shape(); // Create a Shapes object
[Link]();
[Link](5);
[Link](6.0,1.2);
[Link](6,2);
OUTPUT:-
Find area
Circle area = 78.5
Triangle area=3.5999999999999996
Rectangle area=12
Concept- 2 // program to demonstrate method overriding
class Bank{
int getRateOfInterest(){return 0;}
//Creating child classes.
class SBI extends Bank{
int getRateOfInterest(){return 8;}
class ICICI extends Bank{
int getRateOfInterest(){return 7;}
class AXIS extends Bank{
int getRateOfInterest(){return 9;}
//Test class to create objects and call the methods
public class polymorphismExample_4 {
public static void main(String args[]){
SBI s=new SBI();
ICICI i=new ICICI();
AXIS a=new AXIS();
[Link]("SBI Rate of Interest: "+[Link]());
[Link]("ICICI Rate of Interest: "+[Link]());
[Link]("AXIS Rate of Interest: "+[Link]());
OUTPUT:
SBI Rate of Interest: 8
ICICI Rate of Interest: 7
AXIS Rate of Interest: 9
Viva question:-
What do you mean by run-time polymorphism?
What do you mean by compile-time polymorphism
What do you mean by method-overloading?
What do you mean by method-overridden.
8. write a program to show access specifiers (public, private, protected) in java
Java provides a number of access modifiers to set access levels for classes, variables, methods,
and constructors. The four access levels are −
Visible to the package, the default. No modifiers are needed.
Visible to the class only (private).
Visible to the world (public).
Visible to the package and all subclasses (protected).
public : Public class is visible in other packages, field is visible everywhere (class must be public too)
private : Private variables or methods may be used only by an instance of the same class that declares the
variable or method, A private feature may only be accessed by the class that owns the feature.
protected : Is available to all classes in the same package and also available to all subclasses of the class that
owns the protected [Link] access is provided even to subclasses that reside in a different package from
the class that owns the protected feature.
default :What you get by default ie, without any access modifier (ie, public private or protected).It means that
it is visible to all within a particular package.
class class1
int a =10;
private int b=20;
public void show_a()
[Link]("public method show_a() from base class");
protected void show_b()
[Link]("public method show_b()from base class");
class Access_example extends class1
public void show_b()
[Link]("public method show_b() from drived class");
public static void main(String aaa[])
Access_example obj = new Access_example();
class1 c = new class1();
c.show_a();
c.show_b();
//c.b=50; //Private variable cannot be used
obj.show_b();
OUTPUT
public method show_a() from base class
public method show_b()from base class
public method show_b() from drived class
Viva question :-
What do you mean by access Specifiers.
Define private, protected, public and default Specifiers?
List out chart for various access Specifiers?
9. Write a program to show use and Advantages of CONTRUCTOR.
A constructor initializes an object w hen it is created. It has the same name as its class and is
syntactically similar to a method. How ever, constructors have no explicit return type.
Typically, you w ill use a constructor to give initial values to the instance variables defined by the
class, or to perform any other start-up procedures required to create a fully formed object.
All classes have constructors, w hether you define one or not, because Java automatically
provides a default constructor that initializes all member variables to zero. How ever, once you
define your ow n constructor, the default constructor is no longer used.
class Rectangle1
int height;
int width;
Rectangle1()
[Link](" Simple constructor: values are initialised...");
height=10;
width=20;
Rectangle1(int h,int w)
[Link](" Parameterised constructor: values are initialised...");
height=h;
width=w;
void area()
[Link]("Now, The function is called...");
int result=height*width;
[Link]("The area is "+result);
class Constr_Demo
public static void main(String args[])
Rectangle1obj=new Rectangle1();
[Link]();//call the to method
Rectangle1obj1=new Rectangle1(11,20);
[Link]();//call the to method
OUTPUT:-
Simple constructor: values are initialised...
Now, The function is called...
The area is 200
Parameterised constructor: values are initialised...
Now, The function is called...
The area is 220
Viva question:-
What do you mean by copy constructor?
What do you mean by default constructor?
What do you mean by parameterized constructor?
Difference between constructor and a method.
10. Write a program to show Interfacing between two classes
In the Java programming language, an interface is a reference type, similar to a class, that can contain only
constants, method signatures, default methods, static methods, and nested types. Method bodies exist only
for default methods and static methods. Interfaces cannot be instantiated— they can only be implemented by
classes or extended by other interfaces.
Important points about interface or summary of article:
We can’t create instance(interface can’t be instantiated) of interface but we can make reference of it
that refers to the Object of its implementing class.
A class can implement more than one interface.
An interface can extends another interface (but only one interface).
A class that implements interface must implements all the methods in interface.
All the methods are public and abstract. And all the fields are public, static, and final.
It is used to achieve multiple inheritance.
public interface My_interface
void my_method(int val);
class Abc implements My_interface
public void my_method(int i)
[Link]("\n This method from interface :"+i);
public void another_method() //Defining another method not declared in interface
[Link]("\nThis is another method form in class A");
public class InterfaceDemo
public static void main(String args[])
Abc obj1=new Abc();
obj1.my_method(100);
obj1.another_method();
OUTPUT :-
This method from interface :100
This is another method form in class A
Viva quastion:-
What is the process Inter class communication?
What do you mean by static class?
What do you mean by inner class?
11. write a program to demonstrate concept of Thread / Multithreading .
Threads allow a program to operate more efficiently by doing multiple things at the same time. Threads can be
used to perform complicated tasks in the background without interrupting the main program.
There are tw o w ays to create a thread:
1. By extending Thread class
2. By implementing Runnable interface
Concept-1By extending Thread class
public class MyThread1extends Thread
public static void main(String[] args)
MyThread1thread = new MyThread1();
[Link]();
[Link]("This code is outside of the thread");
public void run()
[Link]("This code is running in a thread");
OUTPUT-
This code is outside of the thread
This code is running in a thread
Concept-1By implementing Runnable interface
public class MyThread2 implements Runnable
public static void main(String[] args)
{
MyThread2 obj = new MyThread2();
Thread thread = new Thread(obj);
[Link]();
[Link]("This code is outside of the thread");
public void run() {
[Link]("This code is running in a thread");
OUTPUT-
This code is outside of the thread
This code is running in a thread
12. Write a program to demonstrate copy constructor in JAVA.
In Java, a copy constructor is a special type of constructor that creates an object using another object of the
same Java class. It returns a duplicate copy of an existing object of the class.
We can assign a value to the final field but the same cannot be done while using the clone() method. It is used
if we want to create a deep copy of an existing object. It is easier to implement in comparison to the clone()
method.
Use of Copy Constructor
We can use the copy constructor if we want to:
o Create a copy of an object that has multiple fields.
o Generate a deep copy of the heavy objects.
o Avoid the use of the [Link]() method.
Advantages of Copy Constructor
o If a field declared as final, the copy constructor can change it.
o There is no need for typecasting.
o Its use is easier if an object has several fields.
o Addition of field to the class is easy because of it. We need to change only in
the copy constructor.
class Student6
{
int id;
String name;
Student6(int i,String n)
id = i;
name = n;
Student6(Student6 s)
id = [Link];
name =[Link];
void display(){[Link](id+" "+name);}
public static void main(String args[])
Student6 s1= new Student6(111,"Karan");
Student6 s2 = new Student6(s1);
[Link]();
[Link]();
OUTPUT
111Karan
111Karan