You are on page 1of 21

RADHARAMAN INSTITUTE OF TECHNOLOGY & SCIENCE

BHOPAL (M.P.)

DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING

Principles of Programming Languages (PPL)


Lab File (CS 6002)
CSE III YEAR VI SEMESTER

Submitted To: - Submitted by


Hansa Acharya Gaikwad
Asst. Prof.

SESSION 2017-18
PRINCIPLES OF PROGRAMMING LANGUAGES (PPL)
CS 6002
PRACTICAL CONDUCTED IN LAB

S. Page Performance Submission Teacher’s


TOPIC
No No. date date Signature
C++ PROGRAM TO STORE
TEMPERATURE OF TWO DIFFERENT
1 3
CITIES FOR A WEEK AND DISPLAY IT.
(2D ARRAY)
C++ PROGRAM TO STORE VALUE
ENTERED BY USER IN THREE
2 DIMENSIONAL ARRAY AND DISPLAY IT.
(3D ARRAY)

3 IMPLEMENTATION OF POINTER IN C++


JAVA PROGRAM FOR THREAD
4 CREATION BY EXTENDING THREAD
CLASS
JAVA PROGRAM FOR THREAD
5 CREATION BY IMPLEMENTING
RUNNABLE INTERFACE
IMPLEMENTATION OF CALL BY VALUE
6 METHOD OF PASSING ARGUMENTS
IMPLEMENTATION OF CALL BY
7 REFERENCE METHOD OF PASSING
ARGUMENTS
IMPLEMENTATION OF CALL BY
8 POINTER METHOD OF PASSING
ARGUMENTS IN C++
JAVA PROGRAM TO IMPLEMENT

9 INHERITANCE AND DEMONSTRATE USE


OF METHOD OVERRIDING.
JAVA PROGRAM TO IMPLEMENT
MULTILEVEL INHERITANCE BY
10 APPLYING VARIOUS ACCESS CONTROLS
TO ITS DATA MEMBERS AND METHODS.

2
PROGRAM#1– C++ PROGRAM TO STORE TEMPERATURE OF TWO
DIFFERENT CITIES FOR A WEEK AND DISPLAY IT.

#include <iostream>
using namespace std;

const int CITY = 2;


const int WEEK = 7;

int main()
{
int temperature[CITY][WEEK];

cout << "Enter all temperature for a week of first city and then second city.
\n";

// Inserting the values into the temperature array


for (int i = 0; i < CITY; ++i)
{
for(int j = 0; j < WEEK; ++j)
{
cout << "City " << i + 1 << ", Day " << j + 1 << " : ";
cin >> temperature[i][j];
}
}

cout << "\n\nDisplaying Values:\n";

// Accessing the values from the temperature array


for (int i = 0; i < CITY; ++i)
{
for(int j = 0; j < WEEK; ++j)
{
cout << "City " << i + 1 << ", Day " << j + 1 << " = " <<
temperature[i][j] << endl;
}
}

return 0;
}

3
OUTPUT
Enter all temperature for a week of first city and then second city.

City 1, Day 1 : 32
City 1, Day 2 : 33
City 1, Day 3 : 32
City 1, Day 4 : 34
City 1, Day 5 : 35
City 1, Day 6 : 36
City 1, Day 7 : 38
City 2, Day 1 : 23
City 2, Day 2 : 24
City 2, Day 3 : 26
City 2, Day 4 : 22
City 2, Day 5 : 29
City 2, Day 6 : 27
City 2, Day 7 : 23

Displaying Values:
City 1, Day 1 = 32
City 1, Day 2 = 33
City 1, Day 3 = 32
City 1, Day 4 = 34
City 1, Day 5 = 35
City 1, Day 6 = 36
City 1, Day 7 = 38
City 2, Day 1 = 23
City 2, Day 2 = 24
City 2, Day 3 = 26
City 2, Day 4 = 22
City 2, Day 5 = 29
City 2, Day 6 = 27
City 2, Day 7 = 23

4
PROGRAM#2– C++ PROGRAM TO STORE VALUE ENTERED BY USER
IN THREE DIMENSIONAL ARRAY AND DISPLAY IT.

#include <iostream>
using namespace std;

int main()
{
// This array can store upto 12 elements (2x3x2)
int test[2][3][2];

cout << "Enter 12 values: \n";

// Inserting the values into the test array


// using 3 nested for loops.
for(int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
for(int k = 0; k < 2; ++k )
{
cin >> test[i][j][k];
}
}
}

cout<<"\nDisplaying Value stored:"<<endl;

// Displaying the values with proper index.


for(int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
for(int k = 0; k < 2; ++k)
{
cout << "test[" << i << "][" << j << "][" << k << "] = " << test[i][j][k]
<< endl;
}
}
}

5
return 0;
}

OUTPUT :-
Enter 12 values:
1
2
3
4
5
6
7
8
9
10
11
12

Displaying Value stored:


test[0][0][0] = 1
test[0][0][1] = 2
test[0][1][0] = 3
test[0][1][1] = 4
test[0][2][0] = 5
test[0][2][1] = 6
test[1][0][0] = 7
test[1][0][1] = 8
test[1][1][0] = 9
test[1][1][1] = 10
test[1][2][0] = 11
test[1][2][1] = 12

6
PROGRAMS # 3 – WRITE A C++ PROGRAM TO IMPLEMENT POINTER
#include <iostream>
using namespace std;
int main() {
int *pc, c;

c = 5;
cout << "Address of c (&c): " << &c << endl;
cout << "Value of c (c): " << c << endl << endl;

pc = &c; // Pointer pc holds the memory address of variable c


cout << "Address that pointer pc holds (pc): "<< pc << endl;
cout << "Content of the address pointer pc holds (*pc): " << *pc << endl <<
endl;

c = 11; // The content inside memory address &c is changed from 5 to 11.
cout << "Address pointer pc holds (pc): " << pc << endl;
cout << "Content of the address pointer pc holds (*pc): " << *pc << endl <<
endl;

*pc = 2;
cout << "Address of c (&c): " << &c << endl;
cout << "Value of c (c): " << c << endl << endl;

return 0;
}

7
OUTPUT-

Address of c (&c): 0x7fff5fbff80c


Value of c (c): 5

Address that pointer pc holds (pc): 0x7fff5fbff80c


Content of the address pointer pc holds (*pc): 5

Address pointer pc holds (pc): 0x7fff5fbff80c


Content of the address pointer pc holds (*pc): 11

Address of c (&c): 0x7fff5fbff80c


Value of c (c): 2

8
PROGRAM # 4 – WRITE A JAVA PROGRAM TO CREATE THREAD BY
EXTENDING THREAD CLASS

class MyThread extends Thread


{
MyThread ( )
{
…………..
…………..
}

public void run ( ) //must override


{
………….
………….
}
………..
}

public class Example1


{
public static void main()
{

MyThread t = new MyThread ();


………….
t.start () ;
………….
}
}

9
OUTPUT

Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java
Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java
Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java
Java Java

10
PROGRAM # 5 – WRITE A JAVA PROGRAM TO CREATE THREAD BY
IMPLEMENTING RUNNABLE INTERFACE
class PrintString
{
public static void main (String args [ ])
{
StringThread t = new StringThread ("Java",50);
new Thread(t). start ( );
}
}

class StringThread implements Runnable


{
private String str;
private int num;

StringThread(String s, int n)
{
str = new String (s);
num =n;
}

public void run ( )


{
for (int i=1; i<=num; i++)
System.out.print (str+" ");
}
}

OUTPUT
Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java
Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java
Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java Java
Java Java

11
PROGRAMS # 6 – WRITE A C/C++ PROGRAM TO IMPLEMENT CALL
BY VALUE METHOD OF PASSING ARGUMENTS

By default, C++ uses call by value to pass arguments. In


general, this means that code within a function cannot alter
the arguments used to call the function. Consider the
function swap definition as follows.

// function definition to swap the values.


void swap(int x, int y) {
int temp;

temp = x; /* save the value of x */


x = y; /* put y into x */
y = temp; /* put x into y */

return;
}
Now, let us call the function swap by passing actual values
as in the following example −

#include <iostream>
using namespace std;

// function declaration
void swap(int x, int y);

int main () {
// local variable declaration:
int a = 100;
int b = 200;

cout << "Before swap, value of a :" << a << endl;


cout << "Before swap, value of b :" << b << endl;

// calling a function to swap the values.


swap(a, b);

cout << "After swap, value of a :" << a << endl;


12
cout << "After swap, value of b :" << b << endl;

return 0;
}

OUTPUT

Before swap, value of a :100


Before swap, value of b :200
After swap, value of a :100
After swap, value of b :200

13
PROGRAMS # 7 - WRITE A C/C++ PROGRAM TO IMPLEMENT CALL
BY REFERENCE METHOD OF PASSING ARGUMENTS

To pass the value by reference, argument reference is passed to the functions


just like any other value. So accordingly you need to declare the function
parameters as reference types as in the following function swap(), which
exchanges the values of the two integer variables pointed to by its arguments.

// function definition to swap the values.


void swap(int &x, int &y) {
int temp;
temp = x; /* save the value at address x */
x = y; /* put y into x */
y = temp; /* put x into y */

return;
}
For now, let us call the function swap() by passing values by reference as in the
following example −

#include <iostream>
using namespace std;

// function declaration
void swap(int &x, int &y);

int main () {
// local variable declaration:
int a = 100;
int b = 200;

cout << "Before swap, value of a :" << a << endl;


cout << "Before swap, value of b :" << b << endl;

/* calling a function to swap the values using variable reference.*/


swap(a, b);

cout << "After swap, value of a :" << a << endl;


cout << "After swap, value of b :" << b << endl;

return 0;
14
}

OUTPUT

Before swap, value of a :100


Before swap, value of b :200
After swap, value of a :200
After swap, value of b :100

15
PROGRAMS # 8 - WRITE A C/C++ PROGRAM TO IMPLEMENT OF
CALL BY POINTER METHOD OF PASSING ARGUMENTS IN C++

To pass the value by pointer, argument pointers are passed to the functions just
like any other value. So accordingly you need to declare the function
parameters as pointer types as in the following function swap(), which
exchanges the values of the two integer variables pointed to by its arguments.

// function definition to swap the values.


void swap(int *x, int *y) {
int temp;
temp = *x; /* save the value at address x */
*x = *y; /* put y into x */
*y = temp; /* put x into y */

return;
}
To check the more detail about C++ pointers, kindly check C++ Pointers
chapter.

For now, let us call the function swap() by passing values by pointer as in the
following example −

#include <iostream>
using namespace std;

// function declaration
void swap(int *x, int *y);

int main () {
// local variable declaration:
int a = 100;
int b = 200;

cout << "Before swap, value of a :" << a << endl;


cout << "Before swap, value of b :" << b << endl;

/* calling a function to swap the values.


* &a indicates pointer to a ie. address of variable a and
* &b indicates pointer to b ie. address of variable b.
*/
16
swap(&a, &b);

cout << "After swap, value of a :" << a << endl;


cout << "After swap, value of b :" << b << endl;

return 0;
}

OUTPUT

Before swap, value of a :100


Before swap, value of b :200
After swap, value of a :200
After swap, value of b :100

17
PROGRAMS # 9 - WRITE A JAVA PROGRAM TO IMPLEMENT
INHERITANCE AND DEMONSTRATE USE OF METHOD OVERRIDING.

18
OUTPUT

19
PROGRAM # 10 - WRITE A JAVA PROGRAM TO IMPLEMENT
MULTILEVEL INHERITANCE BY APPLYING VARIOUS ACCESS
CONTROLS TO ITS DATA MEMBERS AND METHODS.
we have three classes – Car, Maruti and Maruti800. We have done a
setup – class Maruti extends Car and class Maruti800 extends Maruti.
With the help of this Multilevel hierarchy setup our Maruti800 class is
able to use the methods of both the classes (Car and Maruti).

class Car{
public Car()
{
System.out.println("Class Car");
}
public void vehicleType()
{
System.out.println("Vehicle Type: Car");
}
}
class Maruti extends Car{
public Maruti()
{
System.out.println("Class Maruti");
}
public void brand()
{
System.out.println("Brand: Maruti");
}
public void speed()
{
20
System.out.println("Max: 90Kmph");
}
}
public class Maruti800 extends Maruti{

public Maruti800()
{
System.out.println("Maruti Model: 800");
}
public void speed()
{
System.out.println("Max: 80Kmph");
}
public static void main(String args[])
{
Maruti800 obj=new Maruti800();
obj.vehicleType();
obj.brand();
obj.speed();
}
}
OUTPUT
Class Car
Class Maruti
Maruti Model: 800
Vehicle Type: Car
Brand: Maruti
Max: 80Kmph

21