Java Lab Manual
Java Lab Manual
Lab # 01 4
Introduction to Java key concepts, configuration and installation 4
1.1 Objective: 4
1.2 Scope: 4
1.3 Useful Concepts: 4
1.4 Example code 6
1.5 Exercise 8
1.6 Home Work 9
Lab # 02 10
Introduction to Logical ,Relational and loops in java 10
2.1 Objective: 10
2.2 Scope: 10
2.3 Useful Concepts: 10
2.4 Examples 13
2.5 Exercises for lab 14
2.6 Home Work: 14
Lab # 03 14
Introduction to Primitive data types, switch cases and Functions 14
3.1 Objective: 14
3.2 Scope: 14
3.3 Useful Concepts and Code: 15
3.4 Exercises for lab 17
3.6 Home Work: 18
Lab # 04 18
Introduction to 2D Dimensional Array and Scanner Class 18
4.1 Objective: 18
4.2 Scope: 18
4.3 Useful Concepts: 18
4.4 Example Code 19
4.5 Exercises for lab 20
4.6 Home Work: 21
Lab # 05 21
Introduction to method overloading, parameterized constructor, recursion 21
5.1 Objective: 21
5.2 Scope: 21
5.3 Useful Concepts: 21
5.4 Example Code 21
5.5 Exercises for lab 24
5.6 Home Work: 24
Lab # 06 25
Introduction to Final keyword, inheritance, Multi –level inheritance, 25
6.1 Objective: 25
6.2 Scope: 25
6.3 Useful Concepts: 25
6.4 Example Code 25
6.5 Exercises for lab 28
6.6 Home Work: 29
Lab # 07 29
Aggregation, Composition Implementation 29
7.1 Objective: 29
7.2 Scope: 29
7.3 Useful Concepts: 29
7.4 Example Code 31
7.5 Exercises for lab 32
7.6 Home Work: 33
Lab # 08 33
Abstract Classes and Interfaces 33
8.1 Objective: 33
8.2 Scope: 33
8.3 Useful Concepts: 33
8.4 Example Code 34
8.5 Exercises for lab 37
8.6 Home Work: 37
Lab # 09 38
Exception Handling (Checked, Un- Checked And Errors) 38
9.1 Objective: 38
9.2 Scope: 38
9.3 Useful Concepts: 38
1) Checked Exception 38
2) Unchecked Exception 38
3) Error 38
9.4 Code Examples: 40
9.5 Exercises for lab 42
9.6 Home Work: 43
Lab # 10 File Handling 43
10.1 Objective: 43
10.2 Scope: 43
10.3 Examples Codes 43
10.4 Exercises for lab 45
10.6 Home Work: 46
Lab # 11 Multi-Threading 46
11.1 Objective: 46
11.2 Scope: 46
11.3 Useful Concepts: 46
11.4 Code 47
11.5 Exercises for lab 48
11.6 Home Work: 48
Lab # 12 Introduction to Java Swing 49
11.1 Objective: 49
11.2 Scope: 49
11.3 Useful Concepts: 49
11.4 Code and Output 50
12.5 Exercises for lab 54
12.6 Home Work: 55
Lab # 01
1.1 Objective:
1. Learn basic concepts of Java Programming Language.
2. Learn difference of JDK,JRE .
3. Learn how to setup Java Environment
1.2 Scope:
The student should know the following at the end of this lab:
1. Introduction to basic concepts required before Java Programming
2. Basics of Java Environment Setup.
3. Configuration of Java Programming Environment
Java Introduction:
Java works on different platforms (Windows, Mac, Mobile App, and Web.)
https://www.guru99.com/difference-between-jdk-jre-
jvm.html#:~:text=JDK%20is%20a%20software%20development%20kit%20whereas%20JRE%20is%20a
,an%20environment%20for%20executing%20bytecode.&text=JDK%20is%20platform%20dependent%2
C%20JRE,for%20developing%2C%20debugging%2C%20etc.
JAVA installation:
https://www.journaldev.com/476/java-windows-10-download-install#java-windows-10-installation-
steps
1. javac HelloWorld.java
2. java HelloWorld
1.5 Exercise
1 Reverse Pyramid
Install java into your laptop and take screenshot with your mac address and mail me
Lab Manual: Java Software Development Paradigm
Lab # 02
2.1 Objective:
To familiarize the students with manipulation with Logical, Relational and Loops in java
2.2 Scope:
The student should know the following at the end of this lab:
1. Become familiar with Logical operators and their usage
2. Become familiar with Relational operators and their usage
3. Become familiar with Loops operators and their usage.
2.3 Useful Concepts:
Do while loop
For loop
2.4 Examples
Lab # 03
3.1 Objective:
To familiarize the students with the Primitive data types, switch cases and Functions concepts
3.2 Scope:
The student should know the following at the end of this lab:
1. Become familiar with difference between primitive and non-primitive.
2. Usage of switch
3. Implementation of functions.
3.3 Useful Concepts and Code:
Data Types
String Operations
Switch
Functions calling
Exercise 3.4.2:
Declare a function that takes two arguments size, after calling it should draw reverse pyramid
equal to the number passed in function.
Lab # 04
4.1 Objective:
To familiarize the students with the usage of two dimensional array and Scanner class basic usage
4.2 Scope:
The student should know the following at the end of this lab:
1. Become familiar with basic usage of 2D Dimensional Array.
2. Taking input through console.
3 usage of netbeans
4.3 Useful Concepts:
2D-arrray
Scanner is a class in java.util package used for obtaining the input of the primitive types like int, double,
etc. and strings. It is the easiest way to read input in a Java program, though not very efficient if you want
an input method for scenarios where time is a constraint like in competitive programming.
1 5 6
5 8 8
9 4 7
Exercise 4.5.2:
Write a program to declare 2D array and store above elements in 2D array using scanner
class.
Department of Computer Science & IT, The University of Lahore, Lahore. 20
Lab Manual: Java Software Development Paradigm
Lab # 05
5.1 Objective:
To familiarize the students with the usage of method overloading, parameterized constructor,
recursion
5.2 Scope:
The student should know the following at the end of this lab:
1. Apply recursion in java
2. Override methods in java.
3. Use parameterized constructor.
5.3 Useful Concepts:
What is Recursion?
The process in which a function calls itself directly or indirectly is called recursion and the
corresponding function is called as recursive function. Using recursive algorithm, certain problems can
be solved quite easily.
In Java, two or more methods can have same name if they differ in parameters (different number of
parameters, different types of parameters, or both). These methods are called overloaded methods and
this feature is called method overloading. For example: ... These methods have the same name but
accept different arguments.
The parameterized constructors are the constructors having a specific number of arguments to be
passed. The purpose of a parameterized constructor is to assign user-wanted specific values to the
instance variables of different objects. A parameterized constructor is written explicitly by a
programmer.
5.4 Example Code
Lab # 06
6.1 Objective:
To familiarize the students with the usage of Final keyword, Implementation of inheritance and Multi
level inheritance
6.2 Scope:
The student should know the following at the end of this lab:
1. Learn usage of Final keyword.
2. Establish concept of inheritance in java.
6.3 Useful Concepts:
1 In Java, the final keyword can be used while declaring an entity. Using the final
keyword means that the value can't be modified in the future. This entity can be - but is not
limited to - a variable, a class or a method.
2 Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a
parent object. It is an important part of OOPs (Object Oriented programming system).
The idea behind inheritance in Java is that you can create new classes that are built upon existing classes.
When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover,
you can add new methods and fields in your current class also.
Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
3 When a class extends a class, which extends anther class then this is called multilevel inheritance. For
example class C extends class B and class B extends class A then this type of inheritance is known
as multilevel inheritance.
Lab # 07
7.1 Objective:
To familiarize the students with Aggregation and Composition difference and implementation.
7.2 Scope:
The student should know the following at the end of this lab:
1. Learn difference between Aggregation and Composition.
2. Establish capability to implement aggregation and composition.
7.3 Useful Concepts:
Aggregation
It is a unidirectional association i.e. a one way relationship. For example, department can have students
but vice versa is not possible and thus unidirectional in nature.
In Aggregation, both the entries can survive individually which means ending one entity will not effect
the other entity
Composition
Composition is a restricted form of Aggregation in which two entities are highly dependent on each other.
It represents part-of relationship.
In composition, both the entities are dependent on each other.
When there is a composition between two entities, the composed object cannot exist without the
other entity.
Q1
Write a program in java to display basic operations of aggregation. There should be Four Classes
Workshop, Car, Tyre, and Engine. User can request in Workshop class to develop Car for them. In car
class there should be a method named as developeCar. It should have instances of Tyre and Engine. User
can request for two types of car (Sports and Family). Sports should have expensive engine and Family
car should have cheap one. Cars can have either four or three tyres.
Department of Computer Science & IT, The University of Lahore, Lahore. 32
Lab Manual: Java Software Development Paradigm
Q2
Write a program in java to illustrate example of aggregation, the program should take input three inputs
from user, two operands and one operator and display their result. For example if user enters 3, 5 and +
then result should be 8 and program should also tell result is even or odd. There should be a Calculation
class having instance of Multiplication, Subtraction, Addition and Division.
Lab # 08
8.1 Objective:
To familiarize the students with the concept of Abstract /Interfaces ways to implement it.
8.2 Scope:
The student should know the following at the end of this lab:
1. Understand concept of Abstract and Interfaces.
2. Learn how to implement Abstract and Interfaces.
8.3 Useful Concepts:
Abstract Classes
A class that is declared using “abstract” keyword is known as abstract class. It can have abstract
methods (methods without body) as well as concrete methods (regular methods with body). A normal
class (non-abstract class) cannot have abstract methods. In this guide we will learn what is an abstract
class, why we use it and what are the rules that we must remember while working with it in Java.
An abstract class cannot be instantiated, which means you are not allowed to create an object of it.
So when we know that all the animal child classes will and should override this method, then there is no
point to implement this method in parent class. Thus, making this method abstract would be the good
choice as by making this method abstract we force all the sub classes to implement this method(
otherwise you will get compilation error), also we need not to give any implementation to this method in
parent class.
Exercise 8.5.2:
Write a program in java to illustrate example of interface implementation through arithmetic operations.
The program should take input three inputs from user, two operands and one operator and display their
result. For example if user enters 3, 5 and + then result should be 8 and program should also tell result is
prime or not. There should be a Calculation class and four interfaces named as Multiplication,
Subtraction, Addition and Division. There should be methods in interfaces and Calculation class should
implement all of them and define them.
8.6 Home Work:
Write down why java allows multiple interfaces to be implemented not multiple classes to inherited?
Lab # 09
9.1 Objective:
To familiarize the students with the concept of Exception handling and its implementation
9.2 Scope:
The student should know the following at the end of this lab:
1. Learn concept of join Exception handling and different types of it
2. Learn how to implement Exceptions.
1) Checked Exception
The classes which directly inherit Throwable class except RuntimeException and Error are known as
checked exceptions e.g. IOException, SQLException etc. Checked exceptions are checked at compile-
time.
2) Unchecked Exception
The classes which inherit RuntimeException are known as unchecked exceptions e.g.
ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked
exceptions are not checked at compile-time, but they are checked at runtime.
3) Error
try The "try" keyword is used to specify a block where we should place exception code.
The try block must be followed by either catch or finally. It means, we can't use try block alone.
catch The "catch" block is used to handle the exception. It must be preceded by
try block which means we can't use catch block alone. It can be followed by finally block later.
finally The "finally" block is used to execute the important code of the program.
throws The "throws" keyword is used to declare exceptions. It doesn't throw an exception.
Type of exception With throw keyword we On other hand with throws keyword
can propagate only both checked and unchecked
unchecked exception i.e exceptions can be declared and for the
2
checked exception cannot propagation checked exception must
be propagated using throw. use throws keyword followed by
specific exception class name.
Write a program that declares two integer arrays having name Array_1 and Array_2. Take input from user
in both arrays in such a way that first Array_1 accepts even number and Second Array_2 accepts odd
number. After taking input in both arrays, loop through both arrays to sum up all values in present in both
arrays. Apply Exception handling for Array of Bound exception and throw exception such that user
cannot enter value greater than 100.
Write a program to calculate daily wage of an employee. Formula for calculating daily wage =
MonthlySalary / 30. The program should take monthly salary as an input from user (employee). Write a
logic for exception handling such that user should be displayed Exception if he enters Salary below zero,
string. If Exception occurs force user to enter salary again until appropriate value is entered.
10.1 Objective:
To familiarize the students with the concept and constraints of File Handling.
10.2 Scope:
The student should know the following at the end of this lab:
1. Learn concept of File handling and their different operations
2. Learn how to implement and re use file handling built in Functions.
Lab # 11 Multi-Threading
11.1 Objective:
To familiarize the students with the concept of Multi-Threading and its implementation.
11.2 Scope:
The student should know the following at the end of this lab:
1. Learn concept of Multi-Threading.
2. Implementation of Java Thread.
However, we use multithreading than multiprocessing because threads use a shared memory area. They
don't allocate separate memory area so saves memory, and context-switching between the threads takes
less time than process.
We create a class that extends the java.lang.Thread class. This class overrides the run() method
available in the Thread class. A thread begins its life inside run() method. We create an object of our
new class and call start() method to start the execution of a thread. Start() invokes the run() method on
the Thread object.
We create a new class which implements java.lang.Runnable interface and override run() method. Then
we instantiate a Thread object and call start() method on this object.
2. We can achieve basic functionality of a thread by extending Thread class because it provides some
inbuilt methods like yield(), interrupt() etc. that are not available in Runnable interface.
11.4 Code
Exercise 11.5.1:
Task 1
Write a Program that finds largest integer from an array. The Program should declare an array =
{1,2,5,8,9,10}. After it Function should create three threads and search for largest integer, when integer is
found , it should destroy all threads and return that integer. First thread should start traversing array from
0 index, Second Thread should start traversing from 1 index and third index should start from 2 index.
Task 2
Write a function that should display your name. The Program should declare four threads that
displays your name upto 100 times. When your name is displayed 1000 th time. All Thread
should be stopped. One thread should not display name more than 250 times.
11.1 Objective:
To familiarize the students with the concept of Creating basic GUI interfaces with java.
11.2 Scope:
The student should know the following at the end of this lab:
1. Learn concept of GUI creation with Java Swing.
2. Creation of Interfaces and Action listener on to them.
Swing API is a set of extensible GUI Components to ease the developer's life to create JAVA
based Front End/GUI Applications. It is built on top of AWT API and acts as a replacement of
AWT API, since it has almost every control corresponding to AWT controls. Swing component
follows a Model-View-Controller architecture to fulfill the following criteria.
A single API is to be sufficient to support multiple look and feel.
API is to be model driven so that the highest level API is not required to have data.
API is to use the Java Bean model so that Builder Tools and IDE can provide better
services to the developers for use.
Department of Computer Science & IT, The University of Lahore, Lahore. 49
Lab Manual: Java Software Development Paradigm
Swing Features
Light Weight − Swing components are independent of native Operating System's API as Swing
API controls are rendered mostly using pure JAVA code instead of underlying operating system
calls.
Rich Controls − Swing provides a rich set of advanced controls like Tree, TabbedPane, slider,
colorpicker, and table controls.
Highly Customizable − Swing controls can be customized in a very easy way as visual
apperance is independent of internal representation.
Pluggable look-and-feel − SWING based GUI Application look and feel can be changed at run-
time, based on available values.
Output :
Output
Click
Implement GUI by considering image above, user should be given functionality to add text and
after clicking button. Number of a’s in text should be displayed.