C# Hands-On
C# Hands-On
C# Hands-On
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
3) Age Calculation
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
int birthyear=Int32.Parse(dateOfBirth.Substring(6,4));
int birthmonth=Int32.Parse(dateOfBirth.Substring(3,2));
int birthdate=Int32.Parse(dateOfBirth.Substring(0,2));
var t=DateTime.Today;
var a=(t.Year*100+t.Month)*100+t.Day;
var b=(birthyear*100+birthmonth)*100+birthdate;
return (a-b-400)/10000;
}
}
}
4) StringConcatenateCoding exercise
using System;
5) Quiz Competition
using System;
namespace JaggedArray
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Enter the number of teams:");
int teams = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(GetTotalScore(teamsArray));
}
6) Reverse a Sentence
using System;
Console.WriteLine("Enter a string");
sentence = Console.ReadLine();
7) Account Details
using System;
public class Account
{
private int id;
private string accountType;
private double balance;
public int Id
{
get{ return id;}
set{ id = value;}
}
if(ac.WithDraw(withdraw))
{
Console.WriteLine("New Balance: " + ac.Balance);
}
}
}
8) Openable Interface
using System;
if(ch == 'T')
Console.WriteLine(t.OpenSesame());
else if(ch == 'P')
Console.WriteLine(p.OpenSesame());
}
}
9) Game Inheritance
using System;
Console.WriteLine("Enter a game");
g.Name = Console.ReadLine();
Console.WriteLine(g.ToString());
Console.WriteLine(gt.ToString());
}
}
using System;
switch(op)
{
case '+':
Console.WriteLine("Result of {0} {1} {2} is {3}", num1, op, num2,
c.Addition(num1, num2));
break;
case '-':
Console.WriteLine("Result of {0} {1} {2} is {3}", num1, op, num2,
c.Subtraction(num1, num2));
break;
case '*':
Console.WriteLine("Result of {0} {1} {2} is {3}", num1, op, num2,
c.Multiplication(num1, num2));
break;
case '/':
double remainder;
Console.WriteLine("Result of {0} {1} {2} is {3}\nRemainder = {4}",
num1, op, num2, c.Division(num1, num2, out remainder), remainder);
break;
default:
Console.WriteLine("Invalid Operator");
break;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/*
Substring(Int32)
Retrieves a substring from this instance. The substring starts at a specified
character position and continues to the end of the string.
Substring(Int32, Int32)
Retrieves a substring from this instance. The substring starts at a specified
character position and has a specified length.
*/
if (bookCode.Length == 18)
{
if(bookCode.Substring(0,3) == "101" || bookCode.Substring(0,3) ==
"102" || bookCode.Substring(0,3) == "103")
Console.WriteLine("Department Code : {0}",
bookCode.Substring(0,3));
else
Console.WriteLine(" Invalid Department Code");
char ch = Convert.ToChar(bookCode.Substring(12,1));
if(Char.IsLetter(ch))
{
if(Char.IsNumber(Convert.ToChar(bookCode.Substring(13,1))) &&
Char.IsNumber(Convert.ToChar(bookCode.Substring(14,1))) &&
Char.IsNumber(Convert.ToChar(bookCode.Substring(15,1))) &&
Char.IsNumber(Convert.ToChar(bookCode.Substring(16,1))) &&
Char.IsNumber(Convert.ToChar(bookCode.Substring(17,1))))
{
Console.WriteLine("Book ID : " + bookCode.Substring(12,6));
}
} else
{
Console.WriteLine("Invalid Book ID");
}
}
else
Console.WriteLine("Invalid Book Code");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public static void Main(string[] args) //DO NOT CHANGE the 'Main' method
signature
{
//Implement your code here
string flightNumber;
string timeLeft;
timeLeft = flightStatus(flightNumber);
Console.WriteLine(timeLeft);
}
}
Product.cs
using System;
using System.Collections.Generic;
using System.Text;
// Implement Properties
public string ProductName
{
set { this._productName = value; }
get { return this._productName; }
}
public string SerialNumber
{
set { this._serialNumber = value; }
get { return this._serialNumber; }
}
public DateTime PurchaseDate
{
set { this._purchaseDate = value; }
get { return this._purchaseDate; }
}
public double Cost
{
set { this._cost = value; }
get { return this._cost; }
}
public override string ToString()
{
return String.Format("{0,-15}{1,-15}{2,-15}{3,-15}", ProductName,
SerialNumber, String.Format("{0:d}", PurchaseDate), Cost);
}
}
Program.cs
using System;
using System.Collections;
using System.Globalization;
using System.IO;
public class Program //DO NOT CHANGE the name of class 'Program'
{
public static void Main(string[] args) //DO NOT CHANGE 'Main' Signature
{
}
Console.WriteLine("{0}", String.Format("{0,-15}{1,-15}{2,-15}{3,-15}",
"Product Name", "Serial Number", "Purchase Date", "Cost"));
foreach (var obj in arlobj)
{
Product prd = (Product)obj;
Console.WriteLine("{0}", prd.ToString());
}
}
Vehicle.cs
namespace LinqApp1
{
public class Vehicle
{
public String VehicleId{get; set; }
public String VehicleName{ get; set; }
public String Brand { get; set; }
public int ReleaseYear { get; set; }
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
};
static void Main(string[] args) //DO NOT Change this 'Main' signature
{
//Implement your code here
Console.WriteLine("Enter From Year:");
int fromYear = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter To Year:");
int toYear = Convert.ToInt32(Console.ReadLine());
GetVehicleName(fromYear, toYear);
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ADO_Net_App1
{
//Do not change any code under this class
public class Program
{
static void Main(string[] args)
{
TraineeBO objTraineeBO = new TraineeBO();
TraineeBL objTraineeBL = new TraineeBL();
string traineeName = string.Empty;
string batchCode = string.Empty;
int numberOfTrainees =0;
try
{
Console.WriteLine("Enter number of trainees:");
numberOfTrainees = int.Parse(Console.ReadLine());
}
catch(FormatException traineeNumberException)
{
Console.WriteLine(traineeNumberException.Message);
Environment.Exit(0);
}
try
{
Console.WriteLine("Enter Trainee Id:");
objTraineeBO.TraineeId = long.Parse(Console.ReadLine());
}
catch(FormatException idException)
{
Console.WriteLine(idException.Message);
Environment.Exit(0);
}
try
{
Console.WriteLine("Enter Trainee Batch Code:");
batchCode = Console.ReadLine();
if (string.IsNullOrEmpty(batchCode))
throw new Exception("Batch Code can not be null or empty");
else
objTraineeBO.BatchCode = batchCode;
}
catch(Exception batchCodeException)
{
Console.WriteLine(batchCodeException.Message);
Environment.Exit(0);
}
}
}
}
}
}
DBConnection.cs
namespace ADO_Net_App1
{
public class DBConnection
{
public static String connStr = "User Id=admin;Password=admin;Data
Source=localhost:1521/XE;";
}
}
TraineeBO.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ADO_Net_App1
{
public class TraineeBO
{
//Write your code here
public long traineeId;
public string traineeName;
public string batchCode;
public TraineeBO()
{
TraineeDA.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using System.Configuration;
namespace ADO_Net_App1
{
public class TraineeDA
{
public string ConnectionString
{
get
{
return DBConnection.connStr;
}
}
try
{
OracleConnection con = new OracleConnection(ConnectionString);
con.Open();
string query = "insert into tblTrainee values(" + objBO.TraineeId +
",'" + objBO.TraineeName + "','" + objBO.BatchCode + "')";
OracleCommand cmd = new OracleCommand(query, con);
int i = cmd.ExecuteNonQuery();
if (i > 0)
return true;
else
return false;
}
catch(Exception e)
{
return false;
}
}
TraineeBL.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ADO_Net_App1
{
public class TraineeBL
{
public bool SaveTraineeDetails(TraineeBO objBO)
{
TraineeDA tDA = new TraineeDA();
bool res = tDA.AddTraineeDetails(objBO);
if (res)
return true;
else
return false;
}
}
}
Program.cs