0% found this document useful (0 votes)
55 views17 pages

Tutorial 3

The document contains code for several C# classes and methods to demonstrate classes and objects: 1. A class called Q1 is defined with private data members and public methods to set and perform operations on those data members. 2. A Clock class is defined with private data members for hour, minute, second and methods to increment time, display time, and get individual time components. 3. A Student class is defined with private data members for name, age, course along with properties and methods to display student information. 4. The code is expanded to create objects for 5 students and display their information using the Student class. 5. A Product class is defined with data members and a constructor

Uploaded by

Abhay Shukla
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
55 views17 pages

Tutorial 3

The document contains code for several C# classes and methods to demonstrate classes and objects: 1. A class called Q1 is defined with private data members and public methods to set and perform operations on those data members. 2. A Clock class is defined with private data members for hour, minute, second and methods to increment time, display time, and get individual time components. 3. A Student class is defined with private data members for name, age, course along with properties and methods to display student information. 4. The code is expanded to create objects for 5 students and display their information using the Student class. 5. A Product class is defined with data members and a constructor

Uploaded by

Abhay Shukla
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 17

Tutorial 3

Name :Abhay Shukla


Enrollment no. :21SOEIT11028

1 : Draw a real picture for class and object. Differentiate class and object in terms of
diagram only.
Perform following tasks.
Task 1: Create a class
Task 2: Add few data members as private
Task 3: Add few methods as public to work on defined data members
Task 4: Create a Demo class with main method.
Task 5: Create at least two objects of a class defined in Task 1 into main method and call
all methods using that object.
Task 6: Write comment for each important portion of code like data members’
declaration, methods, some important logic etc.
Task 7: Summarize above solution in your own few words to visualize the solution to the
end user.

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace t3
{
internal class Q1
{
class p1
{
private int num1;
private int num2;

public void SetNumbers(int num1, int num2)


{
this.num1 = num1;
this.num2 = num2;
}

public int Add()


{
return num1 + num2;
}

public int Subtract()


{
return num1 - num2;
}
}
static void Main(string[] args)
{
Console.WriteLine("Mayank Desai - 21SOEIT11007");
p1 obj1 = new p1();
p1 obj2 = new p1();
obj1.SetNumbers(5, 15);
obj2.SetNumbers(29, 8);

Console.WriteLine("Sum of obj1 numbers: " + obj1.Add());


Console.WriteLine("Difference of obj1 numbers: " + obj1.Subtract());

Console.WriteLine("Sum of obj2 numbers: " + obj2.Add());


Console.WriteLine("Difference of obj2 numbers: " + obj2.Subtract());
Console.ReadLine();
}
}
}

Output:

2. Define a class Clock with three private integer data members hour,
min and sec. Define a no argument constructor to initialize time value to
12:00:00. Define a three argument constructor to initialize the time.
Define a methods to
1. Increment time to next second.
2. Display the time value.
3. Return the hour (int getHour())
4. Return the minute (int getMinute())
5. Return the seconds (int getSeconds())

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace t3
{
internal class p2
{
class Clock
{
private int hour;
private int min;
private int sec;

public Clock()
{
hour = 12;
min = 0;
sec = 0;
}

public Clock(int hour, int min, int sec)


{
this.hour = hour;
this.min = min;
this.sec = sec;
}

public void IncrementTime()


{
sec++;
if (sec >= 60)
{
sec = 0;
min++;
if (min >= 60)
{
min = 0;
hour++;
if (hour >= 24)
{
hour = 0;
}
}
}
}

public void DisplayTime()


{
Console.WriteLine($"{hour:D2}:{min:D2}:{sec:D2}");
}

public int GetHour()


{
return hour;
}

public int GetMinute()


{
return min;
}

public int GetSeconds()


{
return sec;
}
static void Main(string[] args)
{
Console.WriteLine("Abhay Shukla");
Clock clock1 = new Clock();

Clock clock2 = new Clock(6, 45, 30);

clock1.IncrementTime();
clock2.IncrementTime();

clock1.DisplayTime();
clock2.DisplayTime();

int hour = clock1.GetHour();


int min = clock1.GetMinute();
int sec = clock1.GetSeconds();

Console.WriteLine($"Hour: {hour}, Minute: {min}, Seconds: {sec}");


Console.ReadLine();
}
}
}
}

Output:

3. Define a Student class with appropriate data members, property,


constructors, methos etc. Define another class called TestStudent within the
same .cs file. Also create an object of student class and demonstrate the use
of student class.

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace t3
{
internal class p3
{
class Student
{
private string name;
private int age;
private string course;

public string Name


{
get { return name; }
set { name = value; }
}

public int Age


{
get { return age; }
set { age = value; }
}

public string Course


{
get { return course; }
set { course = value; }
}

public Student()
{

name = "Abhay Shukla";


age = 18;
course = "Information Technology";
}

public Student(string name, int age, string course)


{
this.name = name;
this.age = age;
this.course = course;
}

public void DisplayInfo()


{
Console.WriteLine($"Name: {name}, Age: {age}, Course: {course}");
}
}

static void Main(string[] args)


{
Console.WriteLine("Abhay Shukla");
Student student1 = new Student();

student1.Name = "Meet";
student1.Age = 20;
student1.Course = "Mathematics";

Console.WriteLine("Student 1 Information:");
student1.DisplayInfo();

Student student2 = new Student("Abhay", 20, "Physics");

Console.WriteLine("\nStudent 2 Information:");
student2.DisplayInfo();
Console.ReadLine();
}
}
}

Output:
4. Use above program classes and create objects for 5 students and
demonstrate the use student class.
Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace t3
{
internal class p4
{
class Student
{
private string name;
private int age;
private string course;

public string Name


{
get { return name; }
set { name = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}

public string Course


{
get { return course; }
set { course = value; }
}

public Student()
{

name = "Abhay Shukla";


age = 18;
course = "Information Technology";
}

public Student(string name, int age, string course)


{
this.name = name;
this.age = age;
this.course = course;
}

public void DisplayInfo()


{
Console.WriteLine($"Name: {name}, Age: {age}, Course: {course}");
}
}

static void Main(string[] args)


{
Console.WriteLine("Abhay");
Student student1 = new Student();

student1.Name = "raj";
student1.Age = 20;
student1.Course = "Mathematics";

Student student2 = new Student("Mayank", 20, "Physics");


Student student3 = new Student("Shubham", 22, "Physics");
Student student4 = new Student("Murgi", 19, "Biology");
Student student5 = new Student("Vikas", 20, "Mathes");
Console.WriteLine("Student 1 Information:");
student1.DisplayInfo();
Console.WriteLine("\nStudent 2 Information:");
student2.DisplayInfo();
Console.WriteLine("\nStudent 3 Information:");
student3.DisplayInfo();
Console.WriteLine("\nStudent 4 Information:");
student4.DisplayInfo();
Console.WriteLine("\nStudent 5 Information:");
student5.DisplayInfo();
Console.ReadLine();
}
}
}
5. Rearrange the given code to get the desired output.
Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace t3
{
internal class p5

{
class Product
{
int pcode;
String pname, mname;

public Product(int pcd, String pnm, String mnm)


{

pcode = pcd;
pname = pnm;
mname = mnm;
}

public void Display()


{

Console.WriteLine("\nProduct Code:= " + pcode);


Console.WriteLine("\nProduct Name:= " + pname);
Console.WriteLine("\nManufacturer Name:= " + mname);
}
}
static void Main(string[] args)
{
Console.WriteLine("Mayank Desai - 21SOEIT11007");
int n = args.Length;

if (n < 3)
{
Console.WriteLine("Syntax Error\n");
Console.WriteLine("Must Have THREE Arguments\n");
Console.WriteLine("Please, Write as [csc TestProduct ProductCode
ProductName Manufacturer] \n");
}
else
{
int pcd = Convert.ToInt32(args[0]);
String pnm = args[1];
String mnm = args[2];

Product p = new Product(pcd, pnm, mnm);

p.Display();
Console.ReadLine();
}
}
}
}

6. Complete the following code that will generate the given output:
Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace t3
{
internal class p6
{
class Line
{
private double length;
public Line()
{
length = 10;
Console.WriteLine("Object is being created, length = " + length);
}

public void setLength(double len)


{
length = len;
}

public double getLength()


{
return length;
}
}

static void Main(string[] args)


{
Console.WriteLine("Abhay shukla");
Line line = new Line();

Console.WriteLine("Length of line : {0}", line.getLength());

line.setLength(6);
Console.WriteLine("Length of line : {0}", line.getLength());
Console.ReadLine();
}
}
}

7. Define EnrolmentNo and Name properties for the Student class and
demonstrate use of these properties along with required data members,
methods and constructors.
Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace t3
{
internal class p7
{
class Student
{

private int enrolmentNo;


private string name;
private int age;
private string course;

public int EnrolmentNo


{
get { return enrolmentNo; }
set { enrolmentNo = value; }
}

public string Name


{
get { return name; }
set { name = value; }
}

public Student()
{
enrolmentNo = 34;
name = "Vadoliya Vivek";
age = 18;
course = "Informmation technology";
}

public Student(int enrolmentNo, string name, int age, string course)


{
this.enrolmentNo = enrolmentNo;
this.name = name;
this.age = age;
this.course = course;
}

public void DisplayInfo()


{
Console.WriteLine($"Enrolment No: {enrolmentNo}, Name: {name},
Age: {age}, Course: {course}");
}
}

static void Main(string[] args)


{
Console.WriteLine("Mayank Desai - 21SOEIT11007");
Student student1 = new Student();

student1.EnrolmentNo = 1001;
student1.Name = "Mayank";

Console.WriteLine("Student 1 Information:");
student1.DisplayInfo();

Student student2 = new Student(1002, "Raj", 20, "Physics");

Console.WriteLine("\nStudent 2 Information:");
student2.DisplayInfo();
Console.ReadLine();
}
}
}

You might also like