Beruflich Dokumente
Kultur Dokumente
1/9
Exercise 2
In struct type Time, declare the following conversions: - an implicit conversion from int (minutes since midnight) to Time - an explicit conversion from Time to int (minutes since midnight) Use these facilities in your Main method. For instance, you should be able to do this: Time t1 = new Time(9,30); Time t2 = 120; // Two hours int m1 = (int)t1; Console.WriteLine("t1={0} and t2={1} and m1={2}", t1, t2, m1); Time t3 = t1 + 45; The next few exercises use this typeWhy is the addition in the initialization of t3 legal? What is the value of t3?
2/9
Exercise 3
Make the minutes field of struct type Time public (and not readonly) instead of private readonly. Then execute this code: Time t1 = new Time(9,30); Time t2 = t1; t1.minutes = 100; Console.WriteLine("t1={0} and t2={1}", t1, t2); What result do you get? Why? What result do you get if you change Time to be a class instead of a struct type?Why?
Exercise 4
Create a class, StrEncrypt with the following member functions: String EncryptForward (String input) - which will shift the input string by two characters forward - e.g. a becomes c - d becomes f - z becomes b String EncryptBackword(String input) - which will shift the input string by two characters backward - e.g c becomes a - d becomes b - b becomes z Create a windows form to test your class.
3/9
Exercise 6
Add properties: read-only properties, write-only properties, and read/write properties Change Class B to add read-only property (X), write-only property (Y), and read/write property (Z). public int X { get { return m_x; } } public int Y { set { m_y = value; } } public int Z { get { return m_z; } set { m_z = value; } }
4/9
Exercise 7
Write a program to display the name and age of a person. Use a default constructor to assign values to the name and age variables. Use a parameterized constructor to pass the values of name and age. Use a single method to display the values from both the constructors. Output of program:
5/9
Exercise 8
Write a program that calculates the square of an integer, say 3 and a double, say 4.2. Use method overloading to calculate the square of the integer and double values. Output of program:
Exercise 9
Write a program that displays the values of two integers; intX and intY. Assign the values 10 and 20 to intX and intY respectively using a parameterized constructor. Include a default constructor that does nothing. Instantiate an object of this constructor and print the values of the variables intX and intY. Overload the operator to change the sign of the values (i.e. negative values). Display the values of intX and intY. The output of the program:
6/9
7/9
8/9
Home work
Exercise 1
Write a program having a class Student. Create a field stud_no and property stud_name. Assign and display values for the property and field. Output example:
Exercise 2
Modify the class Student in exercise 1, to make stud_name as a readonly property Output example:
Exercise 3
Create an indexer to store the names of students and overload the same to store marks of three subjects of the students Output example:
9/9