Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
BHOPAL (M.P.)
DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING
SESSION 2017-18
PRINCIPLES OF PROGRAMMING LANGUAGES (PPL)
CS 6002
PRACTICAL CONDUCTED IN LAB
2
PROGRAM#1– C++ PROGRAM TO STORE TEMPERATURE OF TWO
DIFFERENT CITIES FOR A WEEK AND DISPLAY IT.
#include <iostream>
using namespace std;
int main()
{
int temperature[CITY][WEEK];
cout << "Enter all temperature for a week of first city and then second city.
\n";
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];
5
return 0;
}
OUTPUT :-
Enter 12 values:
1
2
3
4
5
6
7
8
9
10
11
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;
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-
8
PROGRAM # 4 – WRITE A JAVA PROGRAM TO CREATE THREAD BY
EXTENDING THREAD CLASS
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 ( );
}
}
StringThread(String s, int n)
{
str = new String (s);
num =n;
}
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
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;
return 0;
}
OUTPUT
13
PROGRAMS # 7 - WRITE A C/C++ PROGRAM TO IMPLEMENT CALL
BY REFERENCE METHOD OF PASSING ARGUMENTS
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;
return 0;
14
}
OUTPUT
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.
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;
return 0;
}
OUTPUT
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