You are on page 1of 10

LOVELY PROFESSIONAL UNIVERSITY HOME WORK(Section KE039) Code: CSE Course: Modern Programming Tools & Techniques ii 406T

Department: School: SCE CSE/IT Max. Marks: 30 DOA: 29/08/2012 DOS: 5/09/2012 Name: Nitin Handa Roll No: B54 Reg No: 4050070147
Q1: what is Default Constructor? How default constructor is invoked? What are the different types by which a constructor can be overloaded? Write a Program to justify your answer. [5 Marks] Ans: A default constructor is the one that takes no arguments. It is automatically invoked when an object is created without providing any initial values. In case, the programmer has not defined a default constructor, the compiler automatically generates it. Like method overloading constructor overloading can also be done. Constructor can be overloaded by changing signature. The signature defines the name and the set of parameters for the constructor. In order to use overloaded constructor, the signature must differ for each constructor declaration. This means we can do constructor overloading by: 1. Number of Parameters. 2. Type of Parameters. 3. Sequence of Parameters. Consider this example
class box { public int height,width; public box(int w, int h) { width = w; height = h; Console.WriteLine("Rectangle Constructor"); } public box(int s) {

Section: KE039

} public box(double s) { height = Convert.ToInt32(s); width = Convert.ToInt32(s); Console.WriteLine("Square Constructor with double parameter"); } public box(int w, double h) //changing sequence { height = Convert.ToInt32(h); width = w; Console.WriteLine("Rectangle Constructor with 1 int and 1 double parameter"); } public box(double w, int h) //changing sequence { height = h; width = Convert.ToInt32(w); Console.WriteLine("Rectangle Constructor with 1 double and 1 int parameter"); } } class BoxTest { static void Main(string[] args) { box b1= new box(2, 3); Console.WriteLine("Width: {0} Height: {1}", b1.width, b1.height); box b2 = new box(5); Console.WriteLine("Width: {0} Height: {1}", b2.width, b2.height); box b3 = new box(6.59); Console.WriteLine("Width: {0} Height: {1}", b3.width, b3.height); box b4 = new box(2, 5.5); Console.WriteLine("Width: {0} Height: {1}", b4.width, b4.height); box b5 = new box(4.5, 3); Console.WriteLine("Width: {0} Height: {1}", b5.width, b5.height); } } O/P: Rectangle Constructor Width: 2 Height: 3 Square Constructor Width: 5 Height: 5 Square Constructor with double parameter Width: 7 Height: 7 Rectangle Constructor with 1 int and 1 double parameter Width: 2 Height: 6 Rectangle Constructor with 1 double and 1 int parameter Width: 4 Height: 3

height = s; width = s; Console.WriteLine("Square Constructor");

Here in class box we have 5 constructors: 1st with 2 int arguments, 2nd with 1 int argument, 3rd with 1 double argument, 4th with 1 int and 1 double arguments and 5th with 1 int and 1 double argument. In Main() method of BoxTest class we initialize 3 box class object. First is with 2 arguments, so for this 1st constructor is called. For second object 2nd constructor is called

because we passed only 1 int argument. For the third object 3rd constructor is called because we passed 1 double argument. In the last 2 constructor we have changed the sequence arguments data type. So the constructor will be called accordingly. Q2: On the basis of memory allocation difference between values types and reference types. Give example to justify the need of both in a programming w.r.t. c#. [5 Marks] Ans: Difference between Value types and Reference types: Values type Reference type

A variable that is a value type, stores A variable of a reference type stores a the data reference to the data. The value of value types is stored on The value of a reference types is the managed stack, and can be used allocated on the runtime heap. directly. When a value type is assigned to When a reference type is assigned to another value type, it is copied. another reference type, a reference is assigned to the value.

The following example shows the difference between reference types and value types:
using System; namespace Demo { class abc { public int Value = 0; } class Program { static void Main(string[] args) { int a = 0; int b = a; b = 56; abc obj1 = new abc(); abc obj2 = obj1; obj2.Value = 34; Console.WriteLine("Values: {0},{1}", a, b); Console.WriteLine("Reference: {0},{1}", obj1.Value, obj2.Value); } } } O/P: Values: 0,56

Refernce: 34,34

When we assign value to local variable b, there is not change in local variable a. Because both local variables are of a value type and each local variable of a value type has its own storage. But assigning value 34 to obj2.Value affects the both obj1 and obj2 reference.

Q3: Write a c# program to find out the maximum of an array. Let a[] be an array of integers. If n= 1, a[0] is the only number in the array and so, maximum = a[0]. If n > 1, then do the following: find the maximum of n-1 entries of the array. Compare this maximum with the last entry a[n-1] and finalize. [5 Marks] Ans: The program is as Follows:
using System; class MaxProblem { static void Main(string[] args) { int n, i,maximum; Console.Write("Enter Size of array: "); n = int.Parse(Console.ReadLine()); int[] a=new int[n]; for (i = 0; i < n; i++) { Console.Write("Enter a[{0}]: ",i); a[i] = int.Parse(Console.ReadLine()); } maximum = a[0]; if(n>1) { for (i = 1; i < n; i++) { if (maximum < a[i]) maximum = a[i]; } } Console.WriteLine("Maximum number is {0}", maximum); Console.Read(); } } O/P: Enter Size of Array: 5 Enter a[0]: 23 Enter a[0]: 12 Enter a[0]: 56 Enter a[0]: 45 Enter a[0]: 34 Maximum number is 56

Explanation:

This program ask user to enter size of array. Then program

declare an array of user specified size and then ask to enter number in array. If size of the array is 1 then maximum number is a[0].Else compares maximum with each value of the array and find maximum number of the array and display that.

Q4: Develop a c# application that will determine whether any of several department-store customer has exceeded the credit limit on a charge account. For each customer, the following facts are available: a) b) c) d) e) account number balance at the beginning of the month total of all items charged by the customer this month total of all credits applied to the customers account this month allowed credit limit.

The program should input all these facts as integers, calculate the new balance (= beginning balance + charges credits), display the new balance and determine whether the new balance exceeds the customers credit limit. For those customers whose credit limit is exceeded, the program should display the message "Credit limit exceeded". [5 Marks] Ans: The program is as follows:
using System; namespace ConsoleApplication5 { class customer { private int accNum; private int balance; private int totItemsCharged; private int totCredits; private int CdLimit; private static int count = 1000; public customer(int b, int c) { accNum = count; balance = b; totItemsCharged = 0; totCredits = 0; CdLimit = c; count += 1; } public void NewBalance() { balance = balance + totItemsCharged - totCredits;

} public void setTotCd(int cr) { totCredits = cr; } public void setItemsCharged(int ic) { totItemsCharged = ic; } public int getBalance() { return balance; } public int getCdLimit() { return CdLimit; } public int getAccNum() { return accNum; } } class CreditLimit { static void Main(string[] args) { int b, c, cr, ic; Console.Write("Enter initial balance: "); b=int.Parse(Console.ReadLine()); Console.Write("Enter credit limit: "); c=int.Parse(Console.ReadLine()); customer ca=new customer(b,c); Console.Write("Enter total items charged: "); ic=int.Parse(Console.ReadLine()); Console.Write("Enter total credits used: "); cr=int.Parse(Console.ReadLine()); ca.setItemsCharged(ic); ca.setTotCd(cr); ca.NewBalance(); Console.WriteLine("The new balance is {0}", ca.getBalance()); if (ca.getBalance() > ca.getCdLimit()) Console.WriteLine("Credit limit exceeded for account number {0}", ca.getAccNum()); else { Console.WriteLine("Credit limit is not exceeded for account number {0}", ca.getAccNum()); } } } } O/P: Enter initial balance: 2000

Enter credit limit: 4000 Enter total items charged: 4000 Enter total credits used: 1000 The new balance is 5000 Credit limit exceeded for account number 1000

Explanation: this program declares a class customer which has required private data members and public methods to access them. In Main() method of CreditLimit class we declare an object of customer class and ask user to enter balance, credit limit, items charged, credit used. The program calculates new balance and checks whether new balance exceeded the credit limit. If so display message that credit limit exceeded.

Q5: Create a class Rectangle. The class has attribute Length and width, each of which defaults to 1.It has method that calculate the perimeter and the area of the rectangle. it has set and get methods for both length and width. The set method should verify that length and width are each floating-point numbers larger than 0.0 and less than 20.0. Write a program to test class Rectangle. [5 Marks] Ans: The program is as follows:
using System; namespace ConsoleApplication6 { class Rectangle { private double length; private double width; public Rectangle() { length = 1.0; width = 1.0; } public void setValues(double l, double w) { if ((l >= 0.0) && (w >= 0.0) && (l <= 20.0) && (w <= 20.0)) { length = l; width = w; } else { Console.WriteLine("Length and Width should be in the range 0.0 - 20.0"); } } public double getLength() { return length; } public double getWidth()

} class RecTest { static void Main(string[] args) { double l, w; Rectangle r = new Rectangle(); Console.Write("Enter Length: "); l = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter Width: "); w = Convert.ToDouble(Console.ReadLine()); r.setValues(l, w); Console.WriteLine("Length is {0} and Width is {1}.", r.getLength(), r.getWidth()); Console.WriteLine("Perimeter of the rectangle is {0} units.", r.Perimeter()); Console.WriteLine("Area of the rectangle is {0} sq. units.", r.Area()); } O/P: Enter Length: 5 Enter Width: 4 Length is 5 and Width is 4 Perimeter of the rectangle is 18 units. Area of the rectangle is 20 units.

return width; } public double Perimeter() { double p; p = 2 * (length + width); return p; } public double Area() { double a; a = length * width; return a; }

Explanation: In this program, there is class rectangle which has length and width data members. There are setValues(), getLength(), getWidth(), Perimeter() and Area() methods in the class. Before assigning values to data members the program checks whether the values are in range 0.0-20.0. The program prints the area and perimeter of the rectangle by calling methods. Q6: If a base class has a number of overloaded constructors and an inheriting class has a number of overloaded constructors; can you enforce a call from an inherited constructor to specific base constructor? If yes write a c# program to justify your answer. [5 Marks] Ans: If a base class has a number of overloaded constructors and an inheriting class has a number of overloaded constructors, then we can enforce

a call from an inherited constructor to specific base constructor through base() keyword. Consider following example:
class baseclass { public int a; public baseclass() { Console.WriteLine("Base class constructor"); a = 1; } public baseclass(int a) { Console.WriteLine("Base class constructor with parameter"); this.a = a; } } class deriveclass : baseclass { public deriveclass(): base(4) { Console.WriteLine("Derive class constructor"); } } public deriveclass(int a): base(a) { Console.WriteLine("Derive class constructor with parameter"); } } class ConsTest { static void Main(string[] args) { deriveclass obj1 = new deriveclass(); Console.WriteLine("value is {0}", obj1.a); deriveclass obj2 = new deriveclass(5); Console.WriteLine("value is {0}", obj2.a); } } O/P: Base class constructor with parameter Derive class constructor value is 4 Base class constructor with parameter Derive class constructor with parameter value is 5

Explanation: In this program there is base class baseclass which has 2 overloaded constructors; one with no arguments ans one with 1 argument. This class is inherited by a derive class deriveclass which also has 2 overloaded constructors. The constructor with no arguments enforse to class base class parameterized constructor through using :base() keyword and passes 4 to that. Also derive class parameterized constructor passes its argument to the base class constructor.

In the Main() method of ConsTest class we have declared two objects obj1 and obj2 of deriveclass type. So during initialization of obj1 derive class constructor class base class constructor which set value of a passed in derive class constructor, to 4. So first message is displayed of base class parameterized constructor then derive class default constructor. Similarly for obj2 we have passed value 5 during initialization. So first message is displayed of base class parameterized constructor then derive class parameterized constructor. We can enforce a call from an inherited constructor to specific base constructor through base() keyword.

You might also like