0% found this document useful (0 votes)
12 views20 pages

C# Visual Programming Exercises

The document contains a series of exercises demonstrating various programming concepts in C#, including arithmetic operations, class and object creation, single and multilevel inheritance, hybrid inheritance, multiple inheritance, and Windows Forms applications for student semester details and employee management systems. Each exercise includes code examples and explanations of the functionality implemented. The document serves as a practical guide for understanding object-oriented programming and database connectivity in C#.

Uploaded by

devtvpurpose
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)
12 views20 pages

C# Visual Programming Exercises

The document contains a series of exercises demonstrating various programming concepts in C#, including arithmetic operations, class and object creation, single and multilevel inheritance, hybrid inheritance, multiple inheritance, and Windows Forms applications for student semester details and employee management systems. Each exercise includes code examples and explanations of the functionality implemented. The document serves as a practical guide for understanding object-oriented programming and database connectivity in C#.

Uploaded by

devtvpurpose
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

Exercise 1: Program for Arithmetic Operations

Program :
using System;
using [Link];
using [Link];
using [Link];

namespace samplevpprogram
{
class Program
{
static void Main(string[] args)
{
[Link]("Enter a number1: ");
int num1 = Convert.ToInt32([Link]());
[Link]("Enter another number2: ");
int num2 = Convert.ToInt32([Link]());
[Link]("{0} + {1} = {2}", num1, num2, num1 + num2);
[Link]("{0} - {1} = {2}", num1, num2, num1 - num2);
[Link]("{0} x {1} = {2}", num1, num2, num1 * num2);
[Link]("{0} / {1} = {2}", num1, num2, num1 / num2);
[Link]("{0} mod {1} = {2}", num1, num2, num1 % num2);
[Link]();
}
}
}

Output:

[Visual Programming Lab] Page 1


Exercise 2 : C# Program for Class and Object Creation

Program:
using System;
using [Link];
using [Link];
using [Link];

namespace Classandobjectapplication
{
class Student
{
public string[] studrn = new string[5];
public int[] day = new int[5];
public int[] month = new int[5];
public int[] year = new int[5];
public string[] name = new string[5];
public string[] cname = new string[5];
public void details()
{
[Link]("Implementation of Classes and Objects");
[Link]("****************************************");
[Link]("Enter student details and you can view these details");
[Link]("****************************************");
}
}
class Mainmethod
{
static void Main(string[] args)
{
Student s = new Student();
[Link]();
int i;
for (i = 0; i < 2; i++)
{
[Link]("Enter student reg no:");
[Link][i] = [Link]();
[Link]("Enter student name:");
[Link][i] = [Link]();
[Link]("Enter course name:");
[Link][i] = [Link]();
[Link]("Enter date:");
[Link][i] = Convert.ToInt32([Link]());
[Link]("Enter month(1-12):");
[Link][i] = Convert.ToInt32([Link]());
[Link]("Enter year:");
[Link][i] = Convert.ToInt32([Link]());
}
[Link]("\n\nStudent's List\n");
for (i = 0; i < 2; i++)
{
[Link]("\nStudent ID : " + [Link][i]);
[Link]("\nStudent name : " + [Link][i]);
[Link]("\nCourse name : " + [Link][i]);
[Link]("\ndate of birth(dd-mm-yy) : " + [Link][i] + "-" + [Link][i] + "-" +

[Visual Programming Lab] Page 2


[Link][i]);
}
[Link]();

}
}
}

Output:

[Visual Programming Lab] Page 3


Exercise 3: Implementation of Single Inheritance for finding

Program:
using System;
using [Link];
using [Link];
using [Link];

namespace single_inheritance
{
class Rectangle
{
protected double length, width;
public void getdata()
{
[Link]("Enter the length and width of the rectangle");
length = [Link]([Link]());
width = [Link]([Link]());
}
public double GetArea()
{
return length * width;
}
public void Display()
{
[Link]("Length:{0}",length);
[Link]("Width:{0}",width);
[Link]("Area:{0}", GetArea());
}
}// end class Rectangle
class Childclass : Rectangle
{
private double cost;
public double GetCost()
{
double cost;
cost = GetArea()*70;
return cost;
}
public void Display()
{
[Link]();
[Link]("Cost:{0}",GetCost());
}
}
class ExecuteSingleInheritance
{
static void Main(string[] args)
{
Childclass t =new Childclass();
[Link]();
[Link]();
[Link]();
}
}

[Visual Programming Lab] Page 4


}

Output:

[Visual Programming Lab] Page 5


Exercise 4 : Program for implementation of Multilevel Inheritance

Program:
using System;
using [Link];
using [Link];
using [Link];

namespace multilevelinheritanace
{
public class Person
{
protected string regno, name;
public void get()
{
[Link]("Multilevel Inheritance!");
[Link]("Enter the register number and name of a student :-");
regno = [Link]();
name = [Link]();
}
public virtual void display()
{
[Link]("Student register number - {0} and name is {1}", regno, name);
[Link]();
}
}
class Student : Person
{
public string Classincharge = "PSV";
public override void display()
{
[Link]();
[Link]("Class incharge of the Student is: {0}", Classincharge);
}
}
class Details : Student
{
private string StudentAddress = "BCA, PUCC";
public void display()
{
[Link]("Student Address: {0}", StudentAddress);
}
}
class TestClass
{
public static void Main()
{
Student s = new Student();
[Link](); [Link]();
Details d = new Details();
[Link]();
[Link]();
}
}
}

[Visual Programming Lab] Page 6


Output :

[Visual Programming Lab] Page 7


Exercise 5: Program for implementing Hybrid Inheritance

Program :
using System;
using [Link];
using [Link];
using [Link];

namespace hybridinheritanace
{
class Father
{
public void home()
{
[Link]("Father's home");
}
public void Car()
{
[Link]("Father's Car");
}
}
class Son : Father
{
public void mobile()
{
[Link]("Son's mobile");
}
}

class Daughter : Father


{
public void purse()
{
[Link]("Daughter's purse");
}
}
public class TestHybridInheritance
{
public static void Main(String[] args)
{
Son s = new Son();
[Link]();
[Link]();
[Link]();
Daughter d = new Daughter();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
}

[Visual Programming Lab] Page 8


Output :

[Visual Programming Lab] Page 9


Exercise 6 : Program for Multiple Inheritance Implementation

Program:
using System;
using [Link];
using [Link];
using [Link];

namespace multipleinheritance
{
class Shape
{
protected int width,height;
public void setWidth(int w)
{
width = w;
}
public void setHeight(int h)
{
height = h;
}
}
public interface PaintCost //interface declarations
{
int getCost(int area);
}
class Rectangle : Shape, PaintCost
{
public int getArea()
{
return (width * height);
}
public int getCost(int area)
{
return area * 70;
}
}
class Test
{
static void Main(string[] args)
{
Rectangle Rect = new Rectangle();
int area;
[Link](5);
[Link](7);
area = [Link]();
[Link]("\t \t IMPLEMENTATION OF MULTIPLE INHERITANCE \n");
[Link]("Painting Details:- \n");
[Link]("Total area: {0}", [Link]());
[Link]("Total paint cost: ${0}", [Link](area));
[Link]();
}
}
}

[Visual Programming Lab] Page 10


Output:

[Visual Programming Lab] Page 11


WINDOWS FORM APPLICATIONS

Exercise 6: STUDENT SEMESTER DETAILS

INPUT DESGIN:

[Visual Programming Lab] Page 12


DATABASE CONNECTIVITY:

TEST CONNECTION:

[Visual Programming Lab] Page 13


PROGRAM:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace Student_Semester_Details
{
public partial class sem1 : Form
{
public sem1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
double tot, avg;
tot = [Link]([Link]) +
[Link]([Link]) + [Link]([Link]) +
[Link]([Link]);
[Link] = [Link](tot);
avg = tot / 5;
[Link] = [Link](avg);
if (avg >= 90)
[Link] = "O";
else if (avg < 90 && avg >= 80)
[Link] = "A";
else if (avg < 80 && avg >= 70)
[Link] = "B";
else if (avg < 70 && avg >= 60)
[Link] = "C";
else if (avg < 60 && avg >= 50)
[Link] = "D";
else
[Link] = "FAIL";
SqlConnection con1 = new SqlConnection(@"Data
Source=PC035;Initial Catalog=smarklist;User ID=sa;Password=admin@123");
[Link]();
SqlCommand cmd1 = new SqlCommand("insert into
atable(sname,regno,c#,sqm,ethicalhacking,vp,total,average,grade)values(@snam
e,@regno,@c#,@sqm,@ethicalhacking,@vp,@total,@average,@grade)", con1);
[Link]("@sname", [Link]);
[Link]("@regno", [Link]);
[Link]("c#", [Link]);
[Link]("@sqm", [Link]);
[Link]("@ethicalhacking", [Link]);
[Link]("@vp", [Link]);
[Link]("@total", [Link]);

[Visual Programming Lab] Page 14


[Link]("@average", [Link]);
[Link]("@grade", [Link]);
[Link]();
[Link]("Semester 1 Data Insert Successfully");
[Link]();
sem2 fm2 = new sem2();
[Link]();
[Link]();
}

private void textBox10_TextChanged(object sender, EventArgs e)


{

private void button2_Click(object sender, EventArgs e)


{
[Link] = "";
[Link] = "";
[Link] = "";
[Link] = "";
[Link] = "";
[Link] = "";
[Link] = "";
[Link] = "";
[Link] = "";

private void button5_Click(object sender, EventArgs e)

{
Close();
}

private void button3_Click(object sender, EventArgs e)


{

private void txttot1_TextChanged(object sender, EventArgs e)


{

private void button4_Click(object sender, EventArgs e)


{
}
}
}

[Visual Programming Lab] Page 15


OUTPUT:

[Visual Programming Lab] Page 16


Exercise 6 : EMPLOYEE MANAGEMENT SYSTEM

INPUT DESIGN:

[Visual Programming Lab] Page 17


DATABASE CONNECTIVITY:

TEST CONNECTION:

[Visual Programming Lab] Page 18


PROGRAM:
using System;
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];
using [Link];

namespace Employee_Salary_Management
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button2_Click(object sender, EventArgs e)


{
[Link]();

private void button1_Click(object sender, EventArgs e)


{
double bp, da, ta, gs, it = 0, nps, ns;
bp = [Link]([Link]);
da = bp * 42 / 100;
[Link] = [Link](da);
ta = 3600 + (3600 * 20) / 100;
[Link] = [Link](ta);
gs = bp + da + ta;
[Link] = [Link](gs);

if ((gs * 12) > 250000)


{
it = (((gs * 12) - 250000) * 10 / 100) / 12;
[Link] = [Link](it);
}
nps = gs * 10 / 100;
[Link] = [Link](nps);
ns = gs - it - nps;
[Link] = [Link](ns);

SqlConnection con1 = new SqlConnection(@"Data Source=PC035;Initial Catalog=Employee


Salary;User ID=sa;Password=admin@123");
[Link]();
SqlCommand cmd1 = new SqlCommand("insert into
employeesalary(bp,da,ta,gs,it,nps,ns)values(@bp,@da,@ta,@gs,@it,@nps,@ns)", con1);
[Link]("@bp", [Link]);
[Link]("@da", [Link]);
[Link]("@ta", [Link]);

[Visual Programming Lab] Page 19


[Link]("@gs", [Link]);
[Link]("@it", [Link]);
[Link]("@nps", [Link]);
[Link]("@ns", [Link]);
[Link]();
[Link]("Employee Salary Data Insert Successfully");
[Link]();
}

private void label2_Click(object sender, EventArgs e)


{

}
}
}

OUTPUT:

[Visual Programming Lab] Page 20

Common questions

Powered by AI

Console-based input methods, like those used in the provided C# programs for arithmetic operations and student details, promote simplicity and low resource usage, which is suitable for small applications or educational purposes . However, these methods offer limited interactivity and user-friendliness compared to GUIs, resulting in less intuitive user experience and more potential for input errors, as users must correctly input commands and data via text. GUIs, on the other hand, like the Student Semester Details input design, enhance user interaction with visual elements, making applications more approachable and reducing input errors through structured fields . GUIs can involve more complexity in programming and require more system resources but significantly broaden the user base by catering to less tech-savvy individuals.

Single inheritance in C# allows a class to inherit features from one base class, which simplifies the hierarchy and reduces complexity, allowing easy maintenance and understanding . This is demonstrated by the Rectangle class inheriting from a single class in the program for single inheritance . Multiple inheritance, on the other hand, allows a class to inherit from multiple base classes or interfaces, increasing flexibility but also adding complexity. This is shown in the program where the Rectangle class implements the PaintCost interface in addition to inheriting from the Shape class, thus demonstrating multiple inheritance . The implications are that while single inheritance is straightforward and avoids the diamond problem, multiple inheritance offers greater reuse at the cost of potential ambiguity and complexity in managing base class dependencies.

Direct database connectivity in the Employee Management System, as shown through the use of SqlConnection and SqlCommand, can lead to several challenges such as increased security risks due to hard-coded credentials and SQL injection vulnerabilities . Moreover, direct connectivity can result in tight coupling between application logic and database, which decreases maintainability and scalability. To mitigate these issues, it is advisable to use environment variables or configuration files to store sensitive database connection information securely. Additionally, applying input validation and parameterized queries can prevent SQL injection. Implementing a repository or data access layer can help in decoupling the database logic from application logic, enhancing maintainability and allowing for easier transitions to different database systems.

Hybrid inheritance achieves reusability by combining multiple inheritance forms, allowing classes to inherit attributes and methods from more than one parent class, as demonstrated by the Son and Daughter classes inheriting from the Father class . This approach maximizes code reuse by enabling different derived classes to share base class implementations, thus reducing redundancy. However, the trade-offs include increased complexity in the inheritance hierarchy, leading to potential difficulties in understanding inheritance paths and managing dependencies. These complexities can be partially alleviated using interfaces or abstract classes to guide implementation while avoiding the diamond problem traditionally associated with multiple and hybrid inheritance in languages that do not natively support it.

The cost calculation in the single inheritance program is based on multiplying the area of a rectangle by a fixed cost rate of 70 . While this straightforward approach works for constant rate scenarios, it lacks accuracy in variable cost scenarios influenced by factors such as material differences or location-based pricing adjustments. A potential improvement would involve introducing dynamic pricing based on configurable rates stored in external files or databases, allowing for real-time updates without modifying the program's source code. This would increase accuracy by adapting to varying cost parameters and enhance scalability by accommodating additional factors that influence pricing, thus making the solution more robust for broader application contexts.

In the document's programs, object-oriented programming principles like inheritance and encapsulation significantly enhance code modularity and maintainability. Inheritance allows for code reuse by enabling classes to inherit properties and methods from a base class, seen in programs such as the Rectangle and Childclass inheritance hierarchy, which promote easier updates and extensions across related classes . Encapsulation, demonstrated through the use of private variables and public methods, protects the integrity of data and separates internal states from external control, as shown in the Student class managing student data . Together, these principles contribute to modular organization, enabling adaptation and maintenance of individual modules without ripple effects throughout the entire codebase, thereby enhancing overall maintainability.

The implementation of multilevel inheritance in the document improves program structure by creating a clear hierarchy between classes, where derived classes inherit features and behaviors from their base classes and their ancestors, exemplified by the Details inheriting from Student, which in turn inherits from Person . This structure allows common features and behaviors to be centralized in base classes, reducing redundancy and improving code clarity. However, potential drawbacks include increased complexity in the class hierarchy, which may lead to difficulties in understanding inheritance paths and resolving method calls in deeply nested structures. Additionally, multilevel inheritance can cause rigidity, where changes in base classes might inadvertently affect all descendant classes, thereby necessitating careful design considerations for effective implementation.

Utilizing a graphical UI in the Student Semester Details application improves user input validation by providing structured fields and interactive elements like buttons for task execution, enhancing user interaction and minimizing input errors . This user interface enhancement simplifies real-time validation by employing event-driven programming to check inputs instantly and provide immediate feedback to users, which is more challenging in text-based interfaces. Moreover, a GUI can more effectively manage database functionality by integrating features like dropdowns and date pickers to ensure data consistency before storage. However, transitioning to a GUI can require additional resources and more complex programming compared to simple console applications.

Encapsulation in the provided programs is used to protect the data by keeping it private and exposing only necessary parts through public methods. For instance, the Student class in the Class and Object Creation program encapsulates the student details and provides methods to input and display them . This strategy secures the program by limiting access to internal data, reducing unintended interference and errors. Moreover, it enhances modifiability by allowing internal changes without affecting external code interactions, as developers can alter the private data processing inside the class without impacting the external methods that access these through public methods.

The use of classes and objects in the student management system facilitates efficient data management by organizing data into manageable structures. In the program, the Student class contains arrays to hold multiple student details such as registration numbers, names, and birthdates, encapsulating these details for easy manipulation . Such organization allows for scalability, making it straightforward to manage increased data volume by simply extending arrays or using more advanced data structures. Objects instantiated from the Student class allow for reusability across multiple parts of the program, ensuring consistent data handling and reducing redundancy. This object-oriented approach supports modular programming, enabling components to interact through defined interfaces, which enhances maintainability and reduces complexity in data management.

You might also like