Answers to exercises in Object Oriented Programming with C# 4.0

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:

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.


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());
           
        }
    }
}

4.Answer the following.

  1. A static method can access instance variable? [True/False]

    False

  2. A partial method must be in a _______ class.

    Partial class

  3. You cannot overload two methods that return different return types when names and parameters are same. [True/False]

    True

  4. In order to make property read-only, we need to use read-only keyword.[True/False]

    False. We need to omit Set method.

  5. A class can have more than one indexer with the same type of parameter.[True/False]

    False

  6. A class marked as sealed can have an abstract method. [True/False]

    False

  7. When you create a method in the derived class with the same name as a method in base class, by default, it is said to [override/shadow] the method in the base class.

    Shadow

  8. If a function returns control from a try block for which we have finally block then finally block is not executed and control returns from the function to caller. [True/False]

    False. Finally block is executed then control returns to caller