Professional Documents
Culture Documents
Worksheet 2 - Attempt Review
Worksheet 2 - Attempt Review
2: Attempt review
My courses
https://lms.cuchd.in/mod/quiz/review.php?attempt=1110768&cmid=1039148 1
3/3/23, 4:35 PM Worksheet 1.2: Attempt review
Question 1
Complete
Marked out of
1. UID>0
2. Name is not null
3. Marks>50
using System;
class Student {
private int _uid;
set {
if (value > 0) {
_uid = value;
} else {
Console.WriteLine("UID must be greater than 0");
}
}
if (value != null) {
_name = value;
} else {
Console.WriteLine("Name cannot be null");
}
}
} else {
Console.WriteLine("Marks must be greater than 50");
https://lms.cuchd.in/mod/quiz/review.php?attempt=1110768&cmid=1039148 2
3/3/23, 4:35 PM Worksheet 1.2: Attempt review
}
class Program {
static void Main(string[] args) {
Console.ReadLine();
}
}
In this program, we define a Student class with properties for UID, Name, and Marks. Each property includes a
condition that checks whether the value being assigned meets the required criteria. For example, the UID property
checks that the value is greater than 0 before assigning it to the private _uid field.
In the Main method, we create two instances of the Student class. We intentionally set invalid values for the
properties of the first student to demonstrate the error messages generated by the conditions. Then we set valid
values for the properties of the second student and print out their details.
Student 2 Details:
UID: 20bca1146
Name: vishvadeep singh chauhan
Marks: 70
Explanation:
When we try to set UID to a negative value, it triggers the error message "UID must be greater than 0" since we
have defined a condition that UID should be greater than 0.
https://lms.cuchd.in/mod/quiz/review.php?attempt=1110768&cmid=1039148 3
3/3/23, 4:35 PM Worksheet 1.2: Attempt review
Similarly, when we try to set Name to null, it triggers the error message "Name cannot be null" since we have defined a condition that
When we try to set Marks to a value less than or equal to 50, it triggers the error message "Marks must be greater than 50" since we h
Finally, when we set the values for s2 object, it doesn't trigger any error message and the details are printed on the console.
Previous activity
Jump to...
Next activity
https://lms.cuchd.in/mod/quiz/review.php?attempt=1110768&cmid=1039148 4