POLIMORPHISM AND ABSTRACTION DEMONSTRATION
using System;
using System.Collections.Generic;
// Abstract base class
abstract class Student
{
public string Name { get; set; }
public Student(string name)
{
Name = name;
}
// Abstract method
public abstract string CalculateGrade();
}
// Undergraduate student class
class UndergraduateStudent : Student
{
private double Score;
public UndergraduateStudent(string name, double score)
: base(name)
{
Score = score;
}
public override string CalculateGrade()
{
if (Score >= 90) return "A";
if (Score >= 80) return "B";
if (Score >= 70) return "C";
if (Score >= 60) return "D";
return "F";
}
}
// Graduate student class
class GraduateStudent : Student
{
private double Score;
public GraduateStudent(string name, double score)
: base(name)
{
Score = score;
}
public override string CalculateGrade()
{
if (Score >= 85) return "A";
if (Score >= 75) return "B";
if (Score >= 65) return "C";
return "F";
}
}
class Program
{
static void Main()
{
List<Student> students = new List<Student>()
{
new UndergraduateStudent("Alice", 82.5),
new UndergraduateStudent("Bob", 67.0),
new GraduateStudent("Charlie", 88.0),
new GraduateStudent("Diana", 72.0)
};
foreach (Student student in students)
{
Console.WriteLine("Student: " + student.Name);
Console.WriteLine("Grade: " + student.CalculateGrade());
Console.WriteLine("-----------------------");
}
Console.ReadLine();
}
}
OUTPUT:
SWITCH STATEMENT DEMONSTRATION PROJECT
using System;
class Program
{
static void Main()
{
Console.Write("Enter a number (1 to 7): ");
int day = Convert.ToInt32(Console.ReadLine());
switch (day)
{
case 1:
Console.WriteLine("Sunday");
break;
case 2:
Console.WriteLine("Monday");
break;
case 3:
Console.WriteLine("Tuesday");
break;
case 4:
Console.WriteLine("Wednesday");
break;
case 5:
Console.WriteLine("Thursday");
break;
case 6:
Console.WriteLine("Friday");
break;
case 7:
Console.WriteLine("Saturday");
break;
default:
Console.WriteLine("Invalid input! Please enter a number between 1
and 7.");
break;
}
}
}
OUTPUT :
MAIN WINDOW FORM USING WINDOWS CLASS
using System;
using System.Windows.Forms;
namespace WindowsFormsApp
{
public class MainWindow : Form
{
private Button btnClickMe;
public MainWindow()
{
this.Text = "Main Window";
this.Size = new System.Drawing.Size(400, 300);
this.StartPosition = FormStartPosition.CenterScreen;
btnClickMe = new Button();
btnClickMe.Text = "Click Me";
btnClickMe.Size = new System.Drawing.Size(100, 40);
btnClickMe.Location = new System.Drawing.Point(150, 120);
btnClickMe.Click += BtnClickMe_Click;
this.Controls.Add(btnClickMe);
}
private void BtnClickMe_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello ,welcome to the main window!", "Message",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainWindow());
}
}
}
Output:
PURCHASING ORDER APPLICATION
using System;
using System.Collections.Generic;
namespace PurchaseOrderApp
{
class Program
{
static void Main(string[] args)
{
var orders = new List<string>();
bool exit = false;
while (!exit)
{
Console.WriteLine("\nSelect an option:");
Console.WriteLine("1. Create a new order");
Console.WriteLine("2. View all orders");
Console.WriteLine("3. Exit");
Console.Write("Your choice: ");
var choice = Console.ReadLine();
switch (choice)
{
case "1":
// Create a new order
Console.Write("Enter Product Name: ");
string productName = Console.ReadLine();
Console.Write("Enter Quantity: ");
int quantity = int.Parse(Console.ReadLine());
Console.Write("Enter Unit Price: ");
decimal unitPrice = decimal.Parse(Console.ReadLine());
decimal totalPrice = quantity * unitPrice;
// Corrected the string to use interpolation
string order = "Product: " + productName + ", Quantity: " +
quantity + ", Total Price: " + totalPrice;
orders.Add(order);
Console.WriteLine("Order added successfully!");
break;
case "2":
// View all orders
Console.WriteLine("\nAll Orders:");
if (orders.Count > 0)
{
foreach (var orderDetails in orders)
{
Console.WriteLine(orderDetails);
}
}
else
{
Console.WriteLine("No orders available.");
}
break;
case "3":
// Exit
exit = true;
break;
default:
Console.WriteLine("Invalid option, please try again.");
break;
}
}
}
}
}
OUTPUT:
LOGIN FORM APPLICATION
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("=== Simple Login ===");
Console.Write("Enter username: ");
string username = Console.ReadLine();
Console.Write("Enter password: ");
string password = Console.ReadLine();
if (username == "admin" && password == "1234")
{
Console.WriteLine("Login successful!");
}
else
{
Console.WriteLine("Invalid username or password.");
}
Console.ReadLine();
}
}
OUTPUT:
APPLICATION USING TREEVIEW CONTROL
using System;
using System.Collections.Generic;
namespace ConsoleTreeViewApp
{
class Program
{
// Define a simple TreeNode class for console
class TreeNode
{
public string Text { get; set; }
public List<TreeNode> Children { get; set; }
public TreeNode(string text)
{
Text = text;
Children = new List<TreeNode>();
}
}
static void Main(string[] args)
{
// Build tree structure
TreeNode root = new TreeNode("Animals");
TreeNode mammals = new TreeNode("Mammals");
mammals.Children.Add(new TreeNode("Dog"));
mammals.Children.Add(new TreeNode("Cat"));
mammals.Children.Add(new TreeNode("Elephant"));
TreeNode birds = new TreeNode("Birds");
birds.Children.Add(new TreeNode("Eagle"));
birds.Children.Add(new TreeNode("Parrot"));
birds.Children.Add(new TreeNode("Penguin"));
root.Children.Add(mammals);
root.Children.Add(birds);
// Print the tree to console
PrintTree(root, "");
Console.WriteLine("\nPress any key to exit...");
Console.ReadKey();
}
// Recursive method to print tree with indentation
static void PrintTree(TreeNode node, string indent)
{
Console.WriteLine(indent + node.Text);
foreach (var child in node.Children)
{
PrintTree(child, indent + " "); // Increase indent for children
}
}
}
}
OUTPUT:
APPLICATION USING FONT DIALOG CONTROL
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
lblSampleText.Font = fontDialog1.Font;
}
}
}
}
OUTPUT:
APPLICATION USING COLOR DIALOG CONTROL
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (ColorDialog colorDialog = new ColorDialog())
{
colorDialog.AllowFullOpen = true; // Allow picking any color
colorDialog.ShowHelp = true; // Show help button
colorDialog.Color = lblSample.BackColor; // Start with label’s
current color
if (colorDialog.ShowDialog() == DialogResult.OK)
{
lblSample.BackColor = colorDialog.Color; // Set label background
to chosen color
}
}
}
}
}
OUTPUT :
WEB BROWSER CONTROL TO DISPLAY SELECTED FILE
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "Select a file to display";
dlg.Filter = "All files (*.*)|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
string filePath = dlg.FileName;
// Display the file in the WebBrowser control
webBrowser.Navigate(filePath);
}
}
}
}
}
OUTPUT:
TREE VIEW STRUCTURE FOR DISPLAYING INPUTS
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
TextBox parentTextBox;
Button addParentButton;
TextBox childTextBox;
Button addChildButton;
TreeView treeView;
public Form1()
{
InitializeComponent();
this.Text = "Parent-Child TreeView";
this.ClientSize = new System.Drawing.Size(400, 400);
parentTextBox = new TextBox() { Left = 10, Top = 10, Width = 200 };
addParentButton = new Button() { Left = 220, Top = 8, Text = "Add
Parent" };
addParentButton.Click += AddParentButton_Click;
childTextBox = new TextBox() { Left = 10, Top = 40, Width = 200 };
addChildButton = new Button() { Left = 220, Top = 38, Text = "Add
Child" };
addChildButton.Click += AddChildButton_Click;
treeView = new TreeView() { Left = 10, Top = 70, Width = 350, Height
= 300 };
this.Controls.Add(parentTextBox);
this.Controls.Add(addParentButton);
this.Controls.Add(childTextBox);
this.Controls.Add(addChildButton);
this.Controls.Add(treeView);
this.Load += Form1_Load;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void AddParentButton_Click(object sender, EventArgs e)
{
string parentText = parentTextBox.Text.Trim();
if (!string.IsNullOrEmpty(parentText))
{
if (treeView.Nodes.ContainsKey(parentText))
{
MessageBox.Show("Parent node already exists.");
return;
}
TreeNode parentNode = new TreeNode(parentText) { Name =
parentText };
treeView.Nodes.Add(parentNode);
parentTextBox.Clear();
}
else
{
MessageBox.Show("Please enter a parent node name.");
}
}
private void AddChildButton_Click(object sender, EventArgs e)
{
string childText = childTextBox.Text.Trim();
if (treeView.SelectedNode == null)
{
MessageBox.Show("Please select a parent node to add a child.");
return;
}
if (string.IsNullOrEmpty(childText))
{
MessageBox.Show("Please enter a child node name.");
return;
}
TreeNode parentNode = treeView.SelectedNode;
foreach (TreeNode child in parentNode.Nodes)
{
if (child.Text == childText)
{
MessageBox.Show("Child node already exists under this parent.");
return;
}
}
parentNode.Nodes.Add(childText);
parentNode.Expand();
childTextBox.Clear();
}
}
}
OUTPUT:
EXCEPTION HANDLING IMPLEMENTATION IN C#
using System;
namespace ExceptionHandlingDemo
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Enter the numerator (integer):");
int numerator = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the denominator (integer):");
int denominator = Convert.ToInt32(Console.ReadLine());
int result = DivideNumbers(numerator, denominator);
Console.WriteLine("Result of " + numerator + " / " + denominator + "
= " + result);
}
catch (FormatException)
{
Console.WriteLine("Error: Please enter valid integers only.");
}
catch (DivideByZeroException)
{
Console.WriteLine("Error: Cannot divide by zero.");
}
finally
{
Console.WriteLine("Thank you for using the division app.");
}
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
static int DivideNumbers(int num1, int num2)
{
return num1 / num2;
}
}
}
OUTPUT:
COFFEE SHOP BILLING SYSTEM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string name;
int price;
int tot;
private void Form1_Load(object sender, EventArgs e)
{
private void groupBox1_Enter(object sender, EventArgs e)
{
dataGridView1.Rows.Clear();
if(checkBox1.Checked)
{
name="Coffee";
int qty=int.Parse(numericUpDown1.Value.ToString());
price=20;
tot=qty*price;
this.dataGridView1.Rows.Add(name,price,qty,tot);
}
if(checkBox2.Checked)
{
name="tea";
int qty=int.Parse(numericUpDown2.Value.ToString());
price=15;
tot=qty*price;
this.dataGridView1.Rows.Add(name,price,qty,tot);
}
dataGridView1.Refresh();
int sum = 0;
for (int row = 0; row < dataGridView1.Rows.Count; row++)
{
sum = sum +
Convert.ToInt32(dataGridView1.Rows[row].Cells[3].Value);
}
textBox1.Text = sum.ToString();
}
private void label2_Click(object sender, EventArgs e)
{
}
}
}
OUTPUT:
BULLETED LIST CONTROL APPLICATION
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<string> items = new List<string>();
while (true)
{
Console.Clear();
Console.WriteLine("Simple Bulleted List App");
Console.WriteLine("------------------------");
if (items.Count == 0)
Console.WriteLine("(No items yet)");
else
foreach (var item in items)
Console.WriteLine("• " + item);
Console.WriteLine("\nOptions:");
Console.WriteLine("1 - Add item");
Console.WriteLine("2 - Exit");
Console.Write("Choose an option: ");
string input = Console.ReadLine();
if (input == "1")
{
Console.Write("Enter item to add: ");
string newItem = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(newItem))
items.Add(newItem.Trim());
}
else if (input == "2")
{
break;
}
}
}
}
OUTPUT:
CALENDAR CONTROL FOR SINGLE DAY SELECTION
using System;
using System.Windows.Forms;
namespace singledayselectorapp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Limit calendar selection to a single day
monthCalendar1.MaxSelectionCount = 1;
// Set initial label text
label1.Text = "Select a date";
// Attach event handler for date changes
monthCalendar1.DateChanged += MonthCalendar1_DateChanged;
}
private void MonthCalendar1_DateChanged(object sender,
DateRangeEventArgs e)
{
// Update the label with the selected date
label1.Text = "Selected Date: " + e.Start.ToShortDateString();
}
private void Form1_Load(object sender, EventArgs e)
{
private void monthCalendar_Click(object sender, EventArgs e)
{
private void monthCalendar1_DateChanged_1(object sender,
DateRangeEventArgs e)
{
}
}
}
OUTPUT:
SCROLL BAR FEATURE WITH PANEL CONTROL
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
for (int i = 0; i < 10; i++)
{
Button btn = new Button()
{
Text = "Button " + (i + 1),
Location = new Point(10, i * 30),
Size = new Size(100, 25)
};
panel1.Controls.Add(btn);
}
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
}
}
OUTPUT: