You are on page 1of 4

3/3/23, 4:35 PM Worksheet 1.

2: Attempt review

My courses

Started on Friday, 3 March 2023, 4:26 PM


State Finished
Completed on Friday, 3 March 2023, 4:35 PM
Time taken 9 mins 19 secs

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

WAP to demonstrate Properties and Conditions are:-

1. UID>0
2. Name is not null

3. Marks>50

using System;

class Student {
private int _uid;

private string _name;

private int _marks;

public int UID {


get { return _uid; }

set {

if (value > 0) {
_uid = value;

} else {
Console.WriteLine("UID must be greater than 0");

}
}

public string Name {


get { return _name; }
set {

if (value != null) {
_name = value;

} else {
Console.WriteLine("Name cannot be null");

}
}

public int Marks {

get { return _marks; }


set {

if (value > 50) {


_marks = value;

} 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) {

Student s1 = new Student();

s1.UID = -1; // This will generate an error message

s1.Name = null; // This will generate an error message

s1.Marks = 45; // This will generate an error message

Student s2 = new Student();


s2.UID = 20bca1146;

s2.Name = "vishvadeep singh chauhan";


s2.Marks = 70;

Console.WriteLine("Student 2 Details: ");


Console.WriteLine("UID: " + s2.UID);

Console.WriteLine("Name: " + s2.Name);


Console.WriteLine("Marks: " + s2.Marks);

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.

The output of the program will be:

UID must be greater than 0

Name cannot be null

Marks must be greater than 50

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

You might also like