You are on page 1of 35

List of Practicals

 Implementation of 2D & 3D Array


 Implementation of Web Page in PHP
 Implementation of pointers in C++
 Implementation of Exception Handling in JAVA
 Implementation of Parameter Passing Methods in C++
 Implementation of Concurrent Execution using Threads in
JAVA
 Implement Inheritance, Encapsulation & Polymorphism in
C#
 Design of Lexical Analyzer using flex
 Working of Virtual Machine
Implementation of 2D & 3D Array

//2D Array

class TwoDArr{

public static void main(String args[]){

int td[][];

td= new int[3][3];

td[0][0]=9;

td[0][1]=8;

td[0][2]=7;

td[1][0]=6;

td[1][1]=5;

td[1][2]=4;

td[2][0]=30;

td[2][1]=2;

td[2][2]=1;

for(int i=0; i<3; i++){

for(int j=0; j<3; j++){

System.out.print(td[i][j] + "\t");

System.out.println();

}
Output: -
// 3D Array

class ThreeDArr {

public static void main(String[] args)

int[][][] arr = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } };

for (int i = 0; i < 2; i++)

for (int j = 0; j < 2; j++)

for (int z = 0; z < 2; z++)

System.out.println("arr[" + i

+ "]["

+ j + "]["

+ z + "] = "

+ arr[i][j][z]);

Output: -
Implementation of Web Page in PHP

<!DOCTYPE html>

<html>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="/w3css/3/w3.css">

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-


awesome.min.css">

<body>

<!-- Navigation -->

<nav class="w3-bar w3-black">

<a href="#home" class="w3-button w3-bar-item">Home</a>

<a href="#band" class="w3-button w3-bar-item">Band</a>

<a href="#tour" class="w3-button w3-bar-item">Tour</a>

<a href="#contact" class="w3-button w3-bar-item">Contact</a>

</nav>

<!-- Slide Show -->

<section>

<img class="mySlides" src="img_band_la.jpg"

style="width:100%">

<img class="mySlides" src="img_band_ny.jpg"

style="width:100%">
<img class="mySlides" src="img_band_chicago.jpg"

style="width:100%">

</section>

<!-- Band Description -->

<section class="w3-container w3-center w3-content" style="max-width:600px">

<h2 class="w3-wide">THE BAND</h2>

<p class="w3-opacity"><i>We love music</i></p>

<p class="w3-justify">We have created a fictional band website. Lorem ipsum dolor sit amet,
consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est
laborum consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua.</p>

</section>

<!-- Band Members -->

<section class="w3-row-padding w3-center w3-light-grey">

<article class="w3-third">

<p>John</p>

<img src="img_bandmember.jpg" alt="Random Name" style="width:100%">

<p>John is the smartest.</p>

</article>

<article class="w3-third">

<p>Paul</p>

<img src="img_bandmember.jpg" alt="Random Name" style="width:100%">

<p>Paul is the prettiest.</p>


</article>

<article class="w3-third">

<p>Ringo</p>

<img src="img_bandmember.jpg" alt="Random Name" style="width:100%">

<p>Ringo is the funniest.</p>

</article>

</section>

<!-- Footer -->

<footer class="w3-container w3-padding-64 w3-center w3-black w3-xlarge">

<a href="#"><i class="fa fa-facebook-official"></i></a>

<a href="#"><i class="fa fa-pinterest-p"></i></a>

<a href="#"><i class="fa fa-twitter"></i></a>

<a href="#"><i class="fa fa-flickr"></i></a>

<a href="#"><i class="fa fa-linkedin"></i></a>

<p class="w3-medium">

Powered by <a href="https://www.w3schools.com/w3css/default.asp" target="_blank">w3.css</a>

</p>

</footer>

<script>

// Automatic Slideshow - change image every 3 seconds

var myIndex = 0;

carousel();
function carousel() {

var i;

var x = document.getElementsByClassName("mySlides");

for (i = 0; i < x.length; i++) {

x[i].style.display = "none";

myIndex++;

if (myIndex > x.length) {myIndex = 1}

x[myIndex-1].style.display = "block";

setTimeout(carousel, 3000);

</script>

</body>

</html>

Screenshot: -
Implementation of pointers in C++

// C program to demonstrate that compiler

// internally uses pointer arithmetic to access

// array elements.

#include <stdio.h>

int main()

int arr[] = { 100, 200, 300, 400 };

// Compiler converts below to *(arr + 2).

printf("%d ", arr[2]);

// So below also works.

printf("%d\n", *(arr + 2));

return 0;

Output: -
Implementation of Exception Handling in JAVA

// Example Exception Handling

class Eh{

public static void main(String args[]){

int x=12;

int y=0;

System.out.println("statement 1");

try{

int z=x/y;

System.out.println(z);

catch(Exception e){

System.out.println("statement 2");

System.out.println("statement 3");

}
Output: -
Implementation of Parameter Passing Methods in C++

#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;

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

return 0;

Output: -
#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;

Output: -
Implementation of Concurrent Execution using Threads
in JAVA

// Java code for thread creation by extending the Thread class

class Thread1 extends Thread{

public void run(){

System.out.println("This is Thread1");

class Thread2 extends Thread{

public void run(){

System.out.println("This is Thread2");

class UseThread{

public static void main(String args[]){

Thread1 t1=new Thread1();

Thread2 t2=new Thread2();

t1.start();

t2.start();

}
Output: -
// Java code for thread creation by implementing the Runnable Interface

class RunnableThread1 implements Runnable{

public void run(){

System.out.println("This is Thread1 by runnable interface");

class RunnableThread2 implements Runnable{

public void run(){

System.out.println("This is Thread2 by runnable interface");

class UseRunnable{

public static void main(String args[]){

Thread t1=new Thread(new RunnableThread1());

Thread t2=new Thread(new RunnableThread2());

t1.start();

t2.start();

}
Output: -
Implement Inheritance, Encapsulation & Polymorphism
in C#

Static Polymorphism:
Function Overloading
using System;

namespace PolymorphismApplication {

class Printdata {

void print(int i) {

Console.WriteLine("Printing int: {0}", i );

void print(double f) {

Console.WriteLine("Printing float: {0}" , f);

void print(string s) {

Console.WriteLine("Printing string: {0}", s);

static void Main(string[] args) {

Printdata p = new Printdata();

// Call print to print integer

p.print(5);

// Call print to print float

p.print(500.263);

// Call print to print string


p.print("Hello C++");

Console.ReadKey();

Operator Overloading

using System;

namespace OperatorOvlApplication {

class Box {

private double length; // Length of a box

private double breadth; // Breadth of a box

private double height; // Height of a box

public double getVolume() {

return length * breadth * height;

public void setLength( double len ) {

length = len;

public void setBreadth( double bre ) {

breadth = bre;

public void setHeight( double hei ) {

height = hei;
}

// Overload + operator to add two Box objects.

public static Box operator+ (Box b, Box c) {

Box box = new Box();

box.length = b.length + c.length;

box.breadth = b.breadth + c.breadth;

box.height = b.height + c.height;

return box;

class Tester {

static void Main(string[] args) {

Box Box1 = new Box(); // Declare Box1 of type Box

Box Box2 = new Box(); // Declare Box2 of type Box

Box Box3 = new Box(); // Declare Box3 of type Box

double volume = 0.0; // Store the volume of a box here

// box 1 specification

Box1.setLength(6.0);

Box1.setBreadth(7.0);

Box1.setHeight(5.0);

// box 2 specification

Box2.setLength(12.0);

Box2.setBreadth(13.0);

Box2.setHeight(10.0);

// volume of box 1
volume = Box1.getVolume();

Console.WriteLine("Volume of Box1 : {0}", volume);

// volume of box 2

volume = Box2.getVolume();

Console.WriteLine("Volume of Box2 : {0}", volume);

// Add two object as follows:

Box3 = Box1 + Box2;

// volume of box 3

volume = Box3.getVolume();

Console.WriteLine("Volume of Box3 : {0}", volume);

Console.ReadKey();

Dynamic Polymorphism:

Using Abstract Class

using System;

namespace PolymorphismApplication {

abstract class Shape {

public abstract int area();

}
class Rectangle: Shape {

private int length;

private int width;

public Rectangle( int a = 0, int b = 0) {

length = a;

width = b;

public override int area () {

Console.WriteLine("Rectangle class area :");

return (width * length);

class RectangleTester {

static void Main(string[] args) {

Rectangle r = new Rectangle(10, 7);

double a = r.area();

Console.WriteLine("Area: {0}",a);

Console.ReadKey();

}
Using Virtual Functions

using System;

namespace PolymorphismApplication {

class Shape {

protected int width, height;

public Shape( int a = 0, int b = 0) {

width = a;

height = b;

public virtual int area() {

Console.WriteLine("Parent class area :");

return 0;

class Rectangle: Shape {

public Rectangle( int a = 0, int b = 0): base(a, b) {

public override int area () {

Console.WriteLine("Rectangle class area :");

return (width * height);

class Triangle: Shape {

public Triangle(int a = 0, int b = 0): base(a, b) {


}

public override int area() {

Console.WriteLine("Triangle class area :");

return (width * height / 2);

class Caller {

public void CallArea(Shape sh) {

int a;

a = sh.area();

Console.WriteLine("Area: {0}", a);

class Tester {

static void Main(string[] args) {

Caller c = new Caller();

Rectangle r = new Rectangle(10, 7);

Triangle t = new Triangle(10, 5);

c.CallArea(r);

c.CallArea(t);

Console.ReadKey();

}
Encapsulation :

Using Public Access Specifier

using System;

namespace RectangleApplication {

class Rectangle {

//member variables

public double length;

public double width;

public double GetArea() {

return length * width;

public void Display() {

Console.WriteLine("Length: {0}", length);

Console.WriteLine("Width: {0}", width);

Console.WriteLine("Area: {0}", GetArea());

}//end class Rectangle

class ExecuteRectangle {

static void Main(string[] args) {

Rectangle r = new Rectangle();

r.length = 4.5;
r.width = 3.5;

r.Display();

Console.ReadLine();

Using Private Access Specifier

using System;

namespace RectangleApplication {

class Rectangle {

//member variables

private double length;

private double width;

public void Acceptdetails() {

Console.WriteLine("Enter Length: ");

length = Convert.ToDouble(Console.ReadLine());

Console.WriteLine("Enter Width: ");

width = Convert.ToDouble(Console.ReadLine());

public double GetArea() {

return length * width;

public void Display() {


Console.WriteLine("Length: {0}", length);

Console.WriteLine("Width: {0}", width);

Console.WriteLine("Area: {0}", GetArea());

}//end class Rectangle

class ExecuteRectangle {

static void Main(string[] args) {

Rectangle r = new Rectangle();

r.Acceptdetails();

r.Display();

Console.ReadLine();

Using Protected & Internal Access Specifier

using System;

namespace RectangleApplication {

class Rectangle {

//member variables

internal double length;

internal double width;


double GetArea() {

return length * width;

public void Display() {

Console.WriteLine("Length: {0}", length);

Console.WriteLine("Width: {0}", width);

Console.WriteLine("Area: {0}", GetArea());

}//end class Rectangle

class ExecuteRectangle {

static void Main(string[] args) {

Rectangle r = new Rectangle();

r.length = 4.5;

r.width = 3.5;

r.Display();

Console.ReadLine();

}
Inheritance :

// C# program to illustrate the

// concept of inheritance

using System;

namespace ConsoleApplication1 {

// Base class

class GFG {

// data members

public string name;

public string subject;

// public method of base class

public void readers(string name, string subject)

this.name = name;

this.subject = subject;

Console.WriteLine("Myself: " + name);

Console.WriteLine("My Favorite Subject is: " + subject);

// inheriting the GFG class using :

class GeeksforGeeks : GFG {

// constructor of derived class

public GeeksforGeeks()
{

Console.WriteLine("GeeksforGeeks");

// Driver class

class Sudo {

// Main Method

static void Main(string[] args)

// creating object of derived class

GeeksforGeeks g = new GeeksforGeeks();

// calling the method of base class

// using the derived class object

g.readers("Kirti", "C#");

}
Design of Lexical Analyzer using flex

//Decalring two counters one for number of lines other for number of characters

%{

int no_of_lines = 0;

int no_of_chars = 0;

%}

/***rule 1 counts the number of lines,

rule 2 counts the number of characters

and rule 3 specifies when to stop

taking input***/

%%

\n ++no_of_lines;

. ++no_of_chars;

end return 0;

%%

/*** User code section***/

int yywrap(){}

int main(int argc, char **argv)

yylex();

printf("number of lines = %d, number of chars = %d\n",

no_of_lines, no_of_chars );

return 0;

}
Output: -
Working of Virtual Machine
Virtual machines allow you to run an operating system in an app window on your desktop that behaves
like a full, separate computer. You can use them play around with different operating systems, run
software your main operating system can’t, and try out apps in a safe, sandboxed environment.

There are several good free virtual machine (VM) apps out there, which makes setting up a virtual
machine something anybody can do. You’ll need to install a VM app, and have access to installation
media for the operating system you want to install.

What’s a Virtual Machine?


A virtual machine app creates a virtualized environment—called, simply enough, a virtual machine—that
behaves like a separate computer system, complete with virtual hardware devices. The VM runs as a
process in a window on your current operating system. You can boot an operating system installer disc
(or live CD) inside the virtual machine, and the operating system will be “tricked” into thinking it’s
running on a real computer. It will install and run just as it would on a real, physical machine. Whenever
you want to use the operating system, you can open the virtual machine program and use it in a window
on your current desktop.

In the VM world, the operating system actually running on your computer is called the host and any
operating systems running inside VMs are called guests. It helps keep things from getting too confusing.

In a particular VM, the guest OS is stored on a virtual hard drive—a big, multi-gigabyte file stored on
your real hard drive. The VM app presents this file the guest OS as a real hard drive. This means you
won’t have to mess around with partitioning or doing anything else complicated with your real hard
drive.

Virtualization does add some overhead, so don’t expect them to be as fast as if you had installed the
operating system on real hardware. Demanding games or other apps that require serious graphics and
CPU power don’t really do so well, so virtual machines aren’t the ideal way to play Windows PC games
on Linux or Mac OS X—at least, not unless those games are much older or aren’t graphically demanding.

The limit to how many VMs you can have are really just limited by the amount of hard drive space.

Why You’d Want to Create a Virtual Machine


Aside from being good geeky fun to play around with, VMs offer a number of serious uses. They allow
you to experiment with another OS without having to install it on your physical hardware. For example,
they are a great way to mess around with Linux—or a new Linux distribution—and see if it feels right for
you. When you’re done playing with an OS, you can just delete the VM.

VMs also provide a way to run another OS’ software. For example, as a Linux or Mac user, you could
install Windows in a VM to run Windows apps you might not otherwise have access to. If you want to
run a later version of Windows—like Windows 10—but have older apps that only run on XP, you could
install Windows XP into a VM.

Another advantage VMs provide is that they are “sandboxed” from the rest of your system. Software
inside a VM can’t escape the VM to tamper with the rest of your system. This makes VMs a safe place to
test apps—or websites—you don’t trust and see what they do.

For example, when the “Hi, we’re from Windows” scammers came calling, we ran their software in a
VM to see what they would actually do—the VM prevented the scammers from accessing our
computer’s real operating system and files.

Sandboxing also allows you to run insecure OSes more safely. If you still need Windows XP for older
apps, you could run it in a VM where at least the harm of running an old, unsupported OS is mitigated.

Virtual Machine Apps


There are several different virtual machine programs you can choose from:

 VirtualBox: (Windows, Linux, Mac OS X): VirtualBox is very popular because it’s open-source and
completely free. There’s no paid version of VirtualBox, so you don’t have to deal with the usual
“upgrade to get more features” upsells and nags. VirtualBox works very well, particularly on
Windows and Linux where there’s less competition, making it a good place to start with VMs.

 VMware Player: (Windows, Linux): VMware has their own line of virtual machine programs. You
can use VMware Player on Windows or Linux as a free, basic virtual machine tool. More
advanced features—many of which are found in VirtualBox for free—require upgrading to the
paid VMware Workstation program. We recommend starting out with VirtualBox, but if it
doesn’t work properly you may want to try VMware Player.

 VMware Fusion: (Mac OS X): Mac users must buy VMware Fusion to use a VMware product,
since the free VMware Player isn’t available on a Mac. However, VMware Fusion is more
polished.

 Parallels Desktop: (Mac OS X): Macs also have Parallels Desktop available. Both Parallels
Desktop and VMware Fusion for Mac are more polished than the virtual machine programs on
other platforms, since they’re marketed to average Mac users who might want to run Windows
software.
While VirtualBox works very well on Windows and Linux, Mac users may want to buy a more polished,
integrated Parallels Desktop or VMware Fusion program. Windows and Linux tools like VirtualBox and
VMware Player tend to be targeted to a geekier audience.

There are many more VM options, of course. Linux includes KVM, an integrated virtualization solution.
Professional and Enterprise version of Windows 8 and 10—but not Windows 7—include Microsoft’s
Hyper-V, another integrated virtual machine solution. These solutions can work well, but they don’t
have the most user-friendly interfaces.

You might also like