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

C# Practice Exercise On OOP

The document discusses three object-oriented programming exercises in C#. The first involves creating a Student class to store details. The second involves creating Course classes to handle different course types. The third involves creating a Stack interface and class using exceptions.

Uploaded by

nani031nani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

C# Practice Exercise On OOP

The document discusses three object-oriented programming exercises in C#. The first involves creating a Student class to store details. The second involves creating Course classes to handle different course types. The third involves creating a Stack interface and class using exceptions.

Uploaded by

nani031nani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

C# Practice Exercise on OOP

1. Create a class to store details of student like rollno, name, course


joined and fee paid so far. Assume courses are C# and ASP.NET with
course fees being 2000 and 3000.
Provide the a constructor to take rollno, name and course.
Provide the following methods:
 Payment(amount)
 Print()
 DueAmount property
 TotalFee property

using System;

namespace st
{
class Student
{
private int rollno;
private string name;
private string course;
private int feepaid;

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


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

public void Payment(int amount)


{
feepaid += amount;
}

public void Print()


{
Console.WriteLine(rollno);
Console.WriteLine(name);
Console.WriteLine(course);
Console.WriteLine(feepaid);
}

public int DueAmount


{

get
{
return TotalFee - feepaid;
}
}

public int TotalFee


{
get
{
return course == "c#" ? 2000 : 3000;
}
}
}

class UseStudent
{

public static void Main()


{

Student s = new Student(1, "ABC", "c#");


s.Payment(1000);
s.Print();
Console.WriteLine(s.DueAmount);

}
}
}

Add a static member to store Service Tax, which is set to 12.3%. Also allow a property
through which we can set and get service tax.
Modify TotalFee and DueAmount properties to consider service tax.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace oop1
{
class Student2
{
private int rollno;
private string name;
private string course;
private int feepaid;

private static double servicetax = 12.3;

public Student2(int rollno, string name, string course)


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

public void Payment(int amount)


{
feepaid += amount;
}

public void Print()


{
Console.WriteLine(rollno);
Console.WriteLine(name);
Console.WriteLine(course);
Console.WriteLine(feepaid);
}

public int DueAmount


{

get
{
return TotalFee - feepaid;
}
}

public int TotalFee


{
get
{
double total = course == "c#" ? 2000 : 3000;
// service tax
total = total + total * servicetax / 100;
return (int) total;
}
}

public static double ServiceTax


{
get
{
return servicetax;
}
set
{
servicetax = value;
}
}
} // Student2

class UseStudent2
{

public static void Main()


{

Student2 s = new Student2(1, "ABC", "asp.net");


s.Payment(1000);
s.Print();
Console.WriteLine(s.DueAmount);

}
}
}

2.Create the classes required to store data regarding different types of


Courses. All courses have name, duration and course fee. Some courses are
part time where you have to store the timing for course. Some courses are
onsite where you have to store the company name and the no. of candidates
for the course. For onsite course we charge 10% more on the course fee. For
part-time course, we offer 10% discount.
Provide constructors and the following methods.
 Print()
 GetTotalFee()

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace st
{
abstract class Course
{
protected string name;
protected int duration;
protected int coursefee;

public Course(string name, int duration, int coursefee)


{
this.name = name;
this.duration = duration;
this.coursefee = coursefee;
}

public virtual void Print()


{
Console.WriteLine(name);
Console.WriteLine(duration);
Console.WriteLine(coursefee);
}

public abstract int GetTotalFee();


}

class ParttimeCourse : Course


{
private string timings;

public ParttimeCourse(string name, int duration, int


coursefee, string timings) : base(name,duration,coursefee)
{
this.timings = timings;
}

public override void Print()


{
base.Print();
Console.WriteLine(timings);
}

public override int GetTotalFee()


{
return (int) (coursefee * 0.90); // 10% discount
}

class OnsiteCourse : Course


{
private string company;
private int nostud;

public OnsiteCourse(string name, int duration, int


coursefee, string company, int nostud)
: base(name, duration, coursefee)
{
this.company = company;
this.nostud = nostud;
}

public override void Print()


{
base.Print();
Console.WriteLine(company);
Console.WriteLine(nostud);
}

public override int GetTotalFee()


{
return (int)(coursefee * 1.1); // 10% more
}

class TestCourse
{

public static void Main()


{
Course c = new OnsiteCourse("ASP.NET", 30, 5000, "ABC
Tech", 10);
c.Print();
Console.WriteLine(c.GetTotalFee());

c = new ParttimeCourse("C#", 30, 3000, "7-8pm");


c.Print();
Console.WriteLine(c.GetTotalFee());
}

}
}

3.) Create an interface called Stack with methods Push(), Pop() and property
Length. Create a class that implements this interface. Use an array to
implement stack data structure.
Create user-defined exceptions and ensure Push() and Pop() methods throw those
exceptions when required.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace oop1
{
interface IStack
{
void Push(int v);
int Pop();
int Length { get; }
}
class StackFullException : Exception
{
public StackFullException()
: base("Stack Full")
{
}
}

class StackEmptyException : Exception


{
public StackEmptyException()
: base("Stack Empty")
{
}
}

class Stack : IStack


{
private int[] a = new int[10];
private int top = 0;

public void Push(int v)


{

if (top == 10)
throw new StackFullException();

a[top] = v;
top++;
}

public int Pop()


{
if (top == 0)
throw new StackEmptyException();

top--;
return a[top];
}

public int Length


{
get
{
return top;
}
}
}
class UseStack
{
public static void Main()
{
Stack s = new Stack();
s.Push(20);

Console.WriteLine(s.Pop());
Console.WriteLine(s.Length);

Console.WriteLine(s.Pop());

}
}
}

You might also like