0% found this document useful (0 votes)
60 views

Code Final MPT

The document defines classes for handling activities involving string operations and exception handling. The Activity class defines properties for two strings, an operator, and a constructor to initialize the properties. The Source class contains methods to handle exceptions during operations on Activity objects and perform the operations. The HandleException method catches exceptions related to null/empty strings or invalid operators and returns a message. The Operate method performs the operation based on the operator and returns the result. Main method creates an Activity, calls the Source methods to operate and handle exceptions, and outputs the results.

Uploaded by

Harshada Bansode
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Code Final MPT

The document defines classes for handling activities involving string operations and exception handling. The Activity class defines properties for two strings, an operator, and a constructor to initialize the properties. The Source class contains methods to handle exceptions during operations on Activity objects and perform the operations. The HandleException method catches exceptions related to null/empty strings or invalid operators and returns a message. The Operate method performs the operation based on the operator and returns the result. Main method creates an Activity, calls the Source methods to operate and handle exceptions, and outputs the results.

Uploaded by

Harshada Bansode
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

using System;

class Activity

public string String1;

public string String2;

public char Operator;

public Activity(string str1, string str2, char op)

string1 = str1;

string2 = str2;

Operator = op;

class Source

string result,msg;

public string HandleException(Activity a)

try
{

if (String.IsNullOrWhiteSpace(a.String1) ||
String.IsNullOrWhiteSpace(a.String2))

throw new ArgumentNullException("Null values found");

else if (a.Operator!='+' && a.Operator!='-')

throw new Exception($"{ a.Operator }");

else

msg = "No Exception Found";

catch (ArgumentNullException e)

msg = e.Message;

catch (Exception e)

{
msg = e.Message;

return msg;

public string Operate(Activity a)

char choice = a.Operator;

switch(choice)

case '+':

result = a.String1 + a.String2;

break;

case '-':

result = a.String1.Replace(a.String2, "");

break;

return result;

static void Main(string[] args)

Activity a = new Activity("hello", "world",'+');

Source s = new Source();


string emsg = s.HandleException(a);

Console.WriteLine(emsg);

if(emsg=="No Exception Found")

string toprint = s.Operate(a);

Console.WriteLine(toprint);

Console.Read();

Codethon Solutions
Codethon-2 Solutions:-
Problem-1 Employee –
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
public class Employee
{
//Write your code here
private string empCode,empName;
private double empSal;
private char deptCode;
private static int empCounter = 1000;
public Employee(string empName,double empSal,char deptCode)
{
this.empName = empName;
this.empSal = empSal;
this.deptCode = deptCode;
empCounter++;
this.empCode = generateEmployeeCode(deptCode);
}
public Employee(string empName,double empSal)
{
this.empName = empName;
this.empSal = empSal;
this.deptCode = 'A';
empCounter++;
this.empCode = generateEmployeeCode(deptCode);
}
public string EmpName{
get{return empName;}
set{empName=value;}
}
public double EmpSal{
get{return empSal;}
set{empSal=value;}
}
public string EmpCode{
get{return empCode;}
}
public char DeptCode{
get{return deptCode;}
set{deptCode=value;}
}
private string generateEmployeeCode(char deptCode)
{
return empCounter+deptCode.ToString();
}
public string getEmployeeDetails()
{
return "Code-"+empCode+",Name-"+empName+",Salary-
"+empSal.ToString()+",Department-"+deptCode.ToString();
}
}
CD 2- Problem-2 Product :-
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
public class Product {
//Write your code here
private String productCode, productName;
private double productPrice;
private char categoryCode;
private static int prodCounter = 100;
private String generateProductCode()
{
prodCounter++;
return
String.Concat(this.categoryCode,Convert.ToString(prodCounter));
}
public String getProductDetails()
{
return String.Format("Code-{0},Name-{1},Price-{2},Category-
{3}",this.productCode,
this.productName, this.productPrice, this.categoryCode);
}
public Product() {}
public Product(String productName, double productPrice, char
categoryCode) {
this.productName = productName;
this.productPrice = productPrice;
this.categoryCode = categoryCode;
this.productCode = generateProductCode();
}
public Product(String productName, double productPrice) {
this.productName = productName;
this.productPrice = productPrice;
this.categoryCode = 'E';
this.productCode = generateProductCode();
}
public String ProductCode {
get {return productCode; }
set {productCode = value; }
}
public String ProductName {
get {return productName; }
set {productName = value; }
}
public double ProductPrice {
get {return productPrice; }
set {productPrice = value; }
}
public char CategoryCode {
get {return categoryCode; }
set {categoryCode = value; }
}
}
class Example {
public static void Main() {
Product p=new Product("Laptop",45000.00, 'E');
Console.WriteLine(p.getProductDetails());
}
}
CD 2 Problem-3 Player:-
class Player
{
string name;
string country;
int age;
DateTime doj;
public string Name
{ get { return name; } set { name = value; } }
public string Country
{ get { return country; } set { country = value; } }
public int Age
{
get { return age; }
set { age = value; }
}
public DateTime Doj { get { return doj; }set { doj = value; } }
public Player() { }
public Player(string name, string country, int age, DateTime doj)
{
this.name = name;
this.country = country;
this.age = age;
this.doj = doj;
}
public override string ToString()
{
return "Name-" + Name + ",Country-" + Country + ",Age-" + Age;
}
public bool Equals(Player p)
{
if (this.name.ToLower() == p.name.ToLower() &
this.country.ToLower() ==
p.country.ToLower())
return true;
else
return false;
}
}
Codethon-3 Solutions:-
CD3 Problem-1 Cab –
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Cab
{
//Write your code here
private string cid,regNo,type;
private int capacity;
private double costPerKm;
public string Cid
{
get{return cid;}
set{cid=value;}
}
public string Type
{
get{return type;}
set{type=value;}
}
public string RegNo
{
get{return regNo;}
set{regNo=value;}
}
public int Capacity
{
get{return capacity;}
set{capacity=value;}
}
public double CostPerKm
{
get{return costPerKm;}
set{costPerKm=value;}
}
public Cab()
{
}
public Cab(string cid,string regno,string type,int capacity,double
costPerKm)
{
this.regNo=regno;
this.cid=cid;
this.type=type;
this.capacity=capacity;
this.costPerKm=costPerKm;
}
public override string ToString()
{
//Write your code here
return
"RegistrationNumber:"+RegNo+"VehicleType:"+Type+"Capacity:"+C
apacity+"CostPerKm:"+
CostPerKm+"";
}
public bool Equals(Cab c)
{
//Write your code here
if(this.RegNo.ToLower()==c.RegNo.ToLower() &&
this.Type.ToLower()==c.Type.ToLower())
{
return true;
}
else
return false;
}
}
CD3 Problem-2 Movie –
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Movie
{
private string name;
private string movieId;
private string director;
private int rating;
private DateTime releaseDate=new DateTime();
public string Name{
get{return name;}
set{name=value;}
}
public string MovieId{
get{return movieId;}
set{movieId=value;}
}
public string Director{
get{return director;}
set{director=value;}
}
public int Rating{
get{return rating;}
set{rating=value;}
}
public DateTime ReleaseDate{
get{return releaseDate;}
set{releaseDate=value;}
}
public Movie(){}
public Movie(string name,string movieId,string director,int rating,
DateTime releaseDate){
this.name=name;
this.movieId=movieId;
this.director=director;
this.rating=rating;
this.releaseDate=releaseDate;
}
//Write your code here
public override string ToString()
{
//Write your code here.
return "Name:"+name+"\n"+
"MovieId:"+movieId+"\n"+
"Director:"+director+"\n"+
"Rating:"+rating+"\n"+
"ReleaseDate:"+releaseDate;
}
public bool Equals(Movie m)
{
//Write your code here
if((this.name.ToLower()==m.name.ToLower())
&&(this.movieId.ToLower()==m.movieId.ToLower()))
return true;
else
return false;
}
}
CD3 Problem-3 Product :-
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Product
{
//Write your code here
private string name,productCode,brandName;
private int stockLeft;
private double price;
private DateTime expiryDate;
public string Name{
get{return name;}
set{name=value;}
}
public string ProductCode{
get{return productCode;}
set{productCode=value;}
}
public string BrandName{
get{return brandName;}
set{brandName=value;}
}
public int StockLeft{
get{return stockLeft;}
set{stockLeft=value;}
}
public double Price{
get{return price;}
set{price=value;}
}
public DateTime ExpiryDate{
get{return expiryDate;}
set{expiryDate=value;}
}
public Product(){}
public Product(string name,string productCode,string brandName,int
stockLeft,double
price, DateTime expiryDate){
this.name=name;
this.productCode=productCode;
this.brandName=brandName;
this.stockLeft=stockLeft;
this.price=price;
this.expiryDate=expiryDate;
}
public override string ToString()
{
//Write your code here
return
"Name:"+Name+"\n"+"ProductCode:"+ProductCode+"\n"+"BrandNa
me:"+BrandName+"\n"
+"StockLeft:"+StockLeft.ToString()+"\n"+"Price:"+Price.ToString()+"\n
"+"ExpiryDate:"+Expiry
Date.ToString();
}
public bool Equals(Product p)
{
//Write your code here
if(this.Name.ToLower()==p.Name.ToLower() &&
this.ProductCode.ToLower()==p.ProductCode.ToLower()){
return true;
}
else{
return false;
}
}
}
Codethon-4 Solutions:-
CD4 Problem-1 Product and Shop Class –

CD4 Problem-2 Passenger and Cab Class –

Final Assesment Webinar Codes:-


Question-1 (OOPs):-
class Player
{
string name;
string country;
int age;
DateTime doj;
public string Name
{ get { return name; } set { name = value; } }
public string Country
{ get { return country; } set { country = value; } }
public int Age
{
get { return age; }
set { age = value; }
}
public DateTime Doj { get { return doj; }set { doj = value; } }
public Player() { }
public Player(string name, string country, int age, DateTime doj)
{
this.name = name;
this.country = country;
this.age = age;
this.doj = doj;
}
public override string ToString()
{
return "Name-" + Name + ",Country-" + Country + ",Age-" + Age;
}
public bool Equals(Player p)
{
if (this.name.ToLower() == p.name.ToLower() &
this.country.ToLower() == p.country.ToLower())
return true;
else
return false;
}
}
public override string ToString()
{
return "Name-" + Name + ",Country-" + Country + ",Age-" + Age;
}
public bool Equals(Player p)
{
if (this.name.ToLower() == p.name.ToLower() &
this.country.ToLower() == p.country.ToLower())
return true;
else
return false;
}}}
Question-2 (Collections) :-
class Player
{
public string Fname;
public string Lname;
public string Country;
public int Rating;
}
class Match
{
public List<Player> team1 = new List<Player>();
public List<Player> team2 = new List<Player>();
public Match(List<Player> l1, List<Player> l2)
{
this.team1 = l1;
this.team2 = l2;
}
public Match() { }
public void AddPlayer(Player p, string team)
{
if (team == "Team1")
team1.Add(p);
if (team == "Team2")
team2.Add(p);
}
public bool RemovePlayer(string fname)
{
bool flag;
Player p1 = team1.Find(x => x.Fname == fname);
Player p2= team2.Find(x => x.Fname == fname);
if( p1== null & p2==null)
{
flag = false;
}
else
{
if (p1 != null)
team1.Remove(p1);
if (p2 != null)
team2.Remove(p2);
flag = true;
}
return flag;
}
}
class MatchBO
{
public List<Player> FindPlayer(List<Player> playerList, string
country)
{
List<Player> l1 = playerList.FindAll(x => x.Country == country);
return l1;
}
public List<Player> FindPlayer(List<Player> playerList, int rating)
{
List<Player> l1 = playerList.FindAll(x => x.Rating == rating);
return l1;
}
public Hashtable CountryWiseCount(List<Player> playerList)
{
Hashtable ht = new Hashtable();
var res = from p in playerList
group p by p.Country into grp
select new { Country = grp.Key, Cnt = grp.Count() };
foreach( var x in res)
{
ht.Add(x.Country, x.Cnt);
}
return ht;
}
}
---END---

Collections- Product
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace Practice_2
{
class Product
{
public string ProductCode, Name, Brand;
public double Price;
public Product() { }
public Product(string prodcode, string name, double
price,string brand)
{
ProductCode = prodcode;
Name = name;
Price = price;
Brand = brand;
}
}
class Shop
{
string Name;
List<Product> Prodlist = new List<Product>();
public Shop() { }
public Shop(string name, List<Product> productlist)
{
Name = name;
Prodlist = productlist;
}
public void AddproductToShop(Product p)
{
int flag = 0;
foreach (var str in Prodlist)
{
if (str.ProductCode == p.ProductCode && str.Name ==
p.Name)
{
flag = 1;
}
}
if (flag == 0)
Prodlist.Add(p);
}
public bool RemoveProductFromShop(string productCode)
{
foreach (var str in Prodlist)
{
if (str.ProductCode == productCode)
{
Prodlist.Remove(str);
return true;
}
}
return false;
}
}
class ProductBO
{
public List<Product> FindProduct(List<Product>
prodlist, string brand)
{
List<Product> prod4 = new List<Product>();
foreach (var str in prodlist)
{
if (str.Brand == brand)
prod4.Add(str);
}
return prod4;
}
public List<Product> FindProduct(List<Product>
prodlist, double price)
{
List<Product> prod2 = new List<Product>();
foreach (var str in prodlist)
{
if (str.Price == price)
prod2.Add(str);
}
return prod2;
}
public Hashtable BrandWiseCount(List<Product>
productList)
{
Hashtable ht = new Hashtable();
var res = from p in productList
group p by p.Brand into brand
select new {Brand=brand.Key,Cnt=brand.Count() };
foreach(var x in res)
{
ht.Add(x.Brand, x.Cnt);
}
return ht;
}
}
}
Collections- Movie/Theatre
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace Practice_2
{
class Movie
{
public string MovieId;
public string Name;
public int Rating;
public string Language;
public Movie() { }
public Movie(string MovieId, string Name, int Rating, string }
Language)
{
this.MovieId = MovieId;
this.Name = Name;
this.Rating = Rating;
this.Language = Language;
}
class Theatre
{
public string Name;
public List<Movie> MovieList = new List<Movie>();
public Theatre() { }
public Theatre(string Name, List<Movie> MovieList)
{
this.Name = Name;
this.MovieList = MovieList;
}
public void AddMovieToTheatre(Movie m)
{
int flag = 0;
foreach(var mov in MovieList)
{
if(mov.Name==m.Name && mov.Language==m.Language)
{
flag = 1;
}
}
if(flag==0) }
{
MovieList.Add(m);
}
public bool UpdateMovie(string movieid, int newrating)
{
foreach(var mov in MovieList)
{
if(mov.MovieId==movieid)
{
mov.Rating = newrating;
return true;
}
}
return false;
}
}
class MovieBo
{
public List<Movie> FindMovie(List<Movie>
MovieList,string Language)
{
List<Movie> List = new List<Movie>();
foreach(var mov in MovieList)
{
if(mov.Language==Language)
{
List.Add(mov);
}
}
return List;
}
public List<Movie> FindMovie(List<Movie>
MovieList, int Rating)
{
List<Movie> List = new List<Movie>();
foreach (var mov in MovieList)
{
if (mov.Rating == Rating)
{
List.Add(mov);
}
}
return List;
}
public Hashtable LanguageWiseCount(List<Movie>
MovieList)
{
Hashtable ht = new Hashtable();
var res = from m in MovieList
group m by m.Language into lang
select new {Movie=lang.Key,Cnt=lang.Count()};
foreach(var x in res)
{
ht.Add(x.Movie, x.Cnt);
}
return ht;
}
}
}
Collections- Student
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Practice_2
{
class Student
{
public string Name { get; set; }
public int Score { get; set; }
}
class StudentImplementation
{
public StudentImplementation() { }
public string NameofAllStudents(IList<Student>
students)
{
string ans = "";
foreach(Student s in students)
{
Console.WriteLine(s.Name);
ans = ans + s.Name + " ";
}
return ans;
}
public int
TotalScoreOfAllStudents(IList<Student>
students)
{
int res = 0;
foreach(Student s in students)
{
res = res + s.Score;
}
Console.WriteLine(res);
return res;
}
public double AverageScore(IList<Student> d b
students) o l
{ u e
res = 0;
double i = 0;
double average = 0;
foreach(Student s in students)
{
i++;
res = res + s.Score;
}
if(i!=0)
average = res / i;
Console.WriteLine(res);
return average;
}
}
}
Collections- Player
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace Practice1
{
class Player
{
public string Fname, Lname, Country;
public int Rating;
}
class Match
{
public List<Player> team1 = new List<Player>();
public List<Player> team2 = new List<Player>();
public Match() { }
public Match(List<Player>team1,List<Player>team2)
{
this.team1 = team1;
this.team2 = team2;
}
public void AddPlayer(Player p, string team)
{
if(team == "Team1")
{
team1.Add(p);
}
if (team == "Team2")
{
team2.Add(p);
}
}
public bool RemovePlayer(string fname)
{
bool flag;
Player p1 = team1.Find(x => x.Fname == fname);
Player p2 = team2.Find(x => x.Fname == fname);
if(p1==null && p2==null)
{
flag = false;
}
else
{
if(p1!=null)
{
team1.Remove(p1);
}
if (p2 != null)
{
team1.Remove(p2);
}
flag = true;
}
return flag;
}
}
class MatchBo
{
public List<Player>FindPlayer(List<Player>playerList,string
country)
{
List<Player> playerList1 = new List<Player>();
foreach(var player in playerList)
{
if(player.Country==country)
{
playerList1.Add(player);
}
}
return playerList1;
}
public List<Player>FindPlayer(List<Player>playerList,int
rating)
{
List<Player> playerList2 = new List<Player>();
foreach(var player in playerList)
{
if(player.Rating==rating)
{
playerList2.Add(player);
}
}
return playerList2;
}
public Hashtable CountryWiseCount(List<Player> playerlist)
{
Hashtable ht = new Hashtable();
var res = from p in playerlist
group p by p.Country into country
select new { Country=country.Key,Cnt=country.Count()};
foreach(var x in res)
{
ht.Add(x.Country, x.Cnt);
}
return ht;
}
}
}

Assessment Set-5:-

SENTINAL:-

using System;

using System.Collections.Generic;

using System.Threading.Tasks;

using System.Linq;

using System.Text;

class Program

public string SENTINAL = "~";


public string JoinIt(string[] input)

string str = string.Join(SENTINAL, input);

return str;

public string[] SplitIt(string input)

string[] str = input.Split('~');

return str;

public StringBuilder Reverse(string input)

string[] str = input.Split(' ');

StringBuilder sa = new StringBuilder();

for (int i = str.Length - 1; i > 0; i--)

sa.Append(str[i]);

sa.Append(" ");

return sa;

public string PadLeft(string input, int n, char a)


{

string str = input.PadLeft(n, a);

return str;

static void Main(string[] args)

string[] arr = { "one", "two", "three" };

string split = "one~two~three~four~five~six";

string reverse = "Seven two three";

string padleft = "OneTwoThree";

Program p = new Program();

Console.WriteLine(p.JoinIt(arr));

string[] str = p.SplitIt(split);

foreach(string item in str)

Console.WriteLine(item + " ");

Console.WriteLine();

Console.WriteLine(p.Reverse(reverse));

Console.WriteLine(p.PadLeft(padleft, 15, '~'));

Console.ReadLine();
}

Player:-

using System;

using System.Collections.Generic;

using System.Linq;

using System.IO;

using System.Text;

using System.Threading.Tasks;

public class Player

public string Name { get; set; }

public string Team { get; set; }

public int Score { get; set; }

public class PlayerImplementation


{

public double Average(IList<Player> players)

double sc = 0;

foreach (var item in players)

sc += item.Score;

sc = sc / players.Count;

return sc;

public int Min(IList<Player> players)

int min = 999999;

foreach (var item in players)

if (min > item.Score)

min = item.Score;

return min;
}

public int Max(IList<Player> players)

int max = 0;

foreach (var item in players)

if (max < item.Score)

max = item.Score;

return max;

class Program

public static void Main(string[] args)

IList<Player> players = new List<Player>();

players.Add(new Player { Name = "Sarah", Team =


"India", Score = 30 });

players.Add(new Player { Name = "Ross", Team =


"India", Score = 60 });
players.Add(new Player { Name = "Jen", Team = "USA",
Score = 70 });

players.Add(new Player { Name = "Lisa", Team =


"USA", Score = 40 });

PlayerImplementation p = new PlayerImplementation();

Console.WriteLine(p.Average(players));

Console.WriteLine(p.Min(players));

Console.WriteLine(p.Max(players));

Car:-

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.IO;

using System.Xml.Serialization;
public class Car

public string Make { get; set; }

public string Model { get; set; }

public int Year { get; set; }

public string Color { get; set; }

public string Serialize(Car car)

FileStream fs = new FileStream(@"output.xml",


FileMode.OpenOrCreate,

FileAccess.ReadWrite);

XmlSerializer xs = new XmlSerializer(typeof(Car));

xs.Serialize(fs, car);

fs.Close();

return "Successful";

public Car DeSerialize(string xmlPath)

FileStream fs = new FileStream(@"output.xml",


FileMode.OpenOrCreate,

FileAccess.ReadWrite);

XmlSerializer xs = new XmlSerializer(typeof(Car));


Car cl = (Car)xs.Deserialize(fs);

fs.Close();

return cl;

public Car()

public Car(string make, string model, int year, string


color)

this.Make = make;

this.Model = model;

this.Year = year;

this.Color = color;

class Program

static void Main(string[] args)

{
Car carObj = new Car();

carObj = new Car("Jaguar", "VCSD", 2050,


"Orange");

carObj = new Car("Bentley", "Jaguar", 2020,


"Blue");

string xmlpath = @"output.xml";

string msg = carObj.Serialize(carObj);

Car desrObj = carObj.DeSerialize(xmlpath);

Console.WriteLine(msg);

Console.WriteLine(desrObj.Make + " " +


desrObj.Model + " "

+ desrObj.Year + " " + desrObj.Color);

Console.ReadLine();

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ConsoleApp38
{

class Source

public int Add(out int a)

a = 30;

return a + 3;

public int Add(int a)

return a + 3;

public int Add (int a ,int b)

return a + b;

public string Add(int a,int b,string msg)

return a + b + msg;

}
static void Main(string[] args)

Source s = new Source();

int num = 20;

Console.WriteLine(s.Add(20));

Console.WriteLine(s.Add(out num));

Console.WriteLine(s.Add(20, 12));

Console.WriteLine(s.Add(20,12, "One two"));

Console.ReadLine();

You might also like