6 sem kuvempu university practical solution

30
6 Sem Kuvempu University Practical Solution 1.) Write a program in C# using command line arguments to display a welcome message. The message has to be supplied as input in the command line using System; class prac1 { public static void Main(string[] args) { foreach(string k in args) { Console.Write(k+" "); } Console.ReadLine(); } }

Upload: ashwin-kumar

Post on 27-Oct-2014

330 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: 6 Sem Kuvempu University Practical Solution

6 Sem Kuvempu University Practical Solution

1.) Write a program in C# using command line arguments to display a welcome message. The message has to be supplied as input in the command line

 

using System;

 

class prac1

{

public static void Main(string[] args)

{

foreach(string k in args)

{

Console.Write(k+" ");

}

Console.ReadLine();

}

}

 

 

2.) Write a program in C# to find the area of a circle, given its radius.

 

using System;

Page 2: 6 Sem Kuvempu University Practical Solution

 

class prac2

{

public static void Main()

{

float radius=8.9f;

float pi=3.14f;

float area;

area=pi*radius*radius;

Console.WriteLine("The Area Of Circle whose Radius is {0} :- {1}",radius,area);

Console.ReadLine();

}

}

 

3.)  Write a program in C# to find the area of polygon – triangle and rectangle by using multiple      constructors.

using System;

 

class prac3

{

prac3(double half,double length,double height)

{

double areaoftriangle=half*length*height;

Page 3: 6 Sem Kuvempu University Practical Solution

Console.WriteLine("The Area of Triangle of length {0} and height {1} is: {2}",length,height,areaoftriangle);

Console.ReadLine();

}

prac3(double len,double breadth)

{

double areaofrectangle=len*breadth;

Console.WriteLine("The Area of Rectangle of length {0} and height {1} is: {2}",len,breadth,areaofrectangle);

Console.ReadLine();

}

public static void Main()

{

prac3 obj1=new prac3(0.5,4.2,3.8);

prac3 obj2=new prac3(5.2,2.6);

Console.ReadLine();

}

}

 

4.)Write a program in C# to add and multiply two numbers and display the results. Demonstrate the use of ref and out parameters in this program.

 

using System;

 

class prac4

Page 4: 6 Sem Kuvempu University Practical Solution

{

public static void add(ref int num1,ref int num2)

{

int res_add=num1+num2;

Console.WriteLine("The Addition Of  {0} and {1} usingis : {2} ",num1,num2,res_add);

Console.ReadLine();

}

public static void product(out int number1,out int number2)

{

number1=10;

number2=10;

int res_product=number1*number2;

Console.WriteLine ("The Product Of {0} and {1} usingis : {2}",number1,number2,res_product);

Console.ReadLine();

}

public static void Main(string[] args)

{

int num1;

int num2;

num1=10;

num2=10;

add(ref num1,ref num2);

int number1;

Page 5: 6 Sem Kuvempu University Practical Solution

int number2;

product(out number1,out number2);

}

}

 

5.) Using foreach looping statement, write a program in C# to display all the elements of string array.

 

using System;

 

class prac5

{

public static void Main(string[] args)

{

string[] name={"Shakty","Shashi","Gaurav","Rahul","Pawan","Gandharva","Rakesh"};

foreach(string element in name)

{

Console.WriteLine(element);

Console.WriteLine();

}

Console.ReadLine();

}

}

 

Page 6: 6 Sem Kuvempu University Practical Solution

6.)  Write a program in C# to create a string array to store 10 employees name. Display the      employees name to the console by passing array as a parameter to the display function.

 

using System;

 

class prac6

{

public static void display(out string[] name)

{

name=new string[10]{"Shakty","Ronisha","Shashi","Gaurav","Rahul","Pawan","Gandharva","Rakesh","Manish","Mohd. Arif"};

}

public static void Main(string[] args)

{

string[] name;

display(out name);

Console.WriteLine("Array Elements Are :");

for(int i=0;i

{

Console.WriteLine(name[i]);

}

Console.WriteLine();

Console.WriteLine();

Console.WriteLine();

Page 7: 6 Sem Kuvempu University Practical Solution

Console.WriteLine();

Console.WriteLine();

Console.WriteLine();

Console.WriteLine();

Console.WriteLine("Press any key to exit..................");

Console.ReadLine();

}

}

 

7.)  Write a program in C# to demonstrate the usage of checked and unchecked statements.

 

using System;

 

class prac7

{

static void Main()

{

int x=Int32.MaxValue;

Console.WriteLine(x+1);

checked

{

Console.WriteLine(x+1);

}

Page 8: 6 Sem Kuvempu University Practical Solution

unchecked

{

Console.WriteLine(x+1);

}

}

}

 

8.) Write a class called rectangle which consists of members side_1, side_2 and displayArea(). Write another class square which inherits class rectangle. Take side of square as input and display the area of square on console.

 

using System;

 

public class rectangle

{

public int side_1=30;

public int side_2=40;

public virtual void displayArea()

{

int area=side_1*side_2;

Console.WriteLine("Area of  Rectangle is:-{0}",area);

}

}

class square:rectangle

Page 9: 6 Sem Kuvempu University Practical Solution

{

public override void displayArea()

{

int area=side_1*side_1;

Console.WriteLine("Area of Square whose side {0} which is inherited fromclass  is:- {1}",side_1,area);

}

public static void Main()

{

square obj=new square();

obj.displayArea();

Console.ReadLine();

}

}

9.)  Write a program to sort 20 decimal numbers in decreasing order and print the result.

 

using System;

 

namespace sorting

{

            class Class1

            {

static void sort(double[] a)

                        {

Page 10: 6 Sem Kuvempu University Practical Solution

                                    int i=0;

                                   

                                    while(i

                                    {

                                                double temp;

                                                if(a[i]

                                                {

                                                            temp=a[i+1];

                                                            a[i+1]=a[i];

                                                            a[i]=temp;

                                                            i=0;

                                                            continue;

                                                           

                                                }

                                                i++;

                                    }

                        }

   static void display(double[] a)

                                    {

                                                foreach(double x in a)

                                               

                                                            System.Console.WriteLine(x);

                                               

Page 11: 6 Sem Kuvempu University Practical Solution

                                    }

                       static void Main(string[] args)

                        {

                                   

             double[] a = new double[] {87.1,76.4,76.2,80.5,96.7,4.56,34.11,100.45,89.07,44.34,200.1,10.34,96.45,72.33,7.4,2.5,65.56,87.87,99.83,101.66};

                                      sort(a);

                                    display(a);

                                    Console.Read();

                        }

            }

}

10.) Write a program for matrix multiplication in C#.

 using System;

class Matrix3D

{

public const int DIMSIZE = 3;

private double[ , ] matrix = new double[DIMSIZE, DIMSIZE];

public double this[int x,int y]

{

get { return matrix[x, y]; }

set { matrix[x, y] = value;}

}

Page 12: 6 Sem Kuvempu University Practical Solution

public static Matrix3D operator  *(Matrix3D mat1, Matrix3D mat2)

{

Matrix3D newMatrix = new Matrix3D();

for (int x=0; x < DIMSIZE; x++)

for (int y=0; y < DIMSIZE; y++)

newMatrix[x, y] = mat1[x,  y] * mat2[x, y];

return newMatrix;

}

}

class MatrixTest

{

public static Random rand = new Random();

static void Main()

{

Matrix3D mat1 = new Matrix3D();

Matrix3D mat2 = new Matrix3D();

InitMatrix(mat1);

InitMatrix(mat2);

Console.WriteLine("Matrix 1 : ");

PrintMatrix(mat1);

Console.WriteLine("Marix 2: ");

PrintMatrix(mat2);

Matrix3D mat3 = mat1 * mat2;

Page 13: 6 Sem Kuvempu University Practical Solution

Console.WriteLine();

Console.WriteLine("Matrix 1 * Matrix 2 = ");

PrintMatrix(mat3);

}

public static void InitMatrix(Matrix3D mat)

{

for (int x=0; x < Matrix3D.DIMSIZE; x++)

for (int y=0; y < Matrix3D.DIMSIZE; y++)

mat[x, y] = rand.NextDouble()*10;

}                                  //done

public static void  PrintMatrix(Matrix3D mat)

{

Console.WriteLine();

for (int x=0; x< Matrix3D.DIMSIZE; x++)

{

Console.Write("[ ");

for (int y=0; y < Matrix3D.DIMSIZE; y++)

{

Console.Write("{0,8: # . 00}",mat[x, y]);

if((y+1 % 2)

{

Console.Write(" , ");

}

Page 14: 6 Sem Kuvempu University Practical Solution

Console.WriteLine(" ] ");

}

Console.WriteLine();

}

}}

 

11.) Write a program in C# to sort the students list based on their names. The students list is      stored in a string array.

 

 

using System;

 

namespace sorting

{

           

            class Class1

            {

                       

                        [STAThread]

 

                        static void sort(string[] b)

                        {

                                    int i=0;

                                   

Page 15: 6 Sem Kuvempu University Practical Solution

                                    while(i

                                    {

                                                string temp;

                                                if(0

                                                {

                                                            temp=b[i+1];

                                                            b[i+1]=b[i];

                                                            b[i]=temp;

                                                            i=0;

                                                            continue;

                                                           

                                                }

                                                i++;

                                    }

                        }

 

                                   

                        static void display(string[] b)

                        {

                                    foreach(string x in b)

                                               

                                                System.Console.WriteLine(x);

                                               

Page 16: 6 Sem Kuvempu University Practical Solution

                        }

 

 

                        static void Main(string[] args)

                        {

                                   

                                    string[] b = new String[] {"sumit","samit","scmit","sgmit","sglit"};

                                    sort(b);

                                    display(b);

                                    Console.Read();

                        }

            }

}

12.)  Write a program in C# to demonstrate operator overloading

 using System;

class Matrix3D

{

public const int DIMSIZE = 3;

private double[ , ] matrix = new double[DIMSIZE, DIMSIZE];

public double this[int x,int y]

{

get { return matrix[x, y]; }

set { matrix[x, y] = value;}

Page 17: 6 Sem Kuvempu University Practical Solution

}

public static Matrix3D operator  +(Matrix3D mat1, Matrix3D mat2)

{

Matrix3D newMatrix = new Matrix3D();

for (int x=0; x < DIMSIZE; x++)

for (int y=0; y < DIMSIZE; y++)

newMatrix[x, y] = mat1[x,  y] + mat2[x, y];

return newMatrix;

}

}

class MatrixTest

{

public static Random rand = new Random();

static void Main()

{

Matrix3D mat1 = new Matrix3D();

Matrix3D mat2 = new Matrix3D();

InitMatrix(mat1);

InitMatrix(mat2);

Console.WriteLine("Matrix 1 : ");

PrintMatrix(mat1);

Console.WriteLine("Marix 2: ");

PrintMatrix(mat2);

Page 18: 6 Sem Kuvempu University Practical Solution

Matrix3D mat3 = mat1 + mat2;

Console.WriteLine();

Console.WriteLine("Matrix 1 + Matrix 2 = ");

PrintMatrix(mat3);

}

public static void InitMatrix(Matrix3D mat)

{

for (int x=0; x < Matrix3D.DIMSIZE; x++)

for (int y=0; y < Matrix3D.DIMSIZE; y++)

mat[x, y] = rand.NextDouble()*10;

}                               

public static void  PrintMatrix(Matrix3D mat)

{

Console.WriteLine();

for (int x=0; x< Matrix3D.DIMSIZE; x++)

{

Console.Write("[ ");

for (int y=0; y < Matrix3D.DIMSIZE; y++)

{

Console.Write("{0,8: # . 00}",mat[x, y]);

if((y+1 % 2)

{

Console.Write(" , ");

Page 19: 6 Sem Kuvempu University Practical Solution

}

Console.WriteLine(" ] ");

}

Console.WriteLine();

}

}}

13.)  Write a program in C# to demonstrate boxing and unboxing opertation.

 using System;

 

class prac13

{

public static void Main()

{

int unbox_var=10;

int box_var=100;

object obj=box_var;  //boxing ie converting value type to reference type

Console.WriteLine("Value after boxing is : {0}",obj);

unbox_var=(int)obj;             //unboxing ie converting reference type to value type

Console.WriteLine("Value after unboxing is : {0} ",unbox_var);

Console.ReadLine();

}

}

14.) Write a program in C# to throw and catch any arithmetic exception.

Page 20: 6 Sem Kuvempu University Practical Solution

using System;

 

class prac14

{

public static void Main()

{

int num1=10;

int num2=0;

int div;

try

{

div=num1/num2;

Console.WriteLine(div);

Console.ReadLine();

}

catch(DivideByZeroException e)

{

Console.WriteLine("Exception Message : "+e.Message);

}

}

}

15.) Write a program to demonstrate the usage of threads in C#.

using System;

Page 21: 6 Sem Kuvempu University Practical Solution

using System.Threading;

using System.Collections;

using System.Collections.Generic;

 

 

public class SyncEvents

{

    public SyncEvents()

    {

        _newItemEvent = new AutoResetEvent(false);

 

       

        _exitThreadEvent = new ManualResetEvent(false);

 

        _eventArray = new WaitHandle[2];

        _eventArray[0] = _newItemEvent;

        _eventArray[1] = _exitThreadEvent;

    }

 

  

    public EventWaitHandle ExitThreadEvent

    {

        get { return _exitThreadEvent; }

Page 22: 6 Sem Kuvempu University Practical Solution

    }

    public EventWaitHandle NewItemEvent

    {

        get { return _newItemEvent; }

    }

    public WaitHandle[] EventArray

    {

        get { return _eventArray; }

    }

 

    private EventWaitHandle _newItemEvent;

    private EventWaitHandle _exitThreadEvent;

    private WaitHandle[] _eventArray;

}

 

 

public class Producer

{

    public Producer(Queueq, SyncEvents e)

    {

        _queue = q;

        _syncEvents = e;

    }

Page 23: 6 Sem Kuvempu University Practical Solution

    public void ThreadRun()

    {

        int count = 0;

        Random r = new Random();

        while (!_syncEvents.ExitThreadEvent.WaitOne(0, false))

        {

            lock (((ICollection)_queue).SyncRoot)

            {

                while (_queue.Count < 20)

                {

                    _queue.Enqueue(r.Next(0, 100));

                    _syncEvents.NewItemEvent.Set();

                    count++;

                }

            }

        }

        Console.WriteLine("Producer thread: produced {0} items", count);

    }

    private Queue_queue;

    private SyncEvents _syncEvents;

}

 

 

Page 24: 6 Sem Kuvempu University Practical Solution

public class Consumer

{

    public Consumer(Queueq, SyncEvents e)

    {

        _queue = q;

        _syncEvents = e;

    }

    public void ThreadRun()

    {

        int count = 0;

        while (WaitHandle.WaitAny(_syncEvents.EventArray) != 1)

        {

            lock (((ICollection)_queue).SyncRoot)

            {

                int item = _queue.Dequeue();

            }

            count++;

        }

        Console.WriteLine("Consumer Thread: consumed {0} items", count);

    }

    private Queue_queue;

    private SyncEvents _syncEvents;

}

Page 25: 6 Sem Kuvempu University Practical Solution

 

public class ThreadSyncSample

{

    private static void ShowQueueContents(Queueq)

    {

     

        lock (((ICollection)q).SyncRoot)

        {

            foreach (int i in q)

            {

                Console.Write("{0} ", i);

            }

        }

        Console.WriteLine();

    }

 

    static void Main()

    {

        SyncEvents syncEvents = new SyncEvents();

 

      

        Queuequeue = new Queue();

 

Page 26: 6 Sem Kuvempu University Practical Solution

   

        Console.WriteLine("Configuring worker threads...");

        Producer producer = new Producer(queue, syncEvents);

        Consumer consumer = new Consumer(queue, syncEvents);

 

    

        Thread producerThread = new Thread(producer.ThreadRun);

        Thread consumerThread = new Thread(consumer.ThreadRun);

   

        Console.WriteLine("Launching producer and consumer threads...");       

        producerThread.Start();

        consumerThread.Start();

 

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

        {

            Thread.Sleep(2500);

            ShowQueueContents(queue);

        }

 

 

        Console.WriteLine("Signaling threads to terminate...");

        syncEvents.ExitThreadEvent.Set();

 

Page 27: 6 Sem Kuvempu University Practical Solution

        Console.WriteLine("main thread waiting for threads to finish...");

        producerThread.Join();

        consumerThread.Join();

    }

}

Created with page4, copyright © 2012 by Humansoftware Development Corp., USA. All rights reserved.  Login