Java
Java
EXERCISE 1
AIM: A java program to display the default value of primitive data types of java.
DESCRIPTION:
It's not always necessary to assign a value when a field is declared. Fields that are
declared but not initialized will be set to a reasonable default by the compiler. Generally
speaking, this default will be zero or null, depending on the data type. Relying on such default
values, however, is generally considered bad programming style.
ALGORITHM:
1. Create a class EmployeeDetails
2. Create a method getEmployeeDetails()
Print all the initialized variables
end of method
3. Create a method getCollegeDetails()
Print some information
end of method
end of class
4. Create another class Employee
5. Create main method
Create instance for EmployeeDetails class
Using the instance call the above methods
end of class
OUTPUT:
E:\>javac EmployeeDetails.java
E:\>java EmployeeDetails
0
Null
0.0
False
CONCLUSION:
Getting default values is the way to understand memory allocation strategies and maps to
CO2 and attains with PO1, PO2, PO3, PO4, PO12, PSO1, PSO3.
VIVA QUESTIONS:
1. What are the primitive data types?
2. What are default values for different data types?
3. What are the non primitive data types?
4. Why char has 2bytes of storage?
5. What is Unicode?
EXERCISE 2
AIM: A java program that displays the roots of quadratic equation ax2+bx+c=0 and Calculating
the discriminate D to describe the nature of roots.
DESCRIPTION:
The Quadratic Formula. The quadratic equation
ALGORITHM:
OUTPUT:
E:\java 2>javac QuadraticImpl.java
E:\java 2>java QuadraticImpl
enter a,b,c values
121
Roots are equal
r1=r2=-1.0
CONCLUSION:
Understands the way to formulate Mathematical functions through programming and
maps to CO2 and attains with PO1, PO2, PO3, PO4, PO12, PSO1, PSO3.
.
VIVA QUESTIONS
EXERCISE 3
DESCRIPTION:
In mathematics, the Fibonacci numbers or Fibonacci series or Fibonacci sequence are the
numbers in the following integer sequence:
By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent
number is the sum of the previous two.
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence
relation
ALGORITHM:
1. Create a class Fib()
read n value
end of method
Repeat for i = 0 to n
temp = f1 + f2
print temp
f1 = f2
f2 = temp
end of method
end of class
end of class
OUTPUT:
E:\java>javac Fibonacci.java
E:\java>java Fibonacci
enter n
8
011 2 3 5 8 13
CONCLUSION:
Understands the way to use Recursion functions through programming and also
implement Stack Operations and maps to CO2 and attains with PO1,PO2,PO3,PO4, PO12,
PSO1, PSO3.
VIVA QUESTIONS
1. What is the Fibonacci Sequence?
2. What are the control statements in java ?
3. What is the Dynamic Initialization in java
4. Can we use recursion in java?
EXERCISE 4
AIM: Write a java program to give example for command line arguments
DESCRIPTION:
The java command-line argument is an argument i.e. passed at the time of running the java
program. The arguments passed from the console can be received in the java program and it can
be used as an input. Command line arguments represent the values passed to main() method.
Here, args[] is one dimensional array of String type. So it can store a group of strings, passed to
main() from outside by the user i.e, at the time of running the program.
ALGORITHM:
Repeat for I = 0 to n
Print sum
end method
end class
OUTPUT:
E:\java>javac Command.java
E:\java>java Command 20 45 37 19
20 45 37 19
the sum is:121
CONCLUSION:
Implements the usage of arguments passing into Program that renders student to
understand command line arguments and maps to CO2 and attains with PO1,PO2,PO3,PO4,
PO12, PSO1,PSO3.
.
VIVA QUESTIONS:
1. What is command line argument?
2. What is the prototype of main( ) when command line arguments are supported.?
3. Function to convert string to integer?
4. Default type of values which are reading through command line?
5. Can we run java program without String[] args why?
EXERCISE 5
AIM: Write a java program to sort given list of numbers
DESCRIPTION:
The algorithm works by comparing each item in the list with the item next to it, and
swapping them if required. In other words, the largest element has bubbled to the top of the
array. The algorithm repeats this process until it makes a pass all the way through the list without
swapping any items
ALGORITHM:
1. Create a class EmployeeData
2. Create a method setEmployee()
Print employee details
end of method
3. Create another method sortEmployee()
Sort and print the employee details based on their salaries
end of method
end of class
4. Create another class SortinngEmloyee
5. Create main method()
Create object for the above class
Call all the above methods using the object
end of method
end of class
OUTPUT:
E:\java>javac SortingEmployee.java
E:\java>java SortingEmployee
Enter details:
james e20 50000
Enter details:
peter e30 40000
Enter details:
gosling e22 25000
gosling
peter
james
CONCLUSION:
Student Better understand the way of sorting data with text processing into sorted records
and maps to CO2 and attains with PO1,PO2,PO3,PO4, PO12, PSO1,PSO3.
VIVA QUESTIONS:
EXERCISE 6
AIM: Write a java program to search for an element in a given list of elements (Linear Search)
DESCRIPTION:
Linear search is one of the basic search techniques that we've now. Although this is not a
very good search technique, one should understand this concept. Let's consider our aim to search
for a key element in an array of elements. We loop through all the array elements and check for
existence of the key element. Since we go element by element, this search is called as linear
search or sequential search. Search element is called as key element.
ALGORITHM:
1. Import package
3. declare the method and perform linear search in which key element is compared with each
element in the array
4. if the key element is found then print success otherwise print failure
5. end method
6. end class
9. end class
10. stop
OUTPUT:
E:\java>javac SearchEmployee.java
E:\java>java SearchEmployee
enter keyId
2 is present at 2 james
CONCLUSION:
Searching concept mitigates for student to get analogy of optimal searching of object in
real time and maps to CO1, CO2 and attains with PO1,PO2,PO3,PO4,PO5,PO12,
PSO1,PSO2,PSO3.
.
VIVA QUESTIONS:
1. What is the Searching? Explain Search techniques in java?
2. What is the time complexity for Linear Search? Explain?
3. If element is not present in the given elements then what is time complexity?
EXERCISE 7
AIM: Write a java program to search for an element in a given list of elements (Binary Search)
DESCRIPTION:
Generally, to find a value in unsorted array, we should look through elements of an array one
by one, until searched value is found. In case of searched value is absent from array, we go
through all elements. In average, complexity of such an algorithm is proportional to the
length of the array. Situation changes significantly, when array is sorted. If we know it,
random access capability can be utilized very efficiently to find searched value quick. Cost
of searching algorithm reduces to binary logarithm of the array length. For reference, log 2 (1
000 000) ≈ 20. It means, that in worst case, algorithm makes 20 steps to find a value in
sorted array of a million elements or to say, that it doesn't present it the array.
ALGORITHM:
1. start
2. import package
4. declare the method and perform binary search in which the mid value of the array is calculated
and if the key value is less than the mid value then key is searched in the first part of the array
otherwise it is searched in second part of the array
6. end method
7. end class
9. create a object for the first class and call the method
11. stop
OUTPUT:
E:\java>javac SearchEmp.java
E:\java>java SearchEmp
enter details
james 3
enter details
peter 1
enter details
gosling 2
Enter key
2
2 found at location 2 gosling
CONCLUSION:
Searching concept mitigates for student to get analogy of optimal searching of object in
real time and maps to CO1, CO2 and attains with PO1,PO2,PO3,PO4,PO5,PO12,
PSO1,PSO2,PSO3.
VIVA QUESTIONS
1. What is the Time complexity of Binary Search? Explain?
EXERCISE 8
DESCRIPTION:
This code adds two matrixes you can modify it to add any number of matrices. You
can create a Matrix class and create it's objects and then create an add method which sum the
objects, then you can add any number of matrices by repeatedly calling the method using a loop.
ALGORITHM:
1: start
2: import package
4: declare a method and perform the addition on the matrix a and b and assign it to matrix c
9: end class
10: stop
OUTPUT:
E:\java>javac MatrixImpl.java
E:\java>java MatrixImpl
enter m,n
22
enter matrix 1 elements
2463
enter matrix 2 elements
7356
matrix 1:
24
63
matrix 2:
73
56
addition of matrices:
sum is
97
11 9
CONCLUSION:
Student able to understand implementation of matrices for mathematical methods using
different real time examples and maps to CO1, CO2 and attains with
PO1,PO2,PO3,PO4,PO5,PO12, PSO1,PSO2,PSO3.
VIVA QUESTIONS:
1. What is an Array? How do you define two dimensional arrays in java?
2. How we can print two dimensional array as matrix format?
3. What is the transpose of given matrix?
EXERCISE 9
AIM: Write a java program to determine the multiplication of two matrices
DESCRIPTION:
Let the two matrix to be multiplied be A and B. Let A be a matrix of order d*e - d rows
and e columns and B be the second matrix of order e*f. Note that the number of columns in the
first matrix should be the same as the number of rows in the second matrix. The product matrix
will be of order d*f. Now, our task is to find what numbers go into each of the positions of
matrix C with d rows and f columns. We use an outer loop with loop counter i ranging from 0 to
d. An inner loop has the loop counter j ranging from 0 to f. At any iteration of the loop, i refers to
the row number and j to the column number of the position in matrix C that we are trying to fill.
Now, within the body of the inner loop, we have to write some code which will calculate the
value to be filled. This body will in turn consist of a loop. When we are obtaining the value at
C[i][j], row i of matrix A and column j of matrix B are multiplied element wise.
C[i][j] = A[i][0] * B[0][j] + A[i][1] * B[1][j] + A[i][2] * B[2][j] + .... A[i][e-1] * B[e-1][j]
Both row i and column j have e elements. An loop is used whose counter k, ranges from 0 to e-1.
Within the loop, A[i][k] and B[k][j] are multiplied and the product obtained is added to the
existing value of C[i][j]. We can also declare a variable sum before the start of the innermost
loop, add the element wise products to this variable and assign the resulting sum to C[i][j].
ALGORITHM:
1. start
2. import package
4. declare a method and perform the multiplication on the matrix a and b and assign it to matrix
c
9. end class
10. stop
OUT PUT:
E:\java>javac MatrixMul.java
E:\java>java MatrixMul
enter m,n
22
enter matrix 1 elements
4234
enter matrix 2 elements
6234
matrix 1:
42
34
matrix 2:
62
34
multiplication of matrices:
multiplication is
30 16
30 22
CONCLUSION:
VIVA QUESTIONS:
EXERCISE 10
AIM: Write a java program to sort an array of strings.
DESCRIPTION:
Strings in java are immutable in nature. Immutable means we cannot change the String objects.
Strings are collection of characters, used to perform different pattern matching operations. Here
we have compareTo() method to check strings. If two strings are equal then it will return 0 , if
first string is greater than second one then it will return positive number otherwise negative
number
ALGORITHM:
1.start
4.create a method and sort the array by using one of the sorting technique and print the new
array that is sorted
5. end method
6. end class
11. stop
OUTPUT:
E:\java>javac SortingEmployeeNames.java
E:\java>java SortingEmployeeNames
Enter details:
Enter details:
Enter details:
sai e3 50000
krish
ram
sai
CONCLUSION:
Student able to understand implementation of storing elements into static and dynamic
array allocation and maps to CO1, CO2 and attains with PO1,PO2,PO3,PO4,PO5,PO12,
PSO1,PSO2,PSO3.
VIVA QUESTIONS
3. What is compareTo() method? what are the values return by this method?
EXERCISE 11
AIM: Write a java program to check whether the given string is palindrome or not.
DESCRIPTION:
Palindrome is the string pattern, which is same as the string when it is reversed. To
reverse the string we have reverse() in StringBuffer class. StringBuffer class objects are
mutable(can be manipulate) where String class objects are immutable.
ALGORITHM:
1. Create a class and write a method to check palindrome
2.Using charAt(int) findout reverse string
3. check reverse string with original string using equals()
4. Create an instace to above class
5. Call method using instance.
OUTPUT:
enter string
madam
string is palindrome
CONCLUSION:
Student able to understand implementation of text processing and its methodologies and
maps to CO1, CO2 and attains with PO1,PO2,PO3,PO4,PO5,PO12, PSO1,PSO2,PSO3.
VIVA QUESTIONS:
1. What is palindrome?
2. How to replace characters in strings?
3. What is the use of toString()?
4. What is the meaning of mutable objects?
5. Is StringBuffer object called mutable or immutable?
EXERCISE 12
AIM: Write a java program for the following
Example for call by value
Example for call by reference.
DESCRIPTION:
Java supports call by value by passing a value and returning the value. Call by reference
is way of passing object into setter method and returning object as type. Steps to call are:
ALGORITHM:
Method(o,i)
Javac Calls.java
Java Calls
CONCLUSION:
Able to understand the usage of functions to pass value and address that achieves Method
area in Java Virtual Machine and maps to CO1, CO2 and attains with
PO1,PO2,PO3,PO4,PO5,PO12, PSO1,PSO2,PSO3.
Viva-Voce Questions
1. How to pass an object as a parameter into a setter method?
2. What are setters and getters?
EXERCISE 13
AIM: Write a java program to give the example for “this” operator and also use the ‘this’
keyword as return statement.
DESCRIPTION:
Java supports ‘this’ object in order to process: Current class properties, Current class methods
and Current class Constructors. Bydefault this reference will be created when an object is
created. Refer present class members as follows:
Variablethis.varible_name;
Methodsthis.method_name();
Constructorthis(parameter_list);
ALGORITHM:
1. Create a class and write two constructors
2. Using this(string) pass value to parameterized constructor.
3. Create a method and initialize eid,ename,designation
4. Print details with printEmployeeDetails()
6. using instace set salary and call methods.
OUTPUT:
D:\JAVA\java>javac Employee.java
D:\JAVA\java>javac Salary.java
D:\JAVA\java>javac EmployeeImpl.java
D:\JAVA\java>java EmployeeImpl
lendi
enter id,name,designation
enter basic,da,hra
totalsalary=24000.0
CONCLUSION:
VIVA QUESTIONS:
EXERCISE 14
AIM: Write a java program to demonstrate static variables, methods, and blocks.
DESCRIPTION: Static Keyword in Java is used to access the data without creating Object.
Also static creates the single memory allocation for all Objects. Static method can accept only
static variables. Static block has height priority than static methods(even there is main) as well as
instance methods.
ALGORITHM:
Otherwise failed
OUTPUT:
D:\JAVA\java-1>javac Validity.java
D:\JAVA\java-1>javac Login.java
D:\JAVA\java-1>java Login
enter details
enter username
navya nav
enter password
validity is success
D:\JAVA\java-1>java Login
enter details
enter username
navya n
enter password
failed
CONCLUSION:
VIVA QUESTIONS:
3. Can we execute java program using static block without main()? Explain how?
EXERCISE 15
AIM: Write a java program to give the example for ‘super’ keyword
DESCRIPTION:
Reusability is very important feature in Inheritance, where accessing base class properties and
methods is needed. Super keyword is such object which do the job, by handling the super class
properties, methods and constructors. Invoking super keyword can be done in following way:
Variablesuper.variable_name;
Methodssuper.method_name();
Constructorsuper(parameter_list);
ALGORITHM:
super.variablename;
super.methodname();
super(parameter list)
3. Create an instance to sub class called Faculty and access members using instance
OUTPUT:
D:\JAVA\java-1\simple>javac Employee.java
D:\JAVA\java-1\simple>javac Faculty.java
D:\JAVA\java-1\simple>javac EmployeeImpl.java
D:\JAVA\java-1\simple>java EmployeeImpl
CONCLUSION:
Parent and child relationship between objects to analyze object oriented features. And
maps to CO1, CO2 and attains with PO1,PO2,PO3,PO4,PO5,PO12, PSO1,PSO2,PSO3.
VIVA QUESTIONS
1. How to call super class constructor?
2. Can I Use super inside static method?
3. What is the use of super?
4. Why should call super class constructor as a first statement in sub class constructor?
EXERCISE 16
DESCRIPTION:
Inheritance is way to generate “IS-A” relationship between Parent and Child by accessing the
properties and methods. It increases readability and reusability of code. It can be implemented in
many ways like Simple, Multi-level, Hybrid.
ALGORITHM:
super.variablename;
super.methodname();
super(parameter list)
4. Create an instance to sub class called Faculty and access members using instance
OUTPUT:
D:\JAVA\java-1\simple>javac Employee.java
D:\JAVA\java-1\simple>javac Faculty.java
D:\JAVA\java-1\simple>javac EmployeeImpl.java
D:\JAVA\java-1\simple>java EmployeeImpl
CONCLUSION:
Java reusability is main feature that implements the readability of the application and
illustrating real time scenarios. And maps to CO1, CO2 and attains with
PO1,PO2,PO3,PO4,PO5,PO12, PSO1,PSO2,PSO3
VIVA QUESTIONS:
1. What is reusability?
EXERCISE 17
DESCRIPTION:
Inheritance is one way of implementing super-sub class relationship, for data usability and
readability. Implementing Multi-level inheritance is better one for Objects whose data will be
accessed by many objects there by increasing readability.
CLASS A
CLAS B
CLASS C
ALGORITHM:
super.variablename;
super.methodname();
super(parameter list)
4. Create an instance to sub class called Professor and access members of both Faculty and
Professor using instance
OUTPUT:
D:\JAVA\java-1\simple>javac Employee.java
D:\JAVA\java-1\simple>javac Faculty.java
D:\JAVA\java-1\simple>javac Professor.java
D:\JAVA\java-1\simple>javac EmployeeImpl.java
D:\JAVA\java-1\simple>java EmployeeImpl
CONCLUSION:
Interrelating multiple objects by using the feature of inheritance and enveloping them into
a single real world object .And maps to CO1, CO2 and attains with
PO1,PO2,PO3,PO4,PO5,PO12, PSO1,PSO2,PSO3.
VIVA QUESTIONS:
1. Can java implement multiple inheritance? Explain?
2. What is abstract class?
3. Can we create an instance to abstract class?
EXERCISE 18
DESCRIPTION:
Polymorphism is foremost concern in java to implement methods behaviour in class and in one
or more classes. Having one or forms for the desire of data sharing. Overloading is the one,
whose methods have same name but different type signatures. Type promotion is supported in
Overloading.
Overriding on the other hand, methods with same name and parameters in more than one class
which are of ‘IS-A’ relationship.
ALGORITHM:
OUTPUT:
D:\java>javac MethodOver.java
D:\java>java MethodOver
Peter
e20
31/may/1991
25
50000.0
CONCLUSION:
Polymorphic objects play a major role to interpolating multiple features into single object
inorder to inculcate and analyze the nature of object. And maps to CO1, CO2 and attains with
PO1,PO2,PO3,PO4,PO5,PO12, PSO1,PSO2,PSO3.
VIVA QUESTIONS:
1. Can we overload method using return type?
2. What is type promotion in overloading?
3. What is method overriding?
4. Differentiate both method overloading and overriding?
5. What is final keyword?
EXERCISE 19
DESCRIPTION:
This program describes the difference between method overloading and constructor loading.
Method overloading is a feature that allows a class to have two or more methods having same
name, if their argument lists are different. Constructor overloading that allows a class to have
more than one constructor having different argument lists. When overload method is called, java
looks for a match between the arguments used to call the method and the method parameters.
Finally it matches and displays the output.
ALGORITHM:
2. Write methods called called showDetails(), showDetails(String) and make method overloading
OUTPUT:
D:\java>javac MethodOver.java
D:\java>java MethodOver
James
e10
31/may/1991
25
50000.0
CONCLUSION:
Polymorphic objects play a major role to interpolating multiple features into single object
inorder to inculcate and analyze the nature of object. And maps to CO1, CO2 and attains with
PO1,PO2,PO3,PO4,PO5,PO12, PSO1,PSO2,PSO3.
VIVA QUESTIONS:
1.What is method overloading?
2.What are the types of constructors?
3.What is constructor overloading?
EXERCISE 20
DESCRIPTION:
Exception is the run time error. It will be raised at run time. The exception handling in java is
one of the powerful mechanism to handle the runtime errors so that normal flow of the
application can be maintained.
Checked exceptions − A checked exception is an exception that occurs at the compile time,
these are also called as compile time exceptions. These exceptions cannot simply be ignored at
the time of compilation, the programmer should take care of (handle) these exceptions.
Unchecked exceptions − An unchecked exception is an exception that occurs at the time of
execution. These are also called as Runtime Exceptions. These include programming bugs, such
as logic errors or improper use of an API. Runtime exceptions are ignored at the time of
compilation.
ALGORITHM:
1. Import required packages.
2. Create a class called ExceptionDemo
3. Read values called sId,sName
4. Check two run time errors called StringIndexOutOfBounds and NullPointerException
5. Place statements in try block
6. Catch runtime errors usind catch block and print message
OUTPUT:
Enter Id and Name:
12KD1A0501
Ravi
The student named RAVI is from CSE Dept
CONCLUSION:
Handling errors is major role in application programming to achieve bug free and fixing
errors at runtime. And maps to CO1, CO2 and attains with PO1,PO2,PO3,PO4,PO5,PO12,
PSO1,PSO2,PSO3.
VIVA QUESTIONS:
1. What is an exception?
2. What is the functionality of catch and finally blocks?
3. What is the difference between throw and throws?
4. What is multiple catch block?
EXERCISE 21
AIM: Write a JAVA program for example of try and catch block. In this check whether the
given array size is negative or not.
DESCRIPTION:
Program explains exceptions using try and catch block. In the try block array is created. Check
the array size is negative or not. If the array size is positive then it will be display array size
otherwise it throws an error like java.lang.NegativeArraySizeException.
ALGORITHM:
1:start
2: create class1
declare a parameterized constructor in which assign variable msg to str
declare a method ()
read size of array and declre array
end method()
end class
3.declare main class
give main method as public static void main(String[] args)
create object to class 1
call method declared in class 1 using object.
end main()
end class
4.stop
OUTPUT:
Enter the size:
-3
Array Size is Negative..
CONCLUSION:
Handling errors is major role in application programming to achieve bug free and fixing
errors at runtime. And maps to CO1, CO2 and attains with PO1,PO2,PO3,PO4,PO5,PO12,
PSO1,PSO2,PSO3.
VIVA QUESTIONS:
1. What are the different types of exceptions used in catch block?
EXERCISE 22
AIM: Write a JAVA program to illustrate sub class exception precedence over base class.
DESCRIPTION: In multiple catch blocks exception classes order should be from sub classes to
super classes.
Try
{//body
}
Catch(FileNotFoundException f){}
Catch(EOFException e){}
Catch(IOException i){}
ALGORITHM:
1.start
2.create class 1 extending from Exception base class.
declare a constructor for class1 through which constructor of base class is called.
end class 1.
3.create class 2
write a method and place the statements of exception in try block.
write multiple catch blocks for exceptions occurred, give precedence to subclasses first
and then base class Exception.
print the message using getMessage() method.
end class2.
4.declare main class
give main method as public static void main(String[] args)
create object to class 2
call method declared in class 2 using object.
end main()
end class
5. stop
OUTPUT:
lendi cse dept
CONCLUSION:
Handling errors is major role in application programming to achieve bug free and fixing
errors at runtime. And maps to CO1, CO2 and attains with PO1,PO2,PO3,PO4,PO5,PO12,
PSO1,PSO2,PSO3.
VIVA QUESTIONS:
1. What is an exception?
2. What is the functionality of catch block?
3. What is the use of multi catch block?
4. what is sub class exception precedence over base class explain with example?
5. What is super class of all exception classes?
6. What is difference between checked and unchecked exceptions?
EXERCISE 23
DESCRIPTION: If you are creating your own Exception that is known as custom exception or
user-defined exception. Java custom exceptions are used to customize the exception according to
user need.
By the help of custom exception, you can have your own exception and message.
ALGORITHM:
1.start
2. create a class 1 that extends from Exception base class .
declare a constructor that calls super class constructor.
OUTPUT:
Enter employee EID to find:
e7
Employee Missing..
CONCLUSION:
Handling errors is major role in application programming to achieve bug free and fixing
errors at runtime. And maps to CO1, CO2 and attains with PO1,PO2,PO3,PO4,PO5,PO12,
PSO1,PSO2,PSO3.
VIVA QUESTIONS:
1. What is mean by user defined exception?
2. What is BufferedReader class?
3. How can we access user defined exception?
EXERCISE 24
AIM: Write a JAVA program to illustrate creation of threads using runnable class.(start method
start each of the newly created thread. Inside the run method there is sleep() for suspend the
thread for 500 milliseconds).
DESCRIPTION:
In this program created a thread using Runnable interface. In the Runnable interface a main
thread and child threads are created, along with a run method is used. In the run method there is a
thread sleep method for children and the children is suspending for 500ms and used join().
Syntax:
Try{Thread.sleep(500)}
Catch(InterruptedException ie){}
ALGORITHM:
1.start
2. create a class1 that implements Runnable interface.
declare a constructor for declaring message.
start the thread using start()
end class
OUTPUT:
Breakfast
Lunch
Supper
Dinner
CONCLUSION:
Multi tasking and processing is major part in operating system inorder to share the time
for real time batch processing. And maps to CO1, CO2 and attains with
PO1,PO2,PO3,PO4,PO5,PO12, PSO1,PSO2,PSO3.
VIVA QUESTIONS:
1.What is a Thread?
2.Which method is used to create a thread?
3.What are the stages of thread creation?
4.What is sleep()?
4.Why should we call sleep() with Thread class?
EXERCISE 25
AIM: Write a JAVA program to create a class MyThread in this class a constructor, call the base
class constructor, using super and starts the thread. The run method of the class starts after this. It
can be observed that both main thread and created child thread are executed concurrently.
DESCRIPTION:
This program create a class called My Thread. In this class a constructor calls the base class
constructor using super and starts the thread. The run method of the class starts after this. Here
we can observed that both main thread and creation of child thread are executed concurrently.
ALGORITHM:
1.start
print Thread.currentThread().getName()
i.e name of thread
give sleep()method for interruption of other Threads i.e to
run multi threads simultaneously
end try
declare catch block for handling interruptedException occured.
end for
end run()
end class
3.create main class
give main method as public static void main(String[] args)
create object to class 1
call method of class1
OUTPUT:
Main Thread Running:main
Running Thread is:Thread-0
CONCLUSION:
Multi tasking and processing is major part in operating system inorder to share the time
for real time batch processing. And maps to CO1, CO2 and attains with
PO1,PO2,PO3,PO4,PO5,PO12, PSO1,PSO2,PSO3
VIVA QUESTIONS:
EXERCISE 26
DESCRIPTION: Interfaces are completely abstract classes in Java that provide you with a
uniform way to properly delineate the structure or inner workings of your program from its
publicly available interface, with the consequence being a greater amount of flexibility and
reusable code as well as more control over how you create and interact with other classes. More
precisely, they are a special construct in Java with the additional characteristic that allow you to
perform a kind of multiple inheritance i.e. classes that can be upcast to more than one class; a
technique from which you are severely restricted from undertaking (and with good reason) when
working exclusively with classes. To create an interface in Java, you simply use the
special interface keyword.
Note: Interface consists only abstract methods and static final members.
ALGORITHM:
1 start
2 create a class.
in this class we use number of methods
3 after that create a sub class from super class.
in this also we use methods with diffarent name.
4 after that create an interface like this
interface interfacename
{
//methods;
}
5 after that create a new class from sub class and impliments the methods in interface.
syntax:
class classname extends subclassname impliments methodname
{
//statements;
}
6 after that create a main class
in this create an object for subclass.
7 stop
OUTPUT:
D:\java\java 2>javac MultipleInheritance.java
CONCLUSION:
Parent and child relationship between objects to analyze object oriented features. And
maps to CO1, CO2 and attains with PO1,PO2,PO3,PO4,PO5,PO12, PSO1,PSO2,PSO3.
VIVA QUESTIONS:
1.What is an interface?
2.How can you implement multiple inheritance in java?
3. How can we implement abstract methods?
4.What is the difference between interface and abstract class?
EXERCISE 27
AIM: Write a java program to create a package called employee and import this package out of
the package?
DESCRIPTION: Packages are used in Java in order to prevent naming conflicts, to control
access, to make searching/locating and usage of classes, interfaces, enumerations and
annotations easier, etc.A Package can be defined as a grouping of related types (classes,
interfaces, enumerations and annotations ) providing access protection and namespace
management.
java.io − classes for input , output functions are bundled in this package
java.io − classes for input , output functions are bundled in this package
While creating a package, you should choose a name for the package and include
a package statement along with that name at the top of every source file that contains the classes,
interfaces, enumerations, and annotation types that you want to include in the package.
We have two ways to access members from out of the package:
1. import
2. package.classname
Note: Interface consists only abstract methods and static final members.
ALGORITHM:
1 create a package named p1
syntax: package packagename
2 using this syntax to create an implimentation of package
syntax: public class classname impliments classname
3 in this we use more methods
4 after that create an main method and create an object to super class
5 stop
CONCLUSION:
Parent and child relationship between objects to analyze object oriented features. And
maps to CO1, CO2 and attains with PO1,PO2,PO3,PO4,PO5,PO12, PSO1,PSO2,PSO3.
OUTPUT:
D:\java\packages>javac -d . Employee.java
D:\java\packages>javac -d . Faculty.java
D:\java\packages>javac -d . FacultyType.java
D:\java\packages>set classpath=%classpath%.;d\java\packages\*.class;
D:\java\packages>javac Total.java
D:\java\packages>java Total
e1
sai
professor
full time
VIVA QUESTIONS:
1.What is package?
2.How can you create user defined package?
3.How can compile package?
4.How many ways to access a package?
5. why should we mention public to each member of a package?
EXERCISE 28
AIM: Write a java program to create a package called employee and implement this package out
of the package?
DESCRIPTION: Packages are used in Java in order to prevent naming conflicts, to control
access, to make searching/locating and usage of classes, interfaces, enumerations and annotations
easier, etc.
Some of the existing packages in Java are −
java.io − classes for input , output functions are bundled in this package
ALGORITHM:
OUTPUT:
D:\java\java 2\packages\pkgs1>javac -d . VehicleDetails.java
CONCLUSION:
Bundling data through classes and interfaces to make package and implementing java
encapsulation with specifying access handlers. And maps to CO1, CO2 and attains
PO1,PO2,PO3,PO4,PO5,PO12, PSO1,PSO2,PSO3.
VIVA QUESTIONS:
1. What is a package?
2. what is access specifier?
3. what is protected keyword?
EXERCISE 29
AIM: Write a java program to give a simple example for abstract class?
DESCRIPTION:
An abstract class is, conceptually, a class that cannot be instantiated and is usually implemented
as a class that has one or more pure virtual (abstract) functions.
A pure virtual function is one which must be overridden by any concrete (i.e., non-abstract)
derived class. This is indicated in the declaration with the syntax " = 0" in the member function's
declaration. The purpose of an abstract class (often referred to as an ABC) is to provide an
appropriate base class from which other classes can inherit. Abstract classes cannot be used to
instantiate objects and serves only as an interface. Attempting to instantiate an object of an
abstract class causes a compilation error. Thus, if a subclass of an ABC needs to be instantiated,
it has to implement each of the virtual functions, which means that it supports the interface
declared by the ABC. Failure to override a pure virtual function in a derived class, then
attempting to instantiate objects of that class, is a compilation error. Classes that can be used to
instantiate objects are called concrete classes.
ALGORITHM:
1 start
2 create an abstract class like this
syntax: abstract class classname
3 In this we create a abstract method
4 After that create a sub class
5 Iin this sub class run the abstract method.
6 After that create a main method and create an object to sub class
7 with the help of object call a abstract method
8 stop
CONCLUSION:
Abstracting the data into object needs to hide the functionalities and implementing the
exisiting ones. And maps to CO1, CO2 and attains with PO1,PO2,PO3,PO4,PO5,PO12,
PSO1,PSO2,PSO3.
OUTPUT:
D:\java\java 2>javac AbstractFans.java
VIVA QUESTIONS:
1. What is an abstract class?
2. What is concrete class?
3. can we create an instance to an abstract class?
4. What are the members of an abstract class?
EXERCISE 30
AIM: Write a JAVA program that describes the life cycle of an applet.
DESCRIPTION:
init()- Called exactly once in an applet’s life. It is Called when applet is first loaded, which is
after object creation, e.g., when the browser visits the web page for the first time. Used to read
applet parameters, start downloading any other images or media files, etc.
start()-Called at least once. Called when an applet is started or restarted, i.e., whenever the
browser visits the web page.
stop()- Called at least once. Called when the browser leaves the web page.
destroy ()- Called exactly once. Called when the browser unloads the applet. Used to perform
any final clean-up.
ALGORITHM:
6.Embed class file into html file and run it either using appletViewer or browser
OUTPUT:
// Create a ColorApplet.html to run applet in browser
javac ColorApplet.java
appletviewer ColorApplet.html
OR
javac ColorApplet.java
appletviewer ColorApplet.java
CONCLUSION:
Desiging the graphical application to view the real wolrd ideas into application view.
This generates a GUI based implementation for student and can implement stand alone projects
easily using Java Foundation Classes. Applications like Biometric, Eye recognition and Aadhar
card data storage are good scenarios. It maps to CO1,C02,CO3,CO4,CO5,CO6 and attains to
PO1,PO2,PO3,PO4,PO5,PO12, PSO1,PSO2,PSO3
VIVA QUESTIONS:
1. What is an Applet?
2. What are the stages of applet?
3. What is the use of paint() method?
4. From where paint() is using?
5. what is drawString(String,int,int) method?
6. what is class hierarchy of Applet?
EXERCISE 30(A)
DESCRIPTION:
Dialog control represents a top-level window with a title and a border used to take some form
of input from the user. This class inherits methods from the following classes:
java.awt.Window
java.awt.Component
java.lang.Object
package: java.awt.Dialog
ALGORITHM:
OUTPUT:
CONCLUSION:
Desiging the graphical application to view the real wolrd ideas into application view.
This generates a GUI based implementation for student and can implement stand alone projects
easily using Java Foundation Classes. Applications like Biometric, Eye recognition and Aadhar
card data storage are good scenarios. It maps to CO1,C02,CO3,CO4,CO5,CO6 and attains to
PO1,PO2,PO3,PO4,PO5,PO12, PSO1,PSO2,PSO3
VIVA QUESTIONS:
1. what is awt package?
EXERCISE 30(B)
DESCRIPTION:
The GridLayout is used to arrange the components in rectangular grid. One component is
displayed in each rectangle.
Constructors:
1. GridLayout(): creates a grid layout with one column per component in a row.
2. GridLayout(int rows, int columns): creates a grid layout with the given rows and
columns but no gaps between the components.
3. GridLayout(int rows, int columns, int hgap, int vgap): creates a grid layout with the
given rows and columns alongwith given horizontal and vertical gaps.
ALGORITHM:
1.Create a class and extends it from JFrame
2.set border layout to a frame using GridLayout
setLayout(gridlayoutobject);
3.Create 5 buttons using JButton
JButton b1=new JButton("Button1");
JButton b2=new JButton("Button2");
JButton b3=new JButton("Button3");
JButton b4=new JButton("Button4");
JButton b5=new JButton("Button5");
add(b5);
5.finally make frame as visible using-
setVisible(true);
OUTPUT:
CONCLUSION:
Desiging the graphical application to view the real wolrd ideas into application view.
This generates a GUI based implementation for student and can implement stand alone projects
easily using Java Foundation Classes. Applications like Biometric, Eye recognition and Aadhar
card data storage are good scenarios. It maps to CO1,C02,CO3,CO4,CO5,CO6 and attains to
PO1,PO2,PO3,PO4,PO5,PO12, PSO1,PSO2,PSO3
VIVA QUESTIONS:
EXERCISE 31
DESCRIPTION:
The BorderLayout is used to arrange the components in five regions: north, south, east, west and
center. Each region (area) may contain one component only. It is the default layout of frame or
window. The BorderLayout provides five constants for each region:
ALGORITHM:
1.Create a class and extends it from JFrame
2.set border layout to a frame using BorderLayout
setLayout(borderlayoutobject);
3.Create 5 buttons using JButton
JButton b1=new JButton("Button1");
JButton b2=new JButton("Button2");
JButton b3=new JButton("Button3");
JButton b4=new JButton("Button4");
JButton b5=new JButton("Button5");
add("North",b1);
add("East",b2);
add("South",b3);
.add("West",b4);
add("Center",b5);
5.Finally make frame as visible using-
setVisible(true);
OUTPUT:
CONCLUSION:
Desiging the graphical application to view the real wolrd ideas into application view.
This generates a GUI based implementation for student and can implement stand alone projects
easily using Java Foundation Classes. Applications like Biometric, Eye recognition and Aadhar
card data storage are good scenarios. It maps to CO1,C02,CO3,CO4,CO5,CO6 and attains to
PO1,PO2,PO3,PO4,PO5,PO12, PSO1,PSO2,PSO3
VIVA QUESTIONS:
EXERCISE 32
DESCRIPTION:
This program is used to design layouts of user defined. The layout borders are set suing
setLayout method. Then add three fields to the layout like no, name, email id and url using These
fields are added using JButton method. Finally the layout is created.
ALGORITHM
4.Add the table of name, e-mail, and URL using add() method
INPUT-OUTPUT:
CONCLUSION:
Desiging the graphical application to view the real wolrd ideas into application view.
This generates a GUI based implementation for student and can implement stand alone projects
easily using Java Foundation Classes. Applications like Biometric, Eye recognition and Aadhar
card data storage are good scenarios. It maps to CO1,C02,CO3,CO4,CO5,CO6 and attains to
PO1,PO2,PO3,PO4,PO5,PO12, PSO1,PSO2,PSO3
VIVA QUESTIONS:
EXERCISE 33
DESCRIPTION: This program is used to create a simple calculator having basic arithmetic
operations like addition, subtraction, multiplication, division and modulo division. This
calculator is having buttons and these buttons are created using Button() method. Also this
having text field to display result.
ALGORITHM:
OUTPUT:
CONCLUSION:
Desiging the graphical application to view the real wolrd ideas into application view.
This generates a GUI based implementation for student and can implement stand alone projects
easily using Java Foundation Classes. Applications like Biometric, Eye recognition and Aadhar
card data storage are good scenarios. It maps to CO1,C02,CO3,CO4,CO5,CO6 and attains to
PO1,PO2,PO3,PO11 and PSO1, PSO2
VIVA QUESTIONS:
EXERCISE 34
DESCRIPTION:
Changing the state of an object is known as an event. For example, click on button, dragging
mouse etc. The java.awt.event package provides many event classes and Listener interfaces for
event handling.
ActionEvent ActionListener
MouseWheelEvent MouseWheelListener
KeyEvent KeyListener
ItemEvent ItemListener
TextEvent TextListener
AdjustmentEvent AdjustmentListener
WindowEvent WindowListener
ComponentEvent ComponentListener
ContainerEvent ContainerListener
FocusEvent FocusListener
ALGORITHM:
OUTPUT:
CONCLUSION:
Desiging the graphical application to view the real wolrd ideas into application view.
This generates a GUI based implementation for student and can implement stand alone projects
easily using Java Foundation Classes. Applications like Biometric, Eye recognition and Aadhar
card data storage are good scenarios. It maps to CO1, C02, CO3, CO4, CO5, CO6 and attains to
PO1,PO2,PO3,PO4,PO5,PO12, PSO1,PSO2,PSO3
VIVA QUESTIONS:
EXERCISE 35
AIM :A java program that displays number of characters, lines and words in a text file
DESCRIPTION:
The java.io package contains nearly every class you might ever need to perform input and output
(I/O) in Java. All these streams represent an input source and an output destination. The stream
in the java.io package supports many data such as primitives, object, localized characters, etc.
Byte Streams
Java byte streams are used to perform input and output of 8-bit bytes. Though there are
many classes related to byte streams but the most frequently used classes
are, FileInputStream and FileOutputStream.
Character Streams
Java Byte streams are used to perform input and output of 8-bit bytes, whereas
Java Character streams are used to perform input and output for 16-bit unicode. Though
there are many classes related to character streams but the most frequently used classes
are, FileReader and FileWriter. Though internally FileReader uses FileInputStream and
FileWriter uses FileOutputStream but here the major difference is that FileReader reads
two bytes at a time and FileWriter writes two bytes at a time.
ALGORITHM:
OUTPUT:
D:\JAVA>javac CountLines.java
D:\JAVA>java CountLines
characters:63
words:12
lines:2
CONCLUSION:
Analyzing and accessing text file processing inorder to retrieve the data and handle that
for report generation. All data analytics applications needs to use file streaming strategies It
maps to CO1, C02, CO3, CO4, CO5, CO6 and attains to PO1,PO2,PO3,PO4,PO5,PO12,
PSO1,PSO2,PSO3
VIVA QUESTIONS:
EXERCISE-36
AIM: Write a Java program to demonstrate Java data Base Connectivity.
DESCRIPTION:
ALGORITHM:
INPUT-OUTPUT:
javac JdbcConnectivity.java
java JdbcConnectivity
CONCLUSION:
Creating the database and implementing opetations on that data is very crucial in the real
world applications in order to handle business logic and implementing Model of Application. It
maps to CO1, CO2, CO3, CO4, CO5, CO6 and attains PO1,PO2,PO3,PO4,PO5,PO12,
PSO1,PSO2,PSO3.
1) What is JDBC?
2) What are different types of drivers available?
EXERCISE-37
DESCRIPTION:
Java Socket programming is used for communication between the applications running
on different JRE. Java Socket programming can be connection-oriented or connection-
less.Socket and ServerSocket classes are used for connection-oriented socket programming and
DatagramSocket and DatagramPacket classes are used for connection-less socket programming.
The client in socket programming must know two information: IP Address of Server, and Port
number. The ServerSocket class can be used to create a server socket. This object is used to
establish communication with the clients.
ALGORITHM:
Open a socket.
Read from and write to the socket according to the server's protocol.
Clean up.
INPUT-OUTPUT:
CONCLUSION:
Creating the client server computing application for connecting different networks and
sharing data between themselves in all web based applications. It maps to CO1, CO2, CO3, CO4,
CO5, CO6 and attains PO1,PO2,PO3,PO4,PO5,PO12, PSO1,PSO2,PSO3
1) What is Socket?
2) What is InetAddress?
3) hat is the package used to implement Socket Connection?
EXERCISE-38
DESCRIPTION:
A HashMap contains values based on the key. It implements the Map interface and
extends AbstractMap class.It contains only unique elements. It may have one null key and
multiple null values. It maintains no order. Sorted order of Keys in the Map can done with
TreeMap Interface.
ALGORITHM:
INPUT-OUTPUT
Javac Telephone.java
Java Telephone
CONCLUSION:
Collecting data objects and doing operations on those data collection like adding,
deleting, searching and iterating that resembles real world data like GPS tracking and
implementing Big data Collection. It maps to CO1, CO2, CO3, CO4, CO5, CO6 and attains
PO1,PO2,PO3,PO4,PO5,PO12, PSO1,PSO2,PSO3
1) What is Iterator?
2) Difference between Hashtable or HashMap?
3) What is Comparator?
EXERCISE-39
DESCRIPTION:
Java Reflection is a process of examining or modifying the run time behavior of a class at
run time. The java.lang.Class class provides many methods that can be used to get metadata,
examine and change the run time behavior of a class. The java.lang
and java.lang.reflect packages provide classes for java reflection.
ALGORITHM:
INPUT-OUTPUT
javac GetConstructors.java
java GetConstructors
CONCLUSION:
Introspecting object and implementing its behavior is aim of this program. This annotates
the students learning how object behaves int real world entities.. It maps to CO1, CO2 and
attains PO1,PO2,PO3,PO4,PO5,PO12, PSO1,PSO2,PSO3
1) What is Reflection?
Reflection is used to read a class dynamically to introspect object
2) What is the package used to implement java reflection API?
The package used to implement is : java.lang.reflect.*
3) What is method used to retrieve constructors from Object?
Constructor class using getDeclaredConstructors()
EXERCISE-40
DESCRIPTION:
Threads are very important in scheduling of Tasks. They can be implemented using either
Runnable interface or Thread Class.
ThreadGroup class is used to group the threads by adding them. Finding the active count
of threads can be found by passing threads as a parameter into group.
ALGORITHM:
INPUT-OUTPUT:
Javac ThreadGroupDemo.java
Java ThreadGroupDemo
DVD
Remote
Home Theatre
Number of Threads in Group are: 3
CONCLUSION:
Introspecting object and implementing its behavior is aim of this program. This annotates
the students learning how object behaves int real world entities.. It maps to CO1, CO2 and
attains PO1,PO2,PO3,PO4,PO5,PO12, PSO1,PSO2,PSO3