0% found this document useful (0 votes)
8 views48 pages

Java Programming Lab Exercises Guide

The document outlines a series of laboratory exercises for a B.Tech. program in Computer Science and Engineering, focusing on Java programming and various concepts such as classes, objects, inheritance, exception handling, and event-driven programming. Each laboratory includes an introduction, aims, objectives, experimental procedures, and questions for students to solve, aimed at enhancing their programming skills. The exercises utilize tools like NetBeans IDE and cover practical applications such as online shopping systems and GUI applications using Swing.

Uploaded by

washiqueahmed11
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views48 pages

Java Programming Lab Exercises Guide

The document outlines a series of laboratory exercises for a B.Tech. program in Computer Science and Engineering, focusing on Java programming and various concepts such as classes, objects, inheritance, exception handling, and event-driven programming. Each laboratory includes an introduction, aims, objectives, experimental procedures, and questions for students to solve, aimed at enhancing their programming skills. The exercises utilize tools like NetBeans IDE and cover practical applications such as online shopping systems and GUI applications using Swing.

Uploaded by

washiqueahmed11
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Name: ____________________________ Registration Number:_____________________


Programming Paradigms Laboratory
[Link]. 3rd Semester

Name​ ​ ​ : SHIVARADDI . A .V
Roll Number​​ : 24ETCS002175
Programme​ ​ : BTECH
Department​​ : Computer Science and Engineering

Faculty of Engineering & Technology


Ramaiah University of Applied Sciences

1
Name: ____________________________ Registration Number:_____________________

Faculty Engineering & Technology

Programme B. Tech. in CSE

Year/Semester 2nd Year /3rd Semester


Name of the Laboratory Programming Paradigms Laboratory
Laboratory Code CSD207B

​ ​ ​ ​ ​ ​ ​

2
Name: ____________________________ Registration Number:_____________________

Laboratory 1
Title of the Laboratory Exercise: Introduction to Java programming environment
1.​ Introduction and Purpose of Experiment
Java is a high level, robust, object-oriented and secure programming language. NetBeans IDE
is an open source integrated development environment for the Java programming language.
In this laboratory exercise, students get familiar with the program development tool such as
NetBeans IDE to edit and run Java programs using a set of simple exercises.

2.​ Aim and Objectives


Aim
●​ To familiarise IDE and simple Java programs
Objectives
At the end of this lab, the student will be able to
●​ Explain the features and use of IDE to develop Java programs
●​ Edit and execute simple Java programs

3.​ Experimental Procedure


Students are given a set of Java programs. Edit and execute programs using NetBeans IDE.
4.​ Questions

a.​ Program to print HelloWorld message.

b.​ Program to accept input from user and print it.

c.​ Program to find area of circle by taking input from user side.

d.​ Online Shopping System: Where Users can browse products, add them to cart and
calculate bills

5.​ Calculations/Computations/Algorithms

3
Name: ____________________________ Registration Number:_____________________

a.​ Program to print HelloWorld message.

b.​ Program to accept input from user and print it.

c.​ Program to find area of circle by taking input from user side.

4
Name: ____________________________ Registration Number:_____________________

d.​ Online Shopping System: Where Users can browse products, add them to cart and
calculate bills

5
Name: ____________________________ Registration Number:_____________________

6.​ Presentation of Results

a.

b.

c.

d.

6
Name: ____________________________ Registration Number:_____________________

7
Name: ____________________________ Registration Number:_____________________

Laboratory 2
Title of the Laboratory Exercise: Classes and Objects, Control Statements, Looping Statements
1.​ Introduction and Purpose of Experiment
The class represents a group of objects having similar properties and behaviour. It is a
user-defined blueprint or prototype from which objects are created. Objects are the
instances of a class that are created to use the attributes and methods of a class. For
example, Student is a class while a particular student named Ravi is an object. By solving the
problems, students will be able to develop programs using classes and objects.

2.​ Aim and Objectives


Aim
●​ To develop programs using Classes and Objects
Objectives
●​ At the end of this lab, the student will be able to define a class and object
3.​ Experimental Procedure
i.​ Analyse the problem statement
ii.​ Design an algorithm for the given problem statement
iii.​ Implement the algorithm in Java language
iv.​ Execute the Java program
v.​ Test the implemented program
vi.​ Document the Results
vii.​ Analyse and discuss the outcomes of the experiment

4.​ Questions

a.​ Develop a Java program to reverse the input number using for and while loop.
b.​ Online Shopping System: Create a class Product with attributes product name, price,
quantity and functions inputProductDetaila() where Users can browse products, add
them to cart and getTotal() to calculate bills.

5.​ Calculations/Computations/Algorithms

8
Name: ____________________________ Registration Number:_____________________

Develop a Java program to reverse the input number using for and while loop.

Online Shopping System: Create a class Product with attributes product name, price,
quantity and functions inputProductDetaila() where Users can browse products,
add them to cart and getTotal() to calculate bills.

9
Name: ____________________________ Registration Number:_____________________

c.​ Presentation of Results

a.

10
Name: ____________________________ Registration Number:_____________________

b.

11
Name: ____________________________ Registration Number:_____________________

Laboratory 3
Title of the Laboratory Exercise: Functions and constructors
6.​ Introduction and Purpose of Experiment
The class represents a group of objects having similar properties and behaviour. It is a
user-defined blueprint or prototype from which objects are created. Objects are the instances
of a class that are created to use the attributes and methods of a class. Functions (also called
methods) are blocks of code that perform specific tasks. Constructor is a special type of
method that is used to initialize the object. Constructor is invoked at the time of object
creation.

7.​ Aim and Objectives


Aim
●​ To develop programs using functions and constructors
Objectives
●​ At the end of this lab, the student will be able to define a class, object, function and
constructors
8.​ Experimental Procedure
viii.​ Analyse the problem statement
ix.​ Design an algorithm for the given problem statement
x.​ Implement the algorithm in Java language
xi.​ Execute the Java program
xii.​ Test the implemented program
xiii.​ Document the Results
xiv.​ Analyse and discuss the outcomes of the experiment

9.​ Questions

d.​ Develop a java program to Create a class called Employee that includes three instance
variables—a first name (type String), a last name (type String) and a monthly salary
(double). Define a constructor that initializes the three instance variables, define
functions setInfo() and a getInfo() which takes the salary, number of hours of work per
day of employee as parameter method. If the monthly salary is not positive, do not set
its value. Create two Employee objects and display each object’s yearly salary. Then give
each Employee a 10% raise and display each Employee’s yearly salary again.

e.​ Online Shopping System: Create a class Product with instance variables product name,
price and quantity. Initialize instance variables using constructor with parameters and
define functions inputProductDetaila() where Users can browse products, add them to
cart and getTotal() to calculate bills.

12
Name: ____________________________ Registration Number:_____________________

10.​Calculations/Computations/Algorithms

1.​ Develop a java program to Create a class called Employee that includes three instance
variables—a first name (type String), a last name (type String) and a monthly salary
(double). Define a constructor that initializes the three instance variables, define
functions setInfo() and a getInfo() which takes the salary, number of hours of work per
day of employee as parameter method. If the monthly salary is not positive, do not set
its value. Create two Employee objects and display each object’s yearly salary. Then give
each Employee a 10% raise and display each Employee’s yearly salary again.

13
Name: ____________________________ Registration Number:_____________________

2.​ Online Shopping System: Create a class Product with instance variables product name,
price and quantity. Initialize instance variables using constructor with parameters and
define functions inputProductDetaila() where Users can browse products, add them to
cart and getTotal() to calculate

14
Name: ____________________________ Registration Number:_____________________

15
Name: ____________________________ Registration Number:_____________________

3.​ Presentation of Results

a.

16
Name: ____________________________ Registration Number:_____________________

b.

17
Name: ____________________________ Registration Number:_____________________

Laboratory 4
Title of the Laboratory Exercise: Inheritance, Abstract Method, Encapsulation, Access Modifiers
11.​Introduction and Purpose of Experiment
Inheritance is one of the key features of OOP that allows us to create a new class from an
existing class. In Java, inheritance is an is-a relationship. Abstraction is a process of hiding the
implementation details and showing only functionality to the user. Abstract class can have
abstract and non-abstract methods. It needs to be extended and its method implemented in
subclass. It cannot be instantiated. Wrapping of data and method together into a single
unit is known as Encapsulation.
12.​ Aim and Objectives
Aim
●​ To develop programs using Inheritance and Abstract Method
Objectives
●​ At the end of this lab, the student will be able to define a classes and subclasses,
abstract classes and abstract methods.
13.​Experimental Procedure
xv.​ Analyse the problem statement
xvi.​ Design an algorithm for the given problem statement
xvii.​ Implement the algorithm in Java language
xviii.​ Execute the Java program
xix.​ Test the implemented program
xx.​ Document the Results
xxi.​ Analyse and discuss the outcomes of the experiment

14.​Questions

f.​ Develop a Java program to create a class shape. Let rectangle and triangle inherit this
shape class. Implement the respective methods to calculate the area and perimeter of
each shape.

g.​ Develop a Java program to create an abstract class Employee with abstract methods
calculateSalary() and displayInfo(). Create subclasses Manager and Programmer that
extend the Employee class and implement the respective methods to calculate salary
and display information for each role.

15.​Calculations/Computations/Algorithms

18
Name: ____________________________ Registration Number:_____________________

Algorithm:

a.

Step1: Start
Step2: Define abstract class Shape with abstract methods area() and perimeter()
Step3: Create Rectangle class inheriting Shape and implement area and perimeter
Step4: Create Triangle class inheriting Shape and implement area and perimeter
Step5: In main method, create objects of Rectangle and Triangle
Step6: Call area() and perimeter() for each object
Step7: Display the results
Step8: End

b.
Step1: Start
Step2: Define an abstract class Employee with abstract methods calculateSalary() and displayInfo()
Step3: Create a Manager subclass that extends Employee and implement calculateSalary() and
displayInfo()
Step4: Create a Programmer subclass that extends Employee and implement calculateSalary() and
displayInfo()
Step5: In the main method, create objects of Manager and Programmer
Step6: Call calculateSalary() and displayInfo() for each object
Step7: Display the salary and details of each employee
Step8: End

19
Name: ____________________________ Registration Number:_____________________

a.

20
Name: ____________________________ Registration Number:_____________________

b.

16.​Presentation of Results

21
Name: ____________________________ Registration Number:_____________________

a.

b.

22
Name: ____________________________ Registration Number:_____________________

Laboratory 5
Title of the Laboratory Exercise: Exception Handling
17.​Introduction and Purpose of Experiment
The Exception Handling in Java is used to handle the runtime errors like
ClassNotFoundException, IOException using try-catch blocks. By solving these problems,
students will become familiar with the implementations of Exception handling.
18.​ Aim and Objectives
Aim
●​ To handle runtime errors in Java using try-catch-finally
Objectives
●​ At the end of this lab, the student will be able to define a class, object, function and
handle run time errors
19.​Experimental Procedure
xxii.​ Analyse the problem statement
xxiii.​ Design an algorithm for the given problem statement
xxiv.​ Implement the algorithm in Java language
xxv.​ Execute the Java program
xxvi.​ Test the implemented program
xxvii.​ Document the Results
xxviii.​ Analyse and discuss the outcomes of the experiment

20.​Questions

a.​ Develop a Java program using exception handling to input five array elements through
command line and find the sum and average by throwing
ArrayIndexOutOfBoundException.

23
Name: ____________________________ Registration Number:_____________________

b.​ Develop a Java program using exception handling to throw NumberFormatException


using [Link] to find sum of two input strings by typecasting them into integers.

24
Name: ____________________________ Registration Number:_____________________

21 . presentation of results

4a output:

4b output

25
Name: ____________________________ Registration Number:_____________________

Laboratory 6
Title of the Laboratory Exercise: Event Driven Programming in Java using Swing
21.​Introduction and Purpose of Experiment
The Event-driven programming is a paradigm where the flow of the program is determined
by events such as mouse clicks, key presses, selections, or window actions. In GUI
applications, user actions trigger events, which are captured and handled using event
listeners.
22.​ Aim and Objectives
Aim
●​ To understand and implement event-driven programming concepts in Java by
creating a GUI application using Swing components and handling user events
through appropriate event listeners
Objectives
●​ To understand the concept of event-driven programming.
●​ To learn how to create GUI applications using Java Swing components.
●​ To handle user interactions through event listeners.
●​ To implement basic controls such as buttons, labels, text fields, and message dialogs.
23.​Experimental Procedure
xxix.​ Analyse the problem statement
xxx.​ Design an algorithm for the given problem statement
xxxi.​ Implement the algorithm in Java language
xxxii.​ Execute the Java program
xxxiii.​ Test the implemented program
xxxiv.​ Document the Results
xxxv.​ Analyse and discuss the outcomes of the experiment

24.​Questions

2.​ Write a Java Swing program to create a window with a button and a label. Each time the
button is clicked, increment a counter and display the updated count in the label.

26
Name: ____________________________ Registration Number:_____________________

e.​ Develop a Java Swing application to design a simple calculator with the following features:
4.​ Two text fields for entering numbers: Number 1 and Number 2
5.​ Two buttons: Add
6.​ A label to display the Result​
When the user clicks on:
7.​ Add → display the sum of the two numbers

27
Name: ____________________________ Registration Number:_____________________

28
Name: ____________________________ Registration Number:_____________________

29
Name: ____________________________ Registration Number:_____________________

25.​Presentation of Results

4a output

4.

30
Name: ____________________________ Registration Number:_____________________

31
Name: ____________________________ Registration Number:_____________________

Laboratory 7
Title of the Laboratory Exercise: Event Driven Programming in Java using Swing and AWT components
26.​Introduction and Purpose of Experiment
The Event-driven programming is a paradigm where the flow of the program is determined
by events such as mouse clicks, key presses, selections, or window actions. In GUI
applications, user actions trigger events, which are captured and handled using event
listeners.
27.​ Aim and Objectives
Aim
●​ To understand and implement event-driven programming concepts in Java by
creating a GUI application using Swing components and handling user events
through appropriate event listeners
Objectives
●​ To understand the concept of event-driven programming.
●​ To learn how to create GUI applications using Java Swing components.
●​ To handle user interactions through event listeners.
●​ To implement basic controls such as buttons, labels, text fields, and message dialogs.
28.​Experimental Procedure
xxxvi.​ Analyse the problem statement
xxxvii.​ Design an algorithm for the given problem statement
xxxviii.​ Implement the algorithm in Java language
xxxix.​ Execute the Java program
xl.​ Test the implemented program
xli.​ Document the Results
xlii.​ Analyse and discuss the outcomes of the experiment

29.​Questions

f.​ Demonstrate Event Handling based on AWT components


g.​ Demonstrate Multi Event Handling using Swing by creating sample NOTEPAD Application
h.​ Demonstrate Swing vs AWT Event handling Comparison

30.​Calculations/Computations/Algorithms

a.​ Demonstrate Event Handling based on AWT components

32
Name: ____________________________ Registration Number:_____________________

7b.

33
Name: ____________________________ Registration Number:_____________________

c.

34
Name: ____________________________ Registration Number:_____________________

35
Name: ____________________________ Registration Number:_____________________

31.​Presentation of Results

a. awt

b. swing

36
Name: ____________________________ Registration Number:_____________________

c.

37
Name: ____________________________ Registration Number:_____________________

Laboratory 8
Title of the Laboratory Exercise: functional programing using haskell
[Link] Hello World Program
a.​ algorithm
main :: IO ()
main = do
print("Hello World")
putStrLn "Hello World"

b.​ program

c.​ presentation of results

2. addition of two numbers


a.​ algorithm
main :: IO ()
main = do
putStrLn "Enter first number:"
a <- readLn
putStrLn "Enter second number:"
b <- readLn
let sum = a + b
putStrLn ("Sum = " ++ show sum)

b.​ program

38
Name: ____________________________ Registration Number:_____________________

c.​ presentation of result

3. Square Function Program (Pure Functional Way)

a.​ algorithm

square :: Int -> Int


square x = x * x

main :: IO ()
main = do
putStrLn "Enter a number:"
n <- readLn
print (square n)

b.​ program

39
Name: ____________________________ Registration Number:_____________________

c.​ results

4. Even Number Check (Calling Function Way)


a.​ algorithm

main :: IO ()

main = do

putStrLn "Enter a number:"

n <- readLn :: IO Int

if even n

then putStrLn "The number is Even"

else putStrLn "The number is Odd"

b.​ program

c.​ result

40
Name: ____________________________ Registration Number:_____________________

5. Recursion Example (Factorial of Number)

a.​ algorithm

factorial :: Int -> Int

factorial 0 = 1

factorial n = n * factorial (n - 1)

main :: IO ()

main = do

putStrLn "Enter a number:"

n <- readLn :: IO Int

putStrLn ("Factorial is: " ++ show (factorial n))

b.​ program

c.​ results

6. List comprehension is a compact way to create lists

[Link] values

[Link] conditions (filters)

41
Name: ____________________________ Registration Number:_____________________

a.​ algorithm

main :: IO ()

main = do

-- Generating values

let doubled = [x * 2 | x <- [1..5]]

putStrLn "Generated values (x * 2):"

print doubled

-- Applying conditions (filters)

let evens = [x | x <- [1..10], even x]

putStrLn "Even numbers from 1 to 10:"

print evens

b.​ program

c.​ results

42
Name: ____________________________ Registration Number:_____________________

7. Lazy Evaluation (Thunk Data Structure)

a.​ algorithm

An expression is not evaluated until its value is actually required.

ghci> let infinitelist = [1..]


ghci> take 10 infinitelist
[1,2,3,4,5,6,7,8,9,10]
b.​ program

c.​ result

8. Modularity in Functional Programming

a. algorithm

Building programs by composing small, independent functions


Each function does one simple task
Functions can be reused and combined easily
For example:

let double x = x * 2
let increment x = x + 1
let doubleWithIncrement x = increment (double x)

b. program

43
Name: ____________________________ Registration Number:_____________________

c. results

44
Name: ____________________________ Registration Number:_____________________

Laboratory 9
Title of the Laboratory Exercise: functional programing using haskell
1.​ pattern matching of grade classification
[Link]
grade :: Int -> String
grade 100 = "Outstanding"
grade m
| m >= 90 = "A+"
| m >= 80 = "A"
| m >= 70 = "B"
| m >= 60 = "C"
| m >= 40 = "D"
| otherwise = "Fail"

b. program

c. result

45
Name: ____________________________ Registration Number:_____________________

2.​ Higher-Order Functions (HOF) in Haskell


[Link]
A higher-order function is a function that:
[Link] one or more functions as arguments, or
ii. Returns a function as a result
Examples of HOF
Map Function.
ghci> let increment x =x+1
ghci> map increment [1,2,3]
[2,3,4]
ghci>

Filter Function.
ghci> :t filter
filter :: (a -> Bool) -> [a] -> [a]
ghci> let isEven n = n `mod` 2 == 0
ghci> filter isEven [5,4,3,6]
[4,6]
ghci>

Foldl and Foldr Function


ghci> foldl (+) 0 [1,2,3,4,5]
15
ghci> foldr (*) 1 [1,2,3,4,5]
120

Sort Function
ghci> import [Link] (sort)
ghci> sort [12,4,10,6,1]
[1,4,6,10,12]

zipWith Function
ghci> zipWith (+) [1,2,3] [4,5,6]
[5,7,9]
ghci> zipWith (+) [1,2,3] [4,5,6,7]
[5,7,9]

takeWhile and dropWhile Function


ghci> takeWhile (<3) [1,2,3,4,5]
[1,2]
ghci> dropWhile (<3) [1,2,3,4,5]
[3,4,5]

All and any function


ghci> all even [1,3,4,5]

46
Name: ____________________________ Registration Number:_____________________

False
ghci> any even [1,3,4,5]
True

Lambda Function
A lambda function is an anonymous function (a function without a name)
Syntax: \x -> expression

ghci> (\x -> x + 1) 55


56
ghci> (\x y -> x + y) 3 4
7
ghci> grades = [35, 60, 45, 90, 78]
ghci> doubledGrades = map (\x -> x * 2) grades
ghci> doubledGrades
[70,120,90,180,156]

b. program

47
Name: ____________________________ Registration Number:_____________________

c. results

48

You might also like