PLD Lab Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

MGM’s

Jawaharlal Nehru Engineering College Aurangabad

MGM University, Aurangabad

Department of Computer Science & Engineering

LAB MANUAL

Program (UG/PG) :UG

Year : Second Year

Semester :III

Course Code :20UCS308L

Course Title :Programming Language Design Lab

Prepared By :Ms. A. R. Tungar

Department of Computer Science & Engineering


2021-22
FOREWARD

It is a great pleasure to present this laboratory manual for Second Year engineering
students for the subject of Programming Language Design Laboratory.

As a student, many of you may be wondering with some of the questions in your
mind regarding the subject and exactly what has been tried is to answer through
this manual.

As you may be aware that MGM has already been awarded with ISO
9001:2015,140001:2015 certification and it is an endure to technically equip our
students taking the advantage of the procedural aspects of ISO Certification.

Faculty members are also advised that covering these aspects in initial stage itself,
will greatly relieved them in future as much of the load will be taken care by the
enthusiasm energies of the students once they are conceptually clear.

Dr. H. H. Shinde
Principal

2
LABORATORY MANUAL CONTENTS

This manual is intended for the Second-year students of Computer Science and
Engineering in the subject of Programming Language Design Laboratory. This
manual typically contains practical/Laboratory Sessions related with the subject,
covering various aspects related to the subject to enhanced understanding.

Students are advised to thoroughly go through this manual rather than only topics
mentioned in the syllabus as practical aspects are the key to understanding and
conceptual visualization of theoretical aspects covered in the books.

Good Luck for your Enjoyable Laboratory Sessions

Ms. Anuja R. Tungar Prof. Vijaya B. Musande


Subject Teacher HOD
MGM’s

Jawaharlal Nehru Engineering College, Aurangabad


Department of Computer Science and Engineering

Vision of CSE Department:

To develop computer engineers with necessary analytical ability and human values who can
creatively design, implement a wide spectrum of computer systems for welfare of the society.

Mission of the CSE Department:

I. Preparing graduates to work on multidisciplinary platforms associated with


their professional position both independently and in a team environment.
II. Preparing graduates for higher education and research in Computer Science
and Engineering enabling them to develop systems for society development.

Programme Educational Objectives: Graduates will be able to

I. To analyze, design and provide optimal solution for Computer Science


and Engineering and multidisciplinary problems.

II. To pursue higher studies and research by applying knowledge of mathematics


and fundamentals of computer science.

II. To exhibit professionalism, communication skills and adapt to current trends


by engaging in lifelong learning.

4
Programme Outcomes (POs): Engineering Graduates will be able to:
1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering fundamentals,
and an engineering specialization to the solution of complex engineering problems.

2. Problem analysis: Identify, formulate, review research literature, and analyze complex engineering
problems reaching substantiated conclusions using first principles of mathematics, natural sciences,
and engineering sciences.

3. Design/development of solutions: Design solutions for complex engineering problems and design
system components or processes that meet the specified needs with appropriate consideration for the
public health and safety, and the cultural, societal, and environmental considerations.

4. Conduct investigations of complex problems: Use research-based knowledge and research


methods including design of experiments, analysis and interpretation of data, and synthesis of the
information to provide valid conclusions.

5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities with an
understanding of the limitations.

6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to the
professional engineering practice.

7. Environment and sustainability: Understand the impact of the professional engineering solutions
in societal and environmental contexts, and demonstrate the knowledge of, and need for sustainable
development.

8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms of
the engineering practice.

9. Individual and team work: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings.

10. Communication: Communicate effectively on complex engineering activities with the engineering
community and with society at large, such as, being able to comprehend and write effective reports and
design documentation, make effective presentations, and give and receive clear instructions.

11. Project management and finance: Demonstrate knowledge and understanding of the engineering
and management principles and apply these to one’s own work, as a member and leader in a team, to
manage projects and in multidisciplinary environments.

5
12. Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.

6
LIST OF EXPERIMENTS

Course Code: 20UCS307L


Course Title: Programming Language Design Laboratory.

Sr. No. Name of the Experiment Page No.


1. Study of Problem identification

2. Study of flowcharts and algorithms

3. Study of Life and scope of variables (in C)

4. Study of structures, Functions and arrays

5. Study of Iterative control structures and 2D arrays


Study of data abstraction and encapsulation concepts in Object
6.
Oriented Programming
7. Study of Inheritance concept in Object Oriented Programming

8. Study of Polymorphism concept in Object Oriented Programming

9. Study of Constructors concept in Object Oriented Programming


Write a program to demonstrate use of logical programming
10.
language.

7
DOs and DON’Ts in Laboratory:
Make entry in the Log Book as soon as you enter the Laboratory.

All the students should sit according to their roll numbers starting from their left to right.

All the students are supposed to enter the terminal number in the log book.

Do not change the terminal on which you are working.

All the students are expected to get at least the algorithm of the program/concept to be implemented.

Strictly observe the instructions given by the teacher/Lab Instructor.

Do not disturb machine Hardware / Software Setup.

Instruction for Laboratory Teachers:

 Submission related to whatever lab work has been completed should be done during the next lab
session along with signing the index.

 The promptness of submission should be encouraged by way of marking and evaluation patterns that
will benefit the sincere students.

 Continuous assessment in the prescribed format must be followed.

8
Practical #1

Aim: Study of Problem identification

Objective:

Sr. No. Objectives LO BL


1. Understand the basic concept of problem solving. LO1 1
Acquire knowledge about the basic concept of
2. LO2 1
writing a program.

Problem Statement: Identify three problems that might arise at home, at college, or in a business
that could be solved more efficiently with computer assistance. Draw flow chart and write
pseudocode for the same.

Description: Every student will write his/ her understanding about the 3 problems statements &
write it in the form of algorithm. Out of three problems, 1 will be assigned by the batchmate. When
getting the requirements from the batchmate, student understands the importance of problem
specification, from developer’s point of view. Second problem will be assigned from a student to the
batchmate, here he/ she understands customers point of view. Scope of third problem statement can
be very large,e.g., Bank automation system. Here student will identify various modules required, etc.

9
Practical #2

Aim: Study of flowcharts and algorithms

Objective:

Sr. No. Objectives LO BL


1. Understand the basic concept of problem solving. LO1 1
Acquire knowledge about the basic concept of
2. LO2 1
writing a program.
Understand the role of constants, variables,
3. identifiers, operators, type conversion and other LO3 2
building blocks as a part of High-Quality Programs.

Problem Statement: Consider the following programming problem: Scientists have determined that
the world’s ocean levels are currently rising at about 1.5 millimeters per year. Write a algorithm and
draw a flow chart to display the following:
● The number of millimeters that the oceans will rise in five years
● The number of millimeters that the oceans will rise in seven years
● The number of millimeters that the oceans will rise in ten years
Flowchart:

10
Algorithm:

1. Start

2. initialize rise=1.5

3. Calculate In 5 yrs r5= 5*rise

4. Calculate in 7 yrs r7 = 7*rise

5. Calculate in 10 yrs r10 = 10*rise

6. Display output for rise in 5,7 and 10 years

7. Stop

11
Practical #3
Aim: Study of Life and scope of variables

Objective:

Sr. No. Objectives LO BL


1. Understand the basic concept of problem solving. LO1 1
Acquire knowledge about the basic concept of
2. LO2 1
writing a program.
Understand the role of constants, variables,
3. identifiers, operators, type conversion and other LO3 2
building blocks as a part of High-Quality Programs.

Theory: The scope of a variable refers to the areas or the sections of the program in which the
variable can be accessed, and the lifetime of a variable indicates how long the variable stays alive in
the memory. Scope determines the region in a C program where a variable is available to
use,Visibility of a variable is related to the accessibility of a variable in a particular scope of the
program.

There are four types of scope:

1. File scope: File scope of variables in C is defined as having the availability of the variable
throughout the file / program. It means that the variable has a global scope and it is available all
around for every function and every block in the program

2. Block scope: Block scope of variables in C is defined as when the variable is having a limited
scope and the memory occupied by the variable will be deleted once the execution of the block
ends. The variable is not accessible or available outside the block. A block of code can be
defined in curly braces {code_block}.

3. Function scope: Function scope of variables in C begins with the left curly brace {and ends with
12
a closing right curly brace}. A variable declared inside a function has a function scope. It has
been allocated memory when the function is called and once the function returns something the
function execution ends and with it the variable goes out of scope i.e., it gets deleted from the
memory.

4. Prototype scope: Function prototype scope of variables in C are declared in some function as its
parameters. These variables are similar to the function scope variables where a variable's
memory gets deleted once the function execution terminates.

Scope Rules: Scope of variables in C also have some rules demonstratedbelow. Variables can be
declared on three places in a C Program:
Variables that are declared inside the function or a block of code are known as local variables.
Variables that are declared outside of any function are known as global variables (usually at the start
of a program).
Variables that are declared in the definition of a function parameters as its formal parameters.

Local Variables: When we declare variables inside a function or an inner block of code, then these
variables are known as local variables. They can only be used inside the function scope or the block
scope.

Global Variables:When we declare variables outside of all the functions then these variables are
known as global variables. These variables always have file scope and can be accessed anywhere in
the program, they also remain in the memory until the execution of our program finishes.

Formal parameters: When we define a function and declare some variables in the function
parameters then these parameters are known as formal parameters/ variables. They have function
prototype scope.

Visibility of a variable: Visibility of a variable is defined by how a variable is accessible inside a


program. A variable is visible within its scope and hidden outside the scope. A variable's visibility
controls how much of the rest of the program may access it. You can limit the visibility of a variable
to a specific section of a function, a single function, a single source file, or anyplace in the C
13
program.

Lifetime of a variable: Lifetime of a variable is defined as for how much time period a variable is
occupying a valid space in the system's memory or lifetime is the period between when memory is
allocated to hold the variable and when it is freed. Once the variable is out of scope its lifetime ends.
Lifetime is also known as the range/ extent of a variable.

A variable in the C programming language can have static, automatic or dynamic lifetime.

Static Lifetime: Objects/Variables having static lifetime will remain in the memory until the
execution of the program finishes. These types of variables can be declared using the static keyword,
global variables also have a static lifetime: they survive as long as the program runs.
Example:
static int count = 0;

Automatic Lifetime:Objects/ Variables declared inside a block have automatic lifetime. Local
variables (those defined within a function) have an automatic lifetime by default: they arise when the
function is invoked and are deleted (together with their values) once the function execution finishes.
Example:

{ // block of code
int auto_lifetime_var = 0;
}

Dynamic Lifetime: Objects/Variables which are made during the run-time of a C program using the
Dynamic Memory Allocation concept using the malloc (), calloc () functions in C or new operator in
C++ are stored in the memory until they are explicitly removed from the memory using the free ()
function in C or delete operator in C++ and these variables are said to have a dynamic lifetime. These
variables are stored in the heap section of our system's memory, which is also known as the dynamic
memory.
Example:

14
int *ptr = (int *) malloc (sizeof (int) );

Description: Students will write various programs to elaborate use of these variables.

Example:

15
Student will explain/ discuss the output as:

16
Practical #4

Aim: Study of structures, Functions and arrays

Objective:

Sr. No. Objectives LO BL


1. Understand the basic concept of problem solving. LO1 1
Acquire knowledge about the basic concept of
2. LO2 1
writing a program.
Understand the role of constants, variables,
3. identifiers, operators, type conversion and other LO3 2
building blocks as a part of High-Quality Programs.
Learn to use conditional expressions, looping
4. statements and modular programming to solve LO4 3
problems in procedural programming

Problem statement: WAP using function for the payroll system. Declare an array ofrecords tostore
information of employees.

Develop get_data function to read information of n employess, calc_sal to calculatesalary, and


print_data function to print salary details in a tabular form.
This program can be implemented in two ways:
1. Using all data as Global data &
2. passing structure as parameter to the functions.

If the Basic Salary is less than or equal to 10000 then HRA = 8% of the basic, and DA =10% of the
basic. Basic Salary is less than or equal to 20000 then HRA = 16% and DA= 20%. Basic Salary is
greater than 20000 then HRA = 24% and DA = 30%. Gross = basic + Da + Hra;

17
Practical - 5

Aim:Study of Iterative control structures and 2D arrays, with graphics animation.

Objective:

Sr. No. Objectives LO BL


1. Understand the basic concept of problem solving. LO1 1
Acquire knowledge about the basic concept of
2. LO2 1
writing a program.
Get acquainted with various functionalities of
3. procedure-oriented programming. Use built in LO3 1
functions

Problem Statement: Develop a program for animation of any image using built in functions of
graphics.h file.

18
Practical- 6

Aim:Study of Classes and Objects.

Objective:

Sr. No. Objectives LO BL


1. Understand the basic concept of problem solving. LO1 1
Acquire knowledge about the basic concept of
2. LO2 1
writing a program.
Get acquainted with various functionalities of
3. procedure-oriented programming. Use built in LO3 1
functions
Get acquainted object-oriented programming
4. LO6 2
paradigm (Class and objects)

Problem Statement: (1) Write a program to print result of 5 students using OOP.
(2) Write a program in C++ for the grocery shop to prepare bill of customer.

Theory: Object-oriented programming (OOP) is a computer programming model thatorganizes


software design around data, or objects, rather than functions and logic.An object can be defined as a
data field that has unique attributes and behavior.A class is used to specify the form of an object and
it combines data representationand methods for manipulating that data into one neat package. The
data andfunctions within a class are called members of the class.
C++ Class Definitions:When a class is defined, a blueprint for a data type is created. This doesn't
actuallydefine any data or any memory allocation is done, but it defines what the class name means,
that is, what an objectof the class will consist of and what operations can be performed on such an
object.A class definition starts with the keyword class followed by the class name; and theclass body,
enclosed by a pair of curly braces. A class definition must be followedeither by a semicolon or a list
of declarations.
19
Syntax: -
Class class_name
{
Public:
Data members;
Private:
Member functions;
};

The keyword public determines the access attributes of the members of the class that follows it. A
public member can be accessed from outside the class anywhere within the scope of the class object.
You can also specify the members of a classas private or protected.
Define C++ Objects:A class provides the blueprints for objects, so basically an object is created
from aclass. We declare objects of a class with exactly the same sort of declaration thatwe declare
variables of basic types
Syntaxis:Class_name object_name ;

Accessing the Data Members: The public data members of objects of a class can be accessed using
the directmember access operator (.).
Abstraction: Data abstraction refers to, providing only essential information to the outside worldand
hiding their background details, i.e., to represent the needed information inprogram without
presenting the details.For example, a database system hides certain details of how data is stored
andcreated and maintained. Similar way, C++ classes provides different methods to theoutside world
without giving internal detail about those methods and data.
Encapsulation: Encapsulation is placing the data and the functions that work on that data in thesame
place. While working with procedural languages, it is not always clear whichfunctions work on
which variables but object-oriented programming provides youframework to place the data and the
relevant functions together in the same object

20
Practical 7

Aim:Study of Inheritance in C++.

Objective:

Sr. No. Objectives LO BL


1. Understand the basic concept of problem solving. LO1 1
Acquire knowledge about the basic concept of
2. LO2 1
writing a program.
Get acquainted with various functionalities of
3. procedure-oriented programming. Use built in LO3 1
functions
Get acquainted object-oriented programming
4. LO6 3
paradigms. (Study inheritance)

Problem statement: Write a program to declare a base class student with fieldsrno and marks in two
subjects.Derive another class from this base class with additional attributes total andpercentage of
marks. Read marks & rno and then print the result from the derivedclass.

Theory:

C++ Inheritance: In C++, inheritance is a process in which one object acquires all the properties and
behaviors of its parent object automatically. In such way, you can reuse, extend ormodify the
attributes and behaviors which are defined in other class.In C++, the class which inherits the
members of another class is called derived classand the class whose members are inherited is called
base class. The derived class isthe specialized class for the base class.
Advantages of C++ Inheritance are:
 Code reusability: The members of your parent class can be reused. So, thereis no need to
define the member again. As a result, less code is required in the class.

21
Types Of Inheritance
C++ supports five types of inheritance:
1. Single inheritance
2. Multiple inheritance
3. Hierarchical inheritance
4. Multilevel inheritance
5. Hybrid inheritance
Derived Classes: A Derived class is defined as the class derived from the base class.Syntax to define
a derives class is:
Class derived_class_name :: visibility-mode base_class_name
{
// body of the derived class.
}

Terms used in the syntax:


1. derived_class_name: It is the name of the derived class.
2. visibility mode: The visibility mode specifies whether the features of the base classare publicly
inherited or privately inherited. It can be public or private.
3. base_class_name: It is the name of the base class.When the base class is privately inherited by
the derived class, public members ofthe base class become the private members of the derived
class. Therefore, the publicmembers of the base class are not accessible by the objects of the
derived class onlyby the member functions of the derived class.When the base class is publicly
inherited by the derived class, public members ofthe base class also become the public members
of the derived class. Therefore, thepublic members of the base class are accessible by the
objects of the derived classas well as by the member functions of the base class.
C++ Single Inheritance:Single inheritance is defined as the inheritance in which a derived class is
inheritedfrom the only one base class
C++ Multilevel Inheritance:Multilevel inheritance is a process of deriving a class from another
derived class.When one class inherits another class which is further inherited by another class, itis
known as multi-level inheritance in C++.
C++ Multiple Inheritance: Multiple inheritance is the process of deriving a new class that inherits
the attributesfrom two or more classes.
C++ Hierarchical Inheritance: Hierarchical inheritance is defined as the process of deriving more
than one classfrom a base class.
22
Practical 8

Aim:Study and Implementation of polymorphism in C++.

Objective:

Sr. No. Objectives LO BL


1. Understand the basic concept of problem solving. LO1 1
Acquire knowledge about the basic concept of
2. LO2 1
writing a program.
Get acquainted with various functionalities of
3. procedure-oriented programming. Use built in LO3 1
functions
Get acquainted object-oriented programming
4. LO6 3
paradigms. (Study polymorphism)

Problem statement: Write a program that declares more than one function withsame name but
different prototypes. Access these overloaded functions in yourprogram depending on the vales
entered by the user at run time.

Theory:
C++ Polymorphism:The term "Polymorphism" is the combination of "poly" + "morphs" which
meansmany forms. It is a Greek word. In object-oriented programming, we use three mainconcepts:
inheritance, encapsulation, and polymorphism.Real Life Example ofPolymorphism: Let's consider a
real-life example of polymorphism. A lady behaves like a teacher ina classroom, mother or daughter
in a home and customer in a market. Here, a singleperson is behaving differently according to the
situations.

23
There are two types of polymorphism in C++:

• Compile time polymorphism: The overloaded functions are invoked by matchingthe type and
number of arguments. This information is available at the compiletime and, therefore, compiler
selects the appropriate function at the compile time.It is achieved by function overloading and
operatoroverloading which is alsoknown as static binding or early binding.
• Run time polymorphism: Run time polymorphism is achieved when the object'smethod is
invoked at the run time instead of compile time. It is achieved by methodoverriding which is also
known as dynamic binding or late binding.
Static Binding: Static binding happens at compile-time, i.e., the function call and the
functiondefinition are linked during the compile time itself. So, it is also called earlybinding.This is
because all the information needed to associate the function call to itsdefinition is available at the
compile time. Static binding happens by default forany normal function calls in the program. It also
occurs during functionoverloading and operator overloading.The binding of static, private and final
methods is always compile-time. This isbecause the type of class is determined at the compile-time
and hence bindinghappens then and there.
Dynamic Binding:Dynamic binding takes place during run time based on the type of object. Since
itis delayed till the run time, it is also called late binding or runtime binding. Whenthe compiler
cannot determine all the information required to resolve a functioncall during compile time, these
function calls are not bound until run time.

24
Practical 9
Aim:Study of default, parameterized and copy constructor.

Objective:

Sr. No. Objectives LO BL


1. Understand the basic concept of problem solving. LO1 1
Acquire knowledge about the basic concept of
2. LO2 1
writing a program.
Get acquainted with various functionalities of
3. procedure-oriented programming. Use built in LO3 1
functions
Get acquainted object-oriented programming
4. LO6 2
paradigms. (Study different types of constructors)

Problem Statement: Program to study the use of default, parameterized, copyconstructor in C++.

Theory:
C++ Constructor:In C++, constructor is a special method which is invoked automatically at the
timeof object creation. It is used to initialize the data members of new object generally.The
constructor in C++ has the same name as class or structure.
There can be two types of constructors in C++.
• Default constructor
• Parameterized constructor
C++ Default Constructor: A constructor which has no argument is known as default constructor. It
is invokedat the time of creating object.
C++ Parameterized Constructor: A constructor which has parameters is called parameterized
constructor. It is used toprovide different values to distinct objects.
C++ Copy Constructor: A Copy constructor is an overloaded constructor used to declare and
initialize anobject from another object.
Copy Constructor is of two types:Default Copy constructor: The compiler defines the default copy
25
constructor. Ifthe user defines no copy constructor, compiler supplies its constructor.User Defined
constructor: The programmer defines the user-defined constructor
A Copy Constructor is called in the following scenarios:
• When we initialize the object with another existing object of the same classtype
• When the object of the same class type is passed by value as an argument.
• When the function returns the object of the same class type by value.

26
Practical 10
Aim:Study of Logic Programming paradigm

Objective:

Sr. No. Objectives LO BL


1. Understand the basic concept of problem solving. LO1 1
Acquire knowledge about the basic concept of
2. LO2 1
writing a program.
Get acquainted with various functionalities of
3. procedure-oriented programming. Use built in LO3 1
functions
4. Get acquainted Logic programming paradigm LO5 2

Problem statement:Write a program that has collection of facts and run it to check the goal

Theory:Logic programming is a programming paradigm that is based on logic. Thismeans that a


logic programming language has sentences that follow logic, sothat they express facts and rules.
Computation using logic programming is doneby making logical inferences based on all available
data. In order for computerprograms to make use of logic programming, there must be a base of
existinglogic, called predicates. Predicates are used to build atomic formulas or atoms,which state
true facts. Predicates and atoms are used to create formulas andperform queries. Logic languages
most often rely on queries in order to displayrelevant data. These queries can exist as part of machine
learning, which can berun without the need for manual intervention.It is a type ofprogramming
paradigm that uses logic circuits to control howfacts and rules about the problems within the system
are represented orexpressed. In it, logic is used to represent knowledge, and inference is used
tomanipulate it. It tells the model about how to accomplish a goal rather thanwhat goal to
accomplish.
There are multiple different logic programming languages. The most commonlanguage, Prolog (from
the French programmation en logique, or programmingin logic), can also interface with other
programming languages such as Java andC. On top of being the most popular logic programming
27
language, Prolog wasalso one of the first such languages, with the first Prolog program created in
the1970s for use with interpretations. Prolog was developed using first-order logic,also called
predicate logic, which allows for the use of variables rather thanpropositions. Prolog utilizes artificial
intelligence (AI) to help form itsconclusions and can quickly process large amounts of data. Prolog
can be runwith or without manual inputs, meaning in it can be programmed to runautomatically as
part of data processing.
Prolog – Prolog is a logic programming language. It has important role inartificial intelligence.
Unlike many other programming languages, Prolog isintended primarily as a declarative
programming language. In Prolog, logic isexpressed as relations (called as Facts and Rules). Core
heart of Prolog lies atthe logic being applied. Formulation or Computation is carried out by running
aquery over the relations.
Lisp – Lisp is a family of programming languages with a long history and adistinctive, fully
parenthesized prefix notation. Originally specified in 1958,Lisp is the second-oldest high-level
programming language. Only Fortran isolder, by one year. Lisp has changed since its early days,
andmany dialects have existed over its history. Today, the best-known general-purposeLisp dialects
are Racket, Common Lisp, Scheme, and Clojure.Lisp was originally created as a practical
mathematical notation for computerprograms, influenced by (though not originally derived from) the
notationof Alonzo Church's lambda calculus. It quickly became the favouredprogramming language
for artificial intelligence (AI) research. As one of theearliest programming languages, Lisp pioneered
many ideas in computerscience, including tree data structures, automatic storage management,
dynamictyping, conditionals, higher-order functions, recursion, the self-hostingcompiler, and the
read–eval–print loop.

28
Sample program

Output:

29
30

You might also like