Comsats University Islamabad: Department of Computer Science Lab Assignment - II
Comsats University Islamabad: Department of Computer Science Lab Assignment - II
ISLAMABAD
Department of Computer
Science
Lab Assignment – II
CLO-(C2)
HASSAN AHMED
FA18-BSE-031
BSE-6A
1
1. Declare a class named DayCollection that stores the days of the week. Declare a get accessor that
takes a string, the name of a day, and returns the corresponding integer. For example, Sunday
will return 0, Monday will return 1, and so on. Create indexer in DayCollection. Following is a
demo class.
class Program
{
static void Main(string[] args)
{
DayCollection week = new DayCollection();
System.Console.WriteLine(week["Fri"]);
// Raises ArgumentOutOfRangeException
System.Console.WriteLine(week["Made-up Day"]);
Ans:
using System;
class DC
{
string[] days = { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };
2
}
}
class Program
{
static void Main(string[] args)
{
var week = new DayCollection();
Console.WriteLine(week["Mon"]);
try
{
Console.WriteLine(week["Made-up day"]);
}
catch (ArgumentOutOfRangeException e)
{
Console.WriteLine($"Not supported input: {e.Message}");
}
}
}
// Output: 5
2. Extend the functionality of Box class to overload some operators. Assume following driver class
and overload all used operators. Consider that the box is greater or smaller than other box by
comparing all of its dimensions (i.e. length, breadth and height).
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
Box Box4 = new Box();
// 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);
3
// volume of box 2
volume = Box2.getVolume();
Console.WriteLine("Volume of Box2 : {0}", volume);
// volume of box 3
volume = Box3.getVolume();
Console.WriteLine("Volume of Box3 : {0}", volume);
Ans:
using System;
namespace ConsoleApp13
{
class Box
{
private double length;
private double breadth;
private double height;
4
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;
}
status = true;
}
return status;
}
public static bool operator !=(Box lhs, Box rhs)
{
bool status = false;
status = true;
}
return status;
}
public static bool operator <(Box lhs, Box rhs)
{
bool status = false;
status = true;
}
return status;
}
public static bool operator >(Box lhs, Box rhs)
5
{
bool status = false;
status = true;
}
return status;
}
public static bool operator <=(Box lhs, Box rhs)
{
bool status = false;
status = true;
}
return status;
}
public static bool operator >=(Box lhs, Box rhs)
{
bool status = false;
status = true;
}
return status;
}
public override string ToString()
{
return String.Format("({0}, {1}, {2})", length, breadth, height);
}
}
class Tester
{
static void Main(string[] args)
{
Box Box1 = new Box();
Box Box2 = new Box();
Box Box3 = new Box();
Box Box4 = new Box();
double volume = 0.0;
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
6
Console.WriteLine("Box 1: {0}", Box1.ToString());
Console.WriteLine("Box 2: {0}", Box2.ToString());
volume = Box1.getVolume();
Console.WriteLine("Volume of Box1 : {0}", volume);
volume = Box2.getVolume();
Console.WriteLine("Volume of Box2 : {0}", volume);
volume = Box3.getVolume();
Console.WriteLine("Volume of Box3 : {0}", volume);
if (Box1 != Box2)
Console.WriteLine("Box1 is not equal to Box2");
else
Console.WriteLine("Box1 is not greater or equal to Box2");
Box4 = Box3;
if (Box2 == Box3)
Console.WriteLine("Box2 is equal to Box3");
else
Console.WriteLine("Box2 is not equal to Box3");
Console.ReadKey();
if (Box3 == Box4)
Console.WriteLine("Box3 is equal to Box4");
else
Console.WriteLine("Box3 is not equal to Box4");
7
Console.ReadKey();
if (Box3 != Box4)
Console.WriteLine("Box3 is not equal to Box4");
else
Console.WriteLine("Box3 is not greater or equal to Box4");
Box4 = Box3;
if (Box4 == Box3)
Console.WriteLine("Box4 is equal to Box3 ");
else
Console.WriteLine("Box4 is not equal to Box3");
Console.ReadKey();
if (Box4 != Box3)
Console.WriteLine("Box4 is not equal to Box3");
else
Console.WriteLine("Box4 is not greater or equal to Box3");
Box4 = Box3;
}
}
8
o Teacher details must be shown like Name, Course.
o Student details must be shown like Name, Registration, CGPA.
Ans :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace abc {
public interface IPerson
{
void Add();
void Update();
void Delete();
}
//First Case
public abstract class Student : IPerson
{
}
}
//Second Case
public abstract class Teacher : IPerson
{
public string Name;
public string Course;
public string HighestDegree;
public int dateofjoining;
public char Gender;
}
public void Update()
{
9
public void Delete()
{
}
}
}
}
Again:
Console.WriteLine();
Console.Write("Enter student's ID:");
st[itm].Number = Console.ReadLine().ToString();
st[itm].Name = Console.ReadLine().ToString();
++itm;
return found;
}
public void Update(student[] st, int itm)
{
string id;
int column_index;
Console.Write("Enter student's ID:");
id = Console.ReadLine();
Console.Write("Which field you want to update(1-4)?:");
column_index = int.Parse(Console.ReadLine());
st[index].Name = Console.ReadLine().ToString();
}
else if (column_index == 2)
{
Console.Write("Enter student's Sex(F or M):");
st[index].Gender = Console.ReadLine().ToString();
}
else if (column_index == 3)
{
Console.Write("Enter student's Email :");
st[index].Email = Console.ReadLine().ToString();
}
else if (column_index == 4)
{
Console.Write("Enter student's reg no ");
st[index].RegNumber = Console.ReadLine().ToString();
}
}
else Console.WriteLine("The record is not present ");
11
public void Delete(Student[] st, ref int itm)
{
string id;
int index;
if (itm > 0)
{
Console.Write("Enter student's ID:");
id = Console.ReadLine();
index = search(st, id.ToString(), itm);
cl(st, index);
--itm;
}
else Console.WriteLine("The record doesn't exist.Check the ID and try
again.");
}
else Console.WriteLine("No record to delete");
}
}
try
{
Student[] st = new Student[20]; //create an array to store only 20
students'records for testing.
int itm = 0;
//show menu
display();
int yourchoice;
string confirm;
do
{
12
Console.Write("Enter your choice(1-4):");
yourchoice = int.Parse(Console.ReadLine());
switch (yourchoice)
{
Console.Write("Press y or Y to continue:");
confirm = Console.ReadLine().ToString();
}
catch (FormatException f) { Console.WriteLine("Invalid input"); }
Console.ReadLine();
Console.WriteLine("=====================================================");
Console.WriteLine("=====================================================");
}
}
13
Note:
To read / write a field, implement properties where required.
For question number 3, you may use generic for simplicity. Similarly, you may use array
for static collection or list etc. for dynamic collection.
Proper exception handling should be employed (e.g., Handle all appropriate exceptions)
Clearly mention class, section, submission date, assignment type & number e.g., ‘Lab
Assignment – II’ on the cover page
14