Comsats University Islamabad: Department of Computer Science Lab Assignment - II

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

COMSATS UNIVERSITY

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"]);

// Keep the console window open in debug mode.


System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
}
}
// Output: 5

Ans:

using System;

class DC
{
string[] days = { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };

public int this[string index]


{
get
{

for (int j = 0; j < days.Length; j++)


{
if (days[j] == index )
{
return j;
}

throw new ArgumentOutOfRangeException(


nameof(index),
$"Day {index} is not supported should be in form \"Sun\", \"Mon\");

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();

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

//displaying the Boxes using the overloaded ToString():


Console.WriteLine("Box 1: {0}", Box1.ToString());
Console.WriteLine("Box 2: {0}", Box2.ToString());

// 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);

// Add two object as follows:


Box3 = Box1 + Box2;
Console.WriteLine("Box 3: {0}", Box3.ToString());

// volume of box 3
volume = Box3.getVolume();
Console.WriteLine("Volume of Box3 : {0}", volume);

//comparing the boxes


if (Box1 > Box2)
Console.WriteLine("Box1 is greater than Box2");
else
Console.WriteLine("Box1 is greater than Box2");

if (Box1 < Box2)


Console.WriteLine("Box1 is less than Box2");
else
Console.WriteLine("Box1 is not less than Box2");

if (Box1 >= Box2)


Console.WriteLine("Box1 is greater or equal to Box2");
else
Console.WriteLine("Box1 is not greater or equal to Box2");

if (Box1 <= Box2)


Console.WriteLine("Box1 is less or equal to Box2");
else
Console.WriteLine("Box1 is not less or equal to Box2");
if (Box3 == Box4)
Console.WriteLine("Box3 is equal to Box4");
else
Console.WriteLine("Box3 is not equal to Box4");
Console.ReadKey();
}
}

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

// 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;
}
public static bool operator ==(Box lhs, Box rhs)
{
bool status = false;
if (lhs.length == rhs.length && lhs.height == rhs.height
&& lhs.breadth == rhs.breadth)
{

status = true;
}
return status;
}
public static bool operator !=(Box lhs, Box rhs)
{
bool status = false;

if (lhs.length != rhs.length || lhs.height != rhs.height ||


lhs.breadth != rhs.breadth)
{

status = true;
}
return status;
}
public static bool operator <(Box lhs, Box rhs)
{
bool status = false;

if (lhs.length < rhs.length && lhs.height < rhs.height


&& lhs.breadth < rhs.breadth)
{

status = true;
}
return status;
}
public static bool operator >(Box lhs, Box rhs)
5
{
bool status = false;

if (lhs.length > rhs.length && lhs.height >


rhs.height && lhs.breadth > rhs.breadth)
{

status = true;
}
return status;
}
public static bool operator <=(Box lhs, Box rhs)
{
bool status = false;

if (lhs.length <= rhs.length && lhs.height


<= rhs.height && lhs.breadth <= rhs.breadth)
{

status = true;
}
return status;
}
public static bool operator >=(Box lhs, Box rhs)
{
bool status = false;

if (lhs.length >= rhs.length && lhs.height


>= rhs.height && lhs.breadth >= rhs.breadth)
{

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

Box3 = Box1 + Box2;


Console.WriteLine("Box 3: {0}", Box3.ToString());

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

if (Box1 > Box2)


Console.WriteLine("Box1 is greater than Box2");
else
Console.WriteLine("Box1 is not greater than Box2");

if (Box1 < Box2)


Console.WriteLine("Box1 is less than Box2");
else
Console.WriteLine("Box1 is not less than Box2");

if (Box1 >= Box2)


Console.WriteLine("Box1 is greater or equal to Box2");
else
Console.WriteLine("Box1 is not greater or equal to Box2");

if (Box1 <= Box2)


Console.WriteLine("Box1 is less or equal to Box2");
else
Console.WriteLine("Box1 is not less or equal to Box2");

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 (Box3 <= Box4)


Console.WriteLine("Box3 is less or equal to Box4 ");
else
Console.WriteLine("Box3 is not less or equal to Box4");

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;

if (Box4 <= Box3)


Console.WriteLine("Box4 is less or equal to Box3");
else
Console.WriteLine("Box4 is not less or equal to Box3");

}
}

3. Declare an interface IPerson with following operations:


 Add, Update, Delete
 Create abstract classes for Student and Teacher which implements IPerson interface.
 Student class contains Name, Registration, CGPA fields, Gender.
 Teacher class contains Name, Course (currently teaching course), Highest Degree, date
of joining, Gender.
 Write suitable classes to implement Student and Teacher classes
 Write a Test class which creates different instances of derived classes based on user
input. For example, a menu is shown to user consisting of 5 options.
o 1 for Add, 2 for Update, 3 for Delete, 4 for Display and 5 for exit
 ToString method should be used for Displaying record.

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
{

public string Name;


public int Registration;
public int CGPA;
public char Gender;
public void Add() { }
public void Update()
{

public void Delete()


{

}
}
//Second Case
public abstract class Teacher : IPerson
{
public string Name;
public string Course;
public string HighestDegree;
public int dateofjoining;
public char Gender;

public void Add()


{

}
public void Update()
{

9
public void Delete()
{

}
}

public class impteacher : Teacher


{

public void Add() { }


public void Update()
{

public void Delete()


{

}
}

public class impstude : Student


{

public void Add(student[] st, ref int itm) {

Again:
Console.WriteLine();
Console.Write("Enter student's ID:");
st[itm].Number = Console.ReadLine().ToString();

Console.Write("Enter student's Name:");

st[itm].Name = Console.ReadLine().ToString();

Console.Write("Enter student's Email ");


st[itm].Email = Console.ReadLine().ToString();

Console.Write("Enter student's Gender ");


st[itm].Gender = Console.ReadLine().ToString();

Console.Write("Enter reg no ");


st[itm].RegNumber = Console.ReadLine().ToString();

++itm;

public int search(student[] st, string id, int itemcount)


{
int found = -1;
for (int i = 0; i < itemcount && found == -1; i++)
{

if (st[i].Number == id) found = i;

else found = -1;


10
}

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());

int index = search(st, id.ToString(), itm);

if ((index != -1) && (itm != 0))


{
if (column_index == 1)
{
Console.Write("Enter student's Name:");

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("Invalid column ");

}
else Console.WriteLine("The record is not present ");

public void cl(Student[] st, int index)


{
st[index].Number = null;
st[index].Name = null;
st[index].Gender = null;
st[index].Email = null;
st[index].RegNumber = null;

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

if ((index != -1) && (itm != 0))


{
if (index == (itm - 1))
{

cl(st, index);
--itm;

Console.WriteLine("The record was deleted.");


}
else
{
for (int i = index; i < itm - 1; i++)
{
st[i] = st[i + 1];
cl(st, itm);
--itm;
}

}
else Console.WriteLine("The record doesn't exist.Check the ID and try
again.");

}
else Console.WriteLine("No record to delete");
}
}

public class Test {


static void Main(string[] args)
{

impstude s = new impstude();

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)
{

case 1: s.Add(st, ref itm); break;


case 2: s.Delete(st, ref itm); break;
case 3: s.Update(st, itm); break;
case 4: s.view(st, itm); break;

default: Console.WriteLine("invalid"); break;

Console.Write("Press y or Y to continue:");

confirm = Console.ReadLine().ToString();

} while (confirm == "y" || confirm == "Y");

}
catch (FormatException f) { Console.WriteLine("Invalid input"); }

Console.ReadLine();

static void display()


{

Console.WriteLine("=====================================================");

Console.WriteLine(" MENU ");

Console.WriteLine("=====================================================");

Console.WriteLine(" 1.Add student ");


Console.WriteLine(" 2.Delete student ");
Console.WriteLine(" 3.Update student");
Console.WriteLine(" 4.View all student");
}

}
}

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

You might also like