You are on page 1of 32

q4 .

NET lab 2 18-12-18

Q4. Accept user input int Console.Read(). Cast the int returned into a char. Check that the char
represents a digit using Char.isDigit( char c) which returns a Boolean value that can be used in
if statements. Accept input until 3 digits have been obtained. Display them in sorted order

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
int x;
char c;
char[] arr = new char[3];
for (int i = 0; i < 3; i++)
{
x = Console.Read();
c = (char)x;
if (Char.IsDigit(c))
arr[i] = c;
else
Console.WriteLine("not a digit");
}
Array.Sort(arr);
String s1 = new String(arr);
Console.WriteLine("string: " +s1);
Console.ReadKey(true);
}
}
}

q2 .NET lab 2 18-12-18

Q2. Type casts are used as usual, to force a wider data type into a narrow type. The type
double is double precision floating point, and int is a 64 bit integer.
Double d = 5.66; int i; A cast is needed in the assignment i = (int)d; Only the integer part is
retained after the cast.
The double Math.Sqrt(double d) finds the square root.
Write a program that displays the integer portion and the fractional part of the numbers 1 to 10.
Use a for loop with the usual syntax.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
double d1, sqrt;
int i;
for(int x =0; x<3;x++)
{
d1 = double.Parse(Console.ReadLine());
sqrt = Math.Sqrt(d1);
i = (int)sqrt;
Console.WriteLine("Integer part: " + i);
Console.WriteLine("Fraction part: " + (sqrt - i));
}
Console.ReadKey(true);
}
}
}

q3 .NET lab 2 18-12-18

Q3. Use format strings int x=5; int y=10; int z=15; System.WriteLine( “The value of variable x is
(0) and value of y is (1)”,x,y);
Prints “The value of variable x is 5 and value of y is 10”
WriteLine(“format string”, arg1, arg2,…argN)
In the format string, (0) stands for arg1, (1) for arg2, and (N-1) for argN.
Use a format string and call WriteLine(formatstring,x,y,z) such that x is displayed once, y twice,
and z thrice, on the same line with spaces between .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
int x = 5, y = 10, z = 15;
Console.WriteLine("{0} {1} {2} {3} {4} {5} ", x, y, y, z, z, z);
Console.ReadKey(true);
}
}
}

q1 .NET lab 2 18-12-18

Q1.Console.ReadLine() returns a string. The String class has a Compare(str1,str2) method


Which returns an int value which is zero if str1 and str2 are the same.
int i = String.Compare(“hello”,”bye”); i= String.Compare(“hello”, Console.ReadLine()); etc
Use the above line of code to write a program which displays a message “same strings” or “not
the same strings” accordingly.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
if((String.Compare(Console.ReadLine();,"bye"))==0)
Console.WriteLine("The String is \'bye\'");
else
Console.WriteLine("The String is not \'bye\'");
Console.ReadKey(true);
}
}
}

dotNET lab 3 q3 08-01-19

Q2. (a) Define a class Point with int fields x and y. Add a static int numPoints field that counts
the total number of instances of the class created, so far. Add a static method void showNum()
that displays the value of numPoints field. Provide two constructors (Point() and Point(int i, int
j)). Provide a void display() method that prints “x: _ ; y: _ “. All fields and methods can be public.
(b) In the main method of the Program class, create several instances of Point,and display the
total number using showNum().
Q3. Use the Point class above (no need of any static members. Only x, y, the constructors and
display() are needed. Add a method public Point minPoint(Point[] points) that takes an array of
Point objects and returns the Point closest to origin (r= sqrt(x*x +y*y)). In the main method, use
an instance of Point and use this method. Display the closest point from the array.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication12
{
public class Point
{
int x, y;
public Point()
{
x = 0;
y = 0;
}
public Point(int x1, int y1)
{
x = x1;
y = y1;
}
public void display()
{
Console.WriteLine("x: " + x + " y: " + y);
}
public Point minPoint(Point[] points)
{
int z = 0;
double[] r = new double[10];
for (int i = 0; i < r.Length; ++i)
{
r[i] = Math.Sqrt(points[i].x * points[i].x + points[i].y * points[i].y);
}
double small = r[0];

for (int i = 1; i < r.Length; i++)


{
if (r[i] < small)
{
small = r[i];
z = i;
}
}
return points[z];
}
}
class Program
{
static void Main(string[] args)
{
Point[] p = new Point[10];
for (int i = 0; i < p.Length; ++i)
{
int x1 = int.Parse(Console.ReadLine());
int y1 = int.Parse(Console.ReadLine());
p[i] = new Point(x1, y1);
}
for (int i = 0; i < p.Length; ++i)
{
p[i].display();
}
Point q = new Point();
q = q.minPoint(p);
Console.WriteLine("Closest point to origin: ");
q.display();
Console.ReadKey(true);
}
}
}

dotNET lab 3 q2 08-01-19

Q2. (a) Define a class Point with int fields x and y. Add a static int numPoints field that counts
the total number of instances of the class created, so far. Add a static method void showNum()
that displays the value of numPoints field. Provide two constructors (Point() and Point(int i, int
j)). Provide a void display() method that prints “x: _ ; y: _ “. All fields and methods can be public.
(b) In the main method of the Program class, create several instances of Point,and display the
total number using showNum().

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication11
{
public class Point
{
int x, y;
static int numPoints = 0;
public Point()
{
numPoints++;
x = y = 0;
}
public static void showNum()
{
Console.WriteLine("Number of points " +numPoints);
}
public void display()
{
Console.WriteLine("x: " +x+ "y: " +y);
}

}
class Program
{
static void Main(string[] args)
{
Point [] p = new Point[10];
for (int i = 0; i < p.Length; ++i)
{
p[i] = new Point();
}
Point.showNum();
Console.ReadKey(true);
}
}
}

dotNET lab 3 q1 08-01-19

Q1. Define a function void twoMins(int{] numarr, out int min1, out int min2) that accepts an array
of integers and return the two smallest elements through the out parameters. Call this function
in the main method, pass an integer array and display the two smallest numbers obtained.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
int[] arr = new int[n];
for (int x = 0; x < n; x++)
{
arr[x] = int.Parse(Console.ReadLine());
}
int small1, small2;
twoMins(arr, out small1, out small2);
Console.WriteLine("smallest is " + small1);
Console.WriteLine("second smallest is " + small2);
Console.ReadKey(true);
}
public static void twoMins(int [] numarr, out int min1, out int min2)
{
int y = numarr[0];
int z = numarr[1];
for (int i = 1; i < numarr.Length; i++)
{
if (numarr[i] < y)
{
z = y;
y = numarr[i];
}
else if (numarr[i] < z && numarr[i] != y)
z = numarr[i];
}
min1 = y;
min2 = z;
}
}
}

dotNet lab q3 12-02-09

Q3. Write a program to open a text file and display its contents.

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication13
{
class Program
{
static void Main(string[] args)
{
try{
string files,s1;
files="C:\\Users\\user\\Desktop\\test.txt";
StreamReader sr = new StreamReader(files);
do{
s1 = sr.ReadLine();
Console.WriteLine(s1);
}while(s1!=null);
}
catch(Exception e)
{
Console.WriteLine(e);
}
Console.ReadKey(true);

}
}

dotNet lab q2 12-02-09

Q2. (a) Define a NegBalException class using Exception as base class. Provide a no args
constructor in it which invokes the base(). Define a Bank account class. It has an int balance. It
has a method void dispensecash(int amt). If amt > balance, it creates an instance of
NegBalException and throws it.
NegBalException ng = new NegBalException(); throw ng;
In the Main, create an instance of BankAccount, call dispensecash() two times inside a try
block. Provide a catch block
catch(NegBalException e){}
Call the method amt value less than balance, and then with amt > balance.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConasoleApplication12
{
class NegBalException : Exception
{
public NegBalException()
: base()
{}
}
class BankAccount
{
int bal;
public BankAccount(int a)
{
bal = a;
}
public void dispensecash(int amt)
{
if (amt > bal)
{
NegBalException ng = new NegBalException();
throw ng;
}

}
}
class Program
{
static void Main(string[] args)
{
BankAccount a1 = new BankAccount(10000);
try
{
Console.WriteLine("AMt is 5000");
a1.dispensecash(5000);
Console.WriteLine("AMt is 15000");
a1.dispensecash(15000);
}
catch (NegBalException e)
{
Console.WriteLine(e);
Console.WriteLine();
}
Console.ReadKey(true);
}
}
}

dotNet lab q1 12-02-09

Q1. Write a program that :


1. Repeatedly asks user to enter “zero”, “one” or “two”.
2. Has a try block in which a divide by 0 is done if user entered “zero”, an array is accessed out
of bounds if user entered “one”, a null reference is used if user enters “two” and prints “you
finished the try block without errors” if user entered “three”
3. has the catch blocks for all 3 errors, which print “divide by zero happened” or “array was
accessed out of bounds” or “null object used”
4. has finally block that prints “inside finally block”
5. Asks for user input until user enters “quit”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication11
{
class Q1
{
int[] arr = new int[5]{1,2,3,4,5};
int var = 0;
public void e0()
{
Console.WriteLine(10 / var);
}
public void e1()
{
Console.WriteLine(arr[5]);
}
}
class Program
{
static void Main(string[] args)
{
string s1 = null;
Q1 a1 = new Q1();
Q1 a2 = null;
do{
try{
Console.WriteLine("Enter zero , one or two. Write quit to exit");
s1 = Console.ReadLine();
if (s1.Equals("zero"))
a1.e0();
else if (s1.Equals("one"))
a1.e1();
else if (s1.Equals("two"))
a2.e0();
}
catch(DivideByZeroException e)
{
Console.WriteLine("***Divided by zero*** " + e);
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine("***array was accessed out of bounds***" + e);
}
catch (NullReferenceException e)
{
Console.WriteLine("***null object used***" + e);
}
finally
{
Console.WriteLine("inside finally block");
}
}while(s1!="quit");
}
}
}

dotNet Lab q2 19-2-19

Q2. Open a text file using a StreamReader. Read char by char, and store into a second file,
discarding any ‘a’ s in the input file. If the input file contains “This is about a file”, then the output
file contains”This is bout file”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
FileStream fs1 = new FileStream("test.txt",FileMode.Open);
StreamReader sr = new StreamReader(fs1);
FileStream fs2 = new FileStream("test2.txt", FileMode.Create);
StreamWriter sw = new StreamWriter(fs2);
char c;
while(!sr.EndOfStream)
{
if ((c = (char)sr.Read()) != 'a')
sw.Write(c);
}
Console.ReadKey(true);
sw.Close();
sr.Close();
}
}
}

dotNet Lab q1 19-2-19

Q1. Create a BinaryWriter. Store the values of square roots of 1 to 10 in a file called sqrts.txt.
Open using a BinaryReader, read the square roots, and display “square root of 1: “ etc., and
add the total sum of the squareroots.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{double sum=0;
double a;

try
{
BinaryWriter bw = new BinaryWriter(new FileStream("sqrts.txt", FileMode.Create));
for (int i = 1; i <=10; i++)
{
Console.WriteLine(Math.Sqrt(i));
bw.Write(Math.Sqrt(i));
}
bw.Close();
Console.WriteLine();
BinaryReader br = new BinaryReader(new FileStream("sqrts.txt", FileMode.Open));
for (int i = 1; i <= 10; i++)
{
a=br.ReadDouble();
Console.WriteLine("square root of "+i+" is: "+a);
sum = sum + a;
}
br.Close();
Console.WriteLine("sum of square roots is "+sum);
}
catch(Exception e)
{
Console.WriteLine(e);
}
Console.ReadKey(true);
}
}
}

dotNet lab q2 5-3-19

Q1. Define a namespace called Myspace1. Within this namespace, add 3 simple classes, with
one attribute and one method, atleast. Close the namespace.
Create a second namespace called Myspace2. Add a class to this space which has a main
method. In the main method, create instances of classes defined in Myspace1 (i) without
applying the ‘using’ and

Q2. by applying the using keyword at the beginning of the program.

using System;
using Myspace1;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Myspace1
{
public class c1
{
public int x;
public void display()
{
Console.WriteLine(x);
}
}
public class c2
{
public int x;
public void display()
{
Console.WriteLine(x);
}
}
}
namespace Myspace1
{
public class c3
{
public int x;
public void display()
{
Console.WriteLine(x);
}
}
}

namespace Myspace2
{
class Program
{
static void Main(string[] args)
{
c1 ob1 = new c1();
ob1.x = 1;
ob1.display();
c2 ob2 = new c2();
ob2.x = 2;
ob2.display();
c3 ob3 = new c3();
ob3.x = 3;
ob3.display();
Console.ReadKey();
}
}
}

dotNet lab q1 5-3-19

Q1. Define a namespace called Myspace1. Within this namespace, add 3 simple classes, with
one attribute and one method, atleast. Close the namespace.
Create a second namespace called Myspace2. Add a class to this space which has a main
method. In the main method, create instances of classes defined in Myspace1 (i) without
applying the ‘using’ and

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Myspace1
{
public class c1
{
public int x;
public void display()
{
Console.WriteLine(x);
}
}
public class c2
{
public int x;
public void display()
{
Console.WriteLine(x);
}
}
}
namespace Myspace1
{
public class c3
{
public int x;
public void display()
{
Console.WriteLine(x);
}
}
}
namespace Myspace2
{
class Program
{
static void Main(string[] args)
{
Myspace1.c1 ob1 = new Myspace1.c1();
ob1.x = 1;
ob1.display();
Myspace1.c2 ob2 = new Myspace1.c2();
ob2.x = 2;
ob2.display();
Myspace1.c3 ob3 = new Myspace1.c3();
ob3.x = 3;
ob3.display();
Console.ReadKey();
}
}
}

dotNet Lab 4(22-01-2019) q3

Q3 A StudentList a string array names whose length is tracked by an int size member . It has a
constructor that takes an int and a string array. Provide an indexer to this class by using the
indexer syntax. In the ‘set’ part of the indexer syntax, check that the index is between zero and
size-1. If yes, then store the string in the names array, at the location given by index. Similarly,
in the get part, check that the index value is within bounds and return the correct element.
Create the StudentList object. Add names using an index. Access the names using an index
and display them

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication17
{
class StudentList
{
int len;
string[] names;
StudentList(int temp)
{
len = temp;
names = new string[len];
}
StudentList(int temp,params string[] strArr)
{
len = temp;
names = strArr;
}
public string this[int i]
{
get
{
if (i >= 0 && i < len)
return names[i];
else
return "";
}
set
{
if (i >= 0)
names[i] = value;
else
{
Console.WriteLine("array index out of bounds");
}
}
}
static void Main(string[] args)
{
StudentList s1 = new StudentList(5);
s1[0] = "Chaitu";
s1[1] = "Pranati";
s1[2] = "Nitu";
s1[3] = "Sripad";
s1[4] = "Naveen";
for(int i = 0;i<5;i++)
{
Console.WriteLine(s1[i]);
Console.WriteLine();
}
Console.ReadKey(true);

}
}
}

dotNet Lab 4(22-01-2019) q2

Q2. Define a Student class which has a string filed ‘name’ and an int array of size four called
‘marks’. Provide a constructor that takes a string an int array to initialize the object. You are
required to compare the objects on the basis of average marks. Hence, overload the <= and the
>= binary operators in the class. Remember that these operators have to be overridden as a
pair.In the main method of another class Exp,, create an array of Student objects, and sort the
array using the overloaded operators.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication16
{
class Student
{
string name;
int [] marks = new int[4];
Student(string s, params int[] arr)
{
name = s;
marks = arr;
}
public static Boolean operator <= (Student a, Student b)
{
int sum1=0, sum2=0;
float avg1,avg2;
for (int i =0;i<4;i++)
{
sum1 += a.marks[i];
sum2 += b.marks[i];
}
avg1 = sum1 / 4;
avg2 = sum2 / 4;
if (avg1 <= avg2)
return true;
else
return false;
}
public static Boolean operator >=(Student a, Student b)
{
int sum1 = 0, sum2 = 0;
float avg1, avg2;
for (int i = 0; i < 4; i++)
{
sum1 += a.marks[i];
sum2 += b.marks[i];
}
avg1 = sum1 / 4;
avg2 = sum2 / 4;
if (avg1 >= avg2)
return true;
else
return false;
}
void display()
{
Console.WriteLine("Name: " + name + " Marks: " + marks[0] + " " + marks[1] + " " +
marks[2] + " " + marks[3] + " " + marks[4] + " ");
Console.WriteLine();
}
static void Main(string[] args)
{

Student s1 = new Student("Chaitu",1,1,1,1,1);


Student s2 = new Student("Naveen",2,2,2,2,2);
Student[] stuArr = new Student[5];
stuArr[0] = new Student("Pranati",1,1,1,1,1);
stuArr[1] = new Student("Sripad", 2, 2, 2, 2, 2);
stuArr[2] = new Student("Ravi", 9, 9, 9, 9, 9);
stuArr[3] = new Student("Hanu", 5, 5, 5, 5, 5);
stuArr[4] = new Student("Sowjanya", 7, 7, 7, 7, 7);
if(s1 <= s2)
Console.WriteLine("s1 < s2");
else if (s2 <= s1)
Console.WriteLine("s2 < s1");
Console.WriteLine("Before Sorting..\n");
for (int i = 0; i < 5; i++)
{
stuArr[i].display();
}
Student temp;
for(int i = 0;i<5;i++)
{
for (int j = i+1; j < 5; j++)
if (stuArr[i] >= stuArr[j])
{
temp = stuArr[i];
stuArr[i] = stuArr[j];
stuArr[j] = temp;
}
}
Console.WriteLine("After Sorting..\n");
for(int i =0 ;i<5;i++)
{
stuArr[i].display();
}
Console.ReadKey(true);
}
}
}

dotNET lab 5(1-29-19) q2

Q3. Recall that a Property is a member field of a class to which a Set and a Get block are
attached. The member field itself is private, and it is accessed via the Set and Get.
Define a Greeter class with a Property called ‘background’. This is a string property. Define the
set block such that when this property is set to “blue”, the background color of the window is set
to blue, and a “hello” is displayed in white. If it is set to “white”, then the background becomes
white, and the message is displayed in blue. Any other strings are ignored. Create an instance
of Greeter in the main, and change the Property 3 times,taking user input.
You can change the background color of the console window as follows:
Console.BacgroundColor= ConsoleColor.Blue; //or ConsoleColor.White
Console.Clear(); //refreshes with the new color
Similarly, the color of the foreground (font) is changed as
Console.ForegroundColor = ConsoleColor.White

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication17
{
class Greeter
{
public string background
{
get
{
return "hmm";
}
set
{
if(value=="white")
{
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Blue;
Console.Clear();
Console.WriteLine("hello");
}
else if (value == "blue")
{
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
Console.Clear();
Console.WriteLine("hello");
}
}
}
static void Main(string[] args)
{
Greeter g1 = new Greeter();
for (int i = 0; i < 3;i++ )
g1.background = Console.ReadLine();
Console.ReadKey(true);
}
}
}

dotNET lab 5(1-29-19) q1

Q1.Define a class TwoDPoint, with int coordinates x and y. Define a derived class called
ThreeDPoint with coordinates x,y and z.
Provide a TwoDPoint() constructor which sets x,y to 0 and prints “in TwoDPoints()”. Provide a
ThreeDPoint() constructor that sets x=1,y=1,z=1 and prints “in ThreeDPoint()”. In the main of the
program, create a ThreeDPoint instance and observe the out put
Provide a TwoDPoint(inti,intj) to the class, which assigns the values and prints “in
TwoDPoint(I,j)”. In the derived class, provide a ThreeDPoint(int i,int j, int k) which used the base
constructor to set x and y, then sets the value of z and prints “in ThreeDPoint()”. Create a
ThreeDPoint instance and observe.
Add a virtual double radius() method to the base class. Override it in the derived class. In the
program, declare TwoDPoint p. Assign a TwoDPoint instance to p, invoke radius() on it. Assign
a ThreeDPoint instance to p. Invoke radius() on it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication16
{
public class TwoDPoint
{
double x, y;
public TwoDPoint()
{
x = 0;
y = 0;
Console.WriteLine("in twodpoint\n");
}
public TwoDPoint(double a, double b)
{
x = a;
y = b;
Console.WriteLine("in twodpoint({0},{1})\n",x,y);
}
public virtual double radius()
{
return 2.0;
}
}
public class ThreeDPoint : TwoDPoint
{
double x, y, z;
public ThreeDPoint()
{
x = 1;
y = 1;
z = 1;
Console.WriteLine("in threedpoint\n");
}
public ThreeDPoint(double a, double b, double c) : base(a,b)
{
z = c;
Console.WriteLine("in threedpoint\n");
}
public override double radius()
{
return 3.0;
}
}
class Program
{
static void Main(string[] args)
{
ThreeDPoint p1 = new ThreeDPoint();
ThreeDPoint p2 = new ThreeDPoint(3,3,3);
TwoDPoint p = new TwoDPoint(2,2);
Console.WriteLine(p.radius());
p = new ThreeDPoint(3,3,3);
Console.WriteLine(p.radius());
Console.ReadKey(true);
}
}
}

dotNET lab q2 (5-2-19)

Q1. Define an interface called Dues with a method int getDues(). Define a Student class (with
two members – int feedues and string name) which implements the interface. Define a
Librarymember class (with two members string name and int bookdues) which also implements
the Dues interface.
Provide the correct constructor to both classes. The getDues() method simply returns the dues,
(after printing a suitable message).
Create a several objects of Student type and Librarymember type, in the main method of the
Program class. Declare
Dues d;
Assign the Student object to d, and call get Dues. Then assign the Librarymember object to d
and call the method. This is polymorphism via an interface (interface is a type, like a class)
Create an array of Dues type. Assign some student objects and some Librarymember objects to
the array. Go through the array and call getDues() on all objects.
Note that the class references can also be used to invoke methods in the interface.
Student s = new Student(); I = s.getDues();
Similarly Librarymember m = new Librarymember(); I = m.getDues() etc
Q2. Extend the Dues interface to create a new interface
Public interface Finance : Dues{
Int getRefunds(); // interface method is public by default, and virtual
}
Define the Student class again, implement the extended interface (two methods). Call the
methods using a Finance reference, and also Student reference.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication24
{
public interface Dues
{
int getDues();
}
public interface Finance:Dues
{
int getRefunds();
}
public class Student : Finance
{
int feedues;
string name;
public Student(int f, string n)
{
feedues = f;
name = n;
}
public int getDues()
{
Console.WriteLine(name);
Console.WriteLine();
return feedues;
}
public int getRefunds()
{
return 100;
}
}
class Program
{
static void Main(string[] args)
{
Finance f;
f = new Student(500, "Nitish");
Console.WriteLine(" due: " + f.getDues());
Console.WriteLine(" refund: " + f.getRefunds());
Console.WriteLine();
Student s = new Student(200, "Chyannitt");
Console.WriteLine("due: "+s.getDues());
Console.WriteLine(" refund: "+ s.getRefunds());
Console.WriteLine();

Console.ReadKey(true);
}
}
}

dotNET lab q1 (5-2-19)

Q1. Define an interface called Dues with a method int getDues(). Define a Student class (with
two members – int feedues and string name) which implements the interface. Define a
Librarymember class (with two members string name and int bookdues) which also implements
the Dues interface.
Provide the correct constructor to both classes. The getDues() method simply returns the dues,
(after printing a suitable message).
Create a several objects of Student type and Librarymember type, in the main method of the
Program class. Declare
Dues d;
Assign the Student object to d, and call get Dues. Then assign the Librarymember object to d
and call the method. This is polymorphism via an interface (interface is a type, like a class)
Create an array of Dues type. Assign some student objects and some Librarymember objects to
the array. Go through the array and call getDues() on all objects.
Note that the class references can also be used to invoke methods in the interface.
Student s = new Student(); I = s.getDues();
Similarly Librarymember m = new Librarymember(); I = m.getDues() etc

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication23
{
public interface Dues
{
int getDues();
}
public class Student : Dues
{
int feedues;
string name;
public Student(int f, string n)
{
feedues = f;
name = n;
}
public int getDues()
{
Console.WriteLine(name);
Console.WriteLine();
return feedues;
}
}
public class Librarymember : Dues
{
string name;
int bookdues;
public Librarymember(int b,string n)
{
name = n;
bookdues = b;
}
public int getDues()
{
Console.WriteLine(name);
Console.WriteLine();
return bookdues;
}
}
class Program
{
static void Main(string[] args)
{
Dues d;
d = new Student(500,"Nitish");
Console.WriteLine(d.getDues());
Console.WriteLine();
d = new Librarymember(200,"Chyannitt");
Console.WriteLine(d.getDues());
Console.WriteLine();
Dues[] arr = new Dues[6];
Console.WriteLine();
Console.WriteLine();

for(int i =0;i<3;i++)
{
Console.WriteLine("Enter Student feedue and name for {0}",i+1);
arr[i] = new Student(int.Parse(Console.ReadLine()), Console.ReadLine());
}
for (int i = 3; i < 6; i++)
{
Console.WriteLine("Enter Library member bookdue and name for {0}", i + 1);
arr[i] = new Librarymember(int.Parse(Console.ReadLine()), Console.ReadLine());
}
for(int i = 0;i<6;i++)
{
Console.WriteLine("due: "+arr[i].getDues());
Console.WriteLine();
}
Console.ReadKey(true);
}
}
}

dotNET Lab q2 12-03-19

class Gen<yo,man>
{
yo a;
man b;
public Gen (yo temp1,man temp2)
{
a=temp1;
b=temp2;
}
public yo display()
{
Console.WriteLine(b);
return a;
}
}
public class test
{
public int x;
public test(int y)
{
x=y;
}
}
class Program
{
public static void Main(string [] args)
{
Gen<int,string> ob1 = new Gen<int,string>(420,"this in int");
Console.WriteLine(ob1.display());
Gen<float,string> ob2 = new Gen<float,string>(420.420f,"this is float");
Console.WriteLine(ob2.display());
Gen<string,char> ob3 = new Gen<string,char>("sup",'?');
Console.WriteLine(ob3.display());
Gen<test,string> ob4 = new Gen<test,string>(new test(3),"this is test object");
test t=ob4.display();
Console.WriteLine(t.x);
Console.ReadKey(true);
}
}

dotNET Lab q1 12-03-19

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Gen<yo, man>
{
yo a;
man b;
public Gen(yo temp1, man temp2)
{
a = temp1;
b = temp2;
}
public yo display()
{
Console.WriteLine(b);
return a;
}
public void check()
{
if (a.GetType() == typeof(string) && b.GetType() == typeof(string))
{
Console.WriteLine(addstring(a, b));
}
else if (a.GetType() == typeof(int) && b.GetType() == typeof(int))
{
Console.WriteLine(addint(a, b));
}
else
{
Console.WriteLine("Incompatible type");
}
}
string addstring(yo a, man b)
{
string x = a.ToString();
string y = b.ToString();
return (x + y);
}
int addint(yo a, man b)
{
string x = a.ToString();
string y = b.ToString();
int i1 = int.Parse(x);
int i2 = int.Parse(y);
return (i1 + i2);
}
}
class Program
{
public static void Main(string[] args)
{
Gen<string, string> ob1 = new Gen<string, string>("420", "this in string");
ob1.check();
Console.WriteLine();
Gen<int, int> ob2 = new Gen<int, int>(420, 300);
ob2.check();
Console.WriteLine();
Gen<int, string> ob3 = new Gen<int, string>(420, "this in string");
ob3.check();
Console.WriteLine();
Console.ReadKey(true);
}
}
}

dotNet q2 lab (25-02-19)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public delegate string StrMod(string r);
namespace ConsoleApplication16
{
public class StOps
{
public static string fun1 (string s)
{
string x;
x=s.ToUpper();
return x;
}
public string fun2 (string s)
{
string x;
x=s.Replace(" ","");
return x;
}
public string fun3(string s)
{
string x;
char[] charr;
charr = s.ToCharArray();
Array.Reverse(charr);
x = new String(charr);
Console.WriteLine(s);
Console.WriteLine(x);
if (s.Equals(x))
return "yes";
else
return "no";
}

}
class Program
{
static void Main(string[] args)
{
StOps s1 = new StOps();
StrMod del1 = new StrMod(StOps.fun1);
Console.WriteLine("Enter a sTring: ");
string e;
e=Console.ReadLine();
Console.WriteLine("Tstring in upper: "+del1(e));
del1 = new StrMod(s1.fun2);
Console.WriteLine("Enter a sTring: ");
e=Console.ReadLine();
Console.WriteLine("string without spaces: "+del1(e));
del1 = new StrMod(s1.fun3);
Console.WriteLine("Enter a sTring: ");
e = Console.ReadLine();
Console.WriteLine("palindrome: " + del1(e));
Console.ReadKey();

}
}
}
dotNet q1 LAB (26-02-19)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public delegate void DecrementEventHandler();
namespace ConsoleApplication15
{
public class CoinStore
{
int coinstore = 1000;
public event DecrementEventHandler CoinDispenseEvent;
public void dispensecoin()
{
coinstore = coinstore - 1;
if(CoinDispenseEvent!=null)
{
CoinDispenseEvent();
}
}
}
public class Secondobserver
{
public void ItooObserve()
{
Console.WriteLine("I TOO Observe");
}
}
public class EventDemo
{
public void respondtocoindespense()
{
Console.WriteLine("From Event Handler: coin despensed");
}
static void Main(string[] args)
{
EventDemo demo = new EventDemo();
Secondobserver sec = new Secondobserver();
CoinStore st = new CoinStore();
st.CoinDispenseEvent += demo.respondtocoindespense;
st.CoinDispenseEvent += sec.ItooObserve;
string str;
for(int j=0;j<20;j++)
{
Console.WriteLine("type yes and press enter to dispense coin");
str = Console.ReadLine();
if (str == "yes")
st.dispensecoin();
}
Console.ReadKey();
}
}
}

dotNet Lab 4(22-01-2019) q1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication15
{
class Program
{
int x, y;
Program()
{
x = 0;
y = 0;
}
Program(int a, int b)
{
x = a;
y = b;
}
void display()
{
Console.WriteLine("x: " + x + " y: " + y);
}
static void Main(string[] args)
{
Program p = new Program(2,3);
p.display();
Console.ReadKey(true);
}
}
}
C# and DOTNET Evaluated Lab 1 29nd Dec 2018

Q1.Create an array of strings {“day”, “night”, “Sunday”}. (a)Display its length using the Length
property. (b)Use a for loop to display each string in it. (c) use a foreach loop to find the
combined length of all the strings. (d) use another foreach loop, and the concatenation function
of the string type to concatenate all the strings into a single string, and display it.

Q2. Use the string type function to convert the string “able” into an array of chars . Put these
chars into a second array of chars called revArry, in the reverse order(use a for loop with two
indices) . Use this array (revArry) to instantiate a string which is the reverse of the original string.
Display it.

Q3.Define a Building class with members Area and floors (both int s). Provide two constructors
– one with no parameters, and another which takes two int values and initializes the Area and
floors members. In the main method of a class Program, create 3 instances of the Building
class, using the first constructor for 2 instances, and the second constructor of the third. Find
the total sum of the Area fields of all objects. Use correct access specifiers for the class,
constructors, and the members.

You might also like