0% found this document useful (0 votes)
214 views

Java Lab Manual

This document outlines 12 labs covering key Java concepts. Lab 1 covers Java fundamentals, configuration, and installation. Lab 2 introduces logical, relational, and loop operators. Lab 3 covers primitive data types, switch cases, and functions. Lab 4 discusses 2D arrays and the Scanner class. Subsequent labs cover method overloading, inheritance, exceptions, file handling, multi-threading, and Java Swing. Each lab includes objectives, useful concepts, example code, exercises, and homework assignments.

Uploaded by

awais
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
214 views

Java Lab Manual

This document outlines 12 labs covering key Java concepts. Lab 1 covers Java fundamentals, configuration, and installation. Lab 2 introduces logical, relational, and loop operators. Lab 3 covers primitive data types, switch cases, and functions. Lab 4 discusses 2D arrays and the Scanner class. Subsequent labs cover method overloading, inheritance, exceptions, file handling, multi-threading, and Java Swing. Each lab includes objectives, useful concepts, example code, exercises, and homework assignments.

Uploaded by

awais
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

Table of Contents

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

Introduction to Java key concepts, configuration and installation

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

1.3 Useful Concepts:

Key Fundamentals Concepts:

Java Introduction:

 Java works on different platforms (Windows, Mac, Mobile App, and Web.)

 It is one of the most popular programming language in the world

 It is open-source and free

 It has a huge community support (tens of millions of developers)

 Java is an object oriented language.

 Java is platform independent.

Java Platform Independent:


Difference between JDK and JRE:

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

Check installation of java:

Check installation of javac:


Set Path of javac
SET PATH=C:\Program Files\Java\jdk1.8.0_181\bin
Compilation program with javac and java command

1. javac HelloWorld.java

2. java HelloWorld

Compilation program with javac and java command with arguments

1.4 Example code


 

1.5 Exercise

Implement Following output code given below

1 Reverse Pyramid

2 using Nested For loop

3 Single Print statement


 

1.6 Home Work

Install java into your laptop and take screenshot with your mac address and mail me
Lab Manual: Java Software Development Paradigm

Lab # 02

Introduction to Logical ,Relational and loops in java

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

Department of Computer Science & IT, The University of Lahore, Lahore. 10


Lab Manual: Java Software Development Paradigm

For loop

Department of Computer Science & IT, The University of Lahore, Lahore. 11


Lab Manual: Java Software Development Paradigm

Department of Computer Science & IT, The University of Lahore, Lahore. 12


Lab Manual: Java Software Development Paradigm

2.4 Examples

Example 2.4.1: Demonstration of java code for lab commands.

Department of Computer Science & IT, The University of Lahore, Lahore. 13


Lab Manual: Java Software Development Paradigm

2.5 Exercises for lab


Exercise 2.1:
Implement linear search on this array ={1,9,7,0}. To count numbers greater than 0
Exercise 2.2:
Find all odd, even number from this array ={1,9,7,0}.and display them.
Exercise 2.3:
Apply do while loop on this array ={1,9,7,0} to count all elements.

2.6 Home Work:


Try to run .class code generated in window to another operating system with cmds .

Lab # 03

Introduction to Primitive data types, switch cases and Functions

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.

Department of Computer Science & IT, The University of Lahore, Lahore. 14


Lab Manual: Java Software Development Paradigm

2. Usage of switch
3. Implementation of functions.
3.3 Useful Concepts and Code:

Data Types

Department of Computer Science & IT, The University of Lahore, Lahore. 15


Lab Manual: Java Software Development Paradigm

String Operations

Switch

Department of Computer Science & IT, The University of Lahore, Lahore. 16


Lab Manual: Java Software Development Paradigm

Functions calling

3.4 Exercises for lab


Exercise 3.4.1:
Declare a function that takes two arguments first and last name, after calling it should join both
arguments and show their length with string built int operations.

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.

Department of Computer Science & IT, The University of Lahore, Lahore. 17


Lab Manual: Java Software Development Paradigm

3.6 Home Work:


Study difference between static and non –static .

Lab # 04

Introduction to 2D Dimensional Array and Scanner Class

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 (Java User input)

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.

Department of Computer Science & IT, The University of Lahore, Lahore. 18


Lab Manual: Java Software Development Paradigm

4.4 Example Code

Example 4.4.1: Demonstration of 2D array code

Example 4.4.2: Demonstration of Scanner Class Code

Department of Computer Science & IT, The University of Lahore, Lahore. 19


Lab Manual: Java Software Development Paradigm

4.5 Exercises for lab


Exercise 4.5.1:
Write a program to declare 2D array having following elements. Sum up elements of
diagonal which are even and display result. Result for following array will be 14

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

4.6 Home Work:


Practise Program for declaring multi-dimensional array.
Find difference between implicit and explicit casting.

Lab # 05

Introduction to method overloading, parameterized constructor,


recursion

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

Example 5.4.1: Demonstration of recursion code.

Department of Computer Science & IT, The University of Lahore, Lahore. 21


Lab Manual: Java Software Development Paradigm

Example 5.4.2: Demonstration of method overloading.

Department of Computer Science & IT, The University of Lahore, Lahore. 22


Lab Manual: Java Software Development Paradigm

Example 5.4.2: Demonstration of parameterized constructor.

Department of Computer Science & IT, The University of Lahore, Lahore. 23


Lab Manual: Java Software Development Paradigm

5.5 Exercises for lab


Exercise 5.5.1:
Q1 Define a function named as average, if two integers variables are passed then display their sum. If
three variables are passed then display their result after subtracting them .If four variables are passed then
check if fourth is greater than 2 then display sum of first three variable else multiply all of them and show
their result.
Q2 Define a class named as MyResult. Define two constructors. First constructor took your name
and cgpa . store them in variable and show result . Second constructor took your name , cgpa and
credit hours store them in variables and display your result with credit hours details also.
Q3 Write a java function that took input from user and sum them in a variable then user enters 0.
After user enter 0 then show result

5.6 Home Work:


Write a java function named as showMyName , having two variables first integer type named as
count and second string type named as name . Write logic with recursion in which when you call
showMyName .Then your name will displayed multiple times according to amount of count
variable.

Lab # 06

Introduction to Final keyword, inheritance, Multi –level inheritance,

Department of Computer Science & IT, The University of Lahore, Lahore. 24


Lab Manual: Java Software Development Paradigm

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.

6.4 Example Code

Example 6.4.1: Demonstration of Final keyword.

Department of Computer Science & IT, The University of Lahore, Lahore. 25


Lab Manual: Java Software Development Paradigm

Department of Computer Science & IT, The University of Lahore, Lahore. 26


Lab Manual: Java Software Development Paradigm

Example 6.4.2: Demonstration of Inheritance implementation.

Example 6.4.3: Demonstration of Multi –level inheritance implementation

Department of Computer Science & IT, The University of Lahore, Lahore. 27


Lab Manual: Java Software Development Paradigm

6.5 Exercises for lab


Exercise 6.5.1:
Read out below uml diagram carefully. And implement this two classes, along with their methods and
return types in java.

Department of Computer Science & IT, The University of Lahore, Lahore. 28


Lab Manual: Java Software Development Paradigm

6.6 Home Work:


Read out difference between association, aggregation and composition. And study why java does not
allow multiple inheritance to implement.

Lab # 07

Aggregation, Composition Implementation

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 special form of Association where:

Department of Computer Science & IT, The University of Lahore, Lahore. 29


Lab Manual: Java Software Development Paradigm

It represents Has-A relationship.

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.

Department of Computer Science & IT, The University of Lahore, Lahore. 30


Lab Manual: Java Software Development Paradigm

7.4 Example Code

Example 7.4.1: Demonstration of Composition and Aggregation

Department of Computer Science & IT, The University of Lahore, Lahore. 31


Lab Manual: Java Software Development Paradigm

7.5 Exercises for lab

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.

7.6 Home Work:


Read out the concept of abstract classes and interfaces in java.

Lab # 08

Abstract Classes and Interfaces

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.

An abstract class is like a template.

Department of Computer Science & IT, The University of Lahore, Lahore. 33


Lab Manual: Java Software Development Paradigm

Why we need an abstract class?


Lets say we have a class Animal that has a method sound() and the subclasses of it
like Dog, Lion, Horse, Cat etc. Since the animal sound differs from one animal to another, there is no
point to implement this method in parent class. This is because every child class must override this
method to give its own implementation details, like Lion class will say “Roar” in this method
and Dog class will say “Woof”.

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.

What is an interface in Java?


Interface looks like a class but it is not a class. An interface can have methods and variables just like the
class but the methods declared in interface are by default abstract (only method signature, no body.
Also, the variables declared in an interface are public, static & final by default.

What is the use of interface in Java?


Since methods in interfaces do not have body, they have to be implemented by the class before you can
access them. The class that implements interface must implement all the methods of that interface. Also,
java programming language does not allow you to extend more than one class, however you can
implement more than one interfaces in your class.

8.4 Example Code

Example 8.4.1: Demonstration of Abstract Classes.

Department of Computer Science & IT, The University of Lahore, Lahore. 34


Lab Manual: Java Software Development Paradigm

Example 8.4.2: Demonstration of Interfaces.

Department of Computer Science & IT, The University of Lahore, Lahore. 35


Lab Manual: Java Software Development Paradigm

Department of Computer Science & IT, The University of Lahore, Lahore. 36


Lab Manual: Java Software Development Paradigm

8.5 Exercises for lab


Exercise 8.5.1:
Write a Java Program For VehicleFactory(Abstract class) . A Factory which can create three types of
Vehicles 1 Car 2 Bike 3Truck. There should be an abstract method named as createVehicle(). That should
be defined by all three Vehicle classes. The Program should take vehicle name as an Input from user and
create object of vehicle according to the input of the user and display Vehicle status.

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

Exception Handling (Checked, Un- Checked And Errors)

Department of Computer Science & IT, The University of Lahore, Lahore. 37


Lab Manual: Java Software Development Paradigm

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.

9.3 Useful Concepts:


Exceptions Handling

 Exception is an abnormal condition.


 The core advantage of exception handling is to maintain the normal flow of the application

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

Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.

Department of Computer Science & IT, The University of Lahore, Lahore. 38


Lab Manual: Java Software Development Paradigm

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.

It is executed whether an exception is handled or not.

throw The "throw" keyword is used to throw an exception.

throws The "throws" keyword is used to declare exceptions. It doesn't throw an exception.

It specifies that there may occur an exception in the method.

It is always used with method signature.

Department of Computer Science & IT, The University of Lahore, Lahore. 39


Lab Manual: Java Software Development Paradigm

Difference between Throw and Throws Keyword

Sr. Key throw throws


No.

Definition Throw is a keyword which Throws is a keyword used in the


is used to throw an method signature used to declare an
1 exception explicitly in the exception which might get thrown by
program inside a function the function while executing the code.
or inside a block of code.

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.

Syntax Syntax wise throw On other hand syntax wise throws


3 keyword is followed by the keyword is followed by exception class
instance variable. names.

Declaration In order to use throw On other hand throws keyword is used


keyword we should know with the method signature.
4
that throw keyword is used
within the method.

9.4 Code Examples:

Demonstration of code (Exception Handling)

Department of Computer Science & IT, The University of Lahore, Lahore. 40


Lab Manual: Java Software Development Paradigm

Department of Computer Science & IT, The University of Lahore, Lahore. 41


Lab Manual: Java Software Development Paradigm

Department of Computer Science & IT, The University of Lahore, Lahore. 42


Lab Manual: Java Software Development Paradigm

9.5 Exercises for lab


Exercise 9.5.1:

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.

9.6 Home Work:

What is difference between Throw and Throws Keyword?

What is finally keyword, is it necessary to define it?

What difference is between checked and UN checked exception?

Lab # 10 File Handling

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.

10.3 Examples Codes

Demonstration of file creation

Department of Computer Science & IT, The University of Lahore, Lahore. 43


Lab Manual: Java Software Development Paradigm

Demonstration of file class built in methods

Demonstration of file writing

Department of Computer Science & IT, The University of Lahore, Lahore. 44


Lab Manual: Java Software Development Paradigm

Demonstration of file appending

Demonstration of file reading

Department of Computer Science & IT, The University of Lahore, Lahore. 45


Lab Manual: Java Software Development Paradigm

10.4 Exercises for lab


Exercise 10.5.1:
Task 1
Write a Program that creates directory named by your rollNumber using function of mkdir() .
Create a file named as Task1 into it and add following content into it “1 3 6 9 0 7” into it. After it the
program should display how many odd and prime numbers are there in the file.
Task 2
Write a program that creates a file named as Task2 and stores your name into it. After storing your name
the program should read you’re named and tell if your name is palindrome or not and how many
characters your name has.

10.6 Home Work:


Write a program that shows allows user to enter four integer digits, 1, 2, 3 and 4. If user enters 1
then took input in the form of string and allow him to create a directory having named as entered
by user. If user enters 2 then took input in the form of string and create a file named as entered
by user. If input is 3 then took input in the form of string and store it in the file created. If input is
4 then delete the file.

Lab # 11 Multi-Threading

Department of Computer Science & IT, The University of Lahore, Lahore. 46


Lab Manual: Java Software Development Paradigm

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.

11.3 Useful Concepts:

Multithreading in Java is a process of executing multiple threads simultaneously.

A thread is a lightweight sub-process, the smallest unit of processing. Multiprocessing and


multithreading, both are used to achieve multitasking.

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.

Threads can be created by using two mechanisms:


1. extending the Thread class
2. Implementing the Runnable Interface

Thread creation by extending the Thread class

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.

Thread creation by implementing the Runnable Interface

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.

Thread Class vs Runnable Interface


1. If we extend the Thread class, our class cannot extend any other class because Java doesn’t support
multiple inheritance. But, if we implement the Runnable interface, our class can still extend other base
classes.

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

Department of Computer Science & IT, The University of Lahore, Lahore. 47


Lab Manual: Java Software Development Paradigm

11.5 Exercises for lab


Department of Computer Science & IT, The University of Lahore, Lahore. 48
Lab Manual: Java Software Development Paradigm

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.6 Home Work:


Read difference between awt and spring in javafx.

Lab # 12 Introduction to Java Swing

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.

11.3 Useful Concepts:

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.

11.4 Code and Output

Demonstration of Java swing in main method

Output :

Department of Computer Science & IT, The University of Lahore, Lahore. 50


Lab Manual: Java Software Development Paradigm

Demonstration of Java swing by association

Department of Computer Science & IT, The University of Lahore, Lahore. 51


Lab Manual: Java Software Development Paradigm

Demonstration of Java swing by inheritance

Department of Computer Science & IT, The University of Lahore, Lahore. 52


Lab Manual: Java Software Development Paradigm

Demonstration of Action listener example

Department of Computer Science & IT, The University of Lahore, Lahore. 53


Lab Manual: Java Software Development Paradigm

Output

Department of Computer Science & IT, The University of Lahore, Lahore. 54


Lab Manual: Java Software Development Paradigm

12.5 Exercises for lab


Exercise 12.5.1:

Count Letter “a” in your Name

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.

Department of Computer Science & IT, The University of Lahore, Lahore. 55


Lab Manual: Java Software Development Paradigm

12.6 Home Work:


Alter today’s exercise task, in such a way that there should a colour picker button, which can provide user
to change background colour.

Department of Computer Science & IT, The University of Lahore, Lahore. 56

You might also like