5.7 Student Class: Nested ifelse Statements

The example of Figs. 5.55.6 demonstrates a nested ifelse statement that determines a student’s letter grade based on the student’s average in a course.

Class Student

Class Student (Fig. 5.5) stores a student’s name and average and provides properties for manipulating these values. The class contains:

  • Auto-implemented string property Name (line 7) to store a Student’s name.

  • Instance variable average of type int (line 8) to store a Student’s average in a course and a corresponding Average property (lines 18–36) to get and set the Student’s average. Average’s set accessor uses nested if statements (lines 28–34) to validate the value that’s assigned to the Average property. These statements ensure that the value is greater than 0 and less than or equal to 100; otherwise, instance variable average’s value is left unchanged. Each if statement contains a simple condition—i.e., one that makes only a single test. In Section 6.11, you’ll see how to use logical operators to write compound conditions that conveniently combine several simple conditions. If the condition in line 28 is true, only then will the condition in line 30 be tested, and only if the conditions in both lines 28 and 30 are true will the statement in line 32 execute.

  • A constructor (lines 11–15) that sets the Name and Average properties.

  • Read-only property LetterGrade (lines 39–68), which uses nested ifelse statements to determine the Student’s letter grade based on the Student’s average. A read-only property provides only a get accessor. Note that the local variable letterGrade is initialized to string.Empty (line 43), which represents the empty string (that is, a string containing no characters).

Fig. 5.5 Student class that stores a student name and average.

Alternate View

  1    // Fig. 5.5: Student.cs
  2    // Student class that stores a student name and average.
  3    using System;
  4
  5    class Student
  6    {
  7       public string Name { get; set; } // property
  8       private int average; // instance variable
  9
 10       // constructor initializes Name and Average properties
 11       public Student(string studentName, int studentAverage)
 12       {
 13          Name = studentName;
 14          Average = studentAverage; // sets average instance variable
 15       }
 16
 17       // property to get and set instance variable average
 18       public int Average
 19       {
 20          get // returns the Student's average
 21          {
 22             return average;
 23          }
 24          set // sets the Student's average
 25          {
 26             // validate that value is > 0 and <= 100; otherwise,
 27             // keep instance variable average's current value
 28             if (value > 0)                                       
 29             {                                                    
 30                if (value <= 100)
 31                {                                                 
 32                   average = value; // assign to instance variable
 33                }                                                 
 34             }                                                    
 35          }
 36       }
 37
 38       // returns the Student's letter grade, based on the average
 39       string LetterGrade
 40       {
 41          get
 42          {
 43              string letterGrade = string.Empty; // string.Empty is ""
 44
 45              if (average >= 90)     
 46              {                      
 47                 letterGrade = "A";  
 48              }                      
 49              else if (average >= 80)
 50              {                      
 51                 letterGrade = "B";  
 52              }                      
 53              else if (average >= 70)
 54              {                      
 55                 letterGrade = "C";  
 56              }                      
 57              else if (average >= 60)
 58              {                      
 59                 letterGrade = "D";  
 60              }                      
 61              else                   
 62              {                      
 63                 letterGrade = "F";  
 64              }                      
 65
 66              return letterGrade;
 67          }
 68       }
 69    }

Class StudentTest

To demonstrate the nested if statements and nested ifelse statements in class Student’s Average and LetterGrade properties, respectively, method Main (Fig. 5.6) creates two Student objects (lines 9–10). Next, lines 12–15 display each Student’s name, average and letter grade by accessing the objects’ Name, Average and LetterGrade properties, respectively.

Fig. 5.6 Create and test Student objects.

Alternate View

  1   // Fig. 5.6: StudentTest.cs
  2   // Create and test Student objects.
  3   using System;
  4
  5   class StudentTest
  6   {
  7      static void Main()
  8      {
  9         Student student1 = new Student("Jane Green", 93);
 10         Student student2 = new Student("John Blue", 72);
 11
 12         Console.Write($"{student1.Name}'s letter grade equivalent of ");
 13         Console.WriteLine($"{student1.Average} is {student1.LetterGrade}");
 14         Console.Write($"{student2.Name}'s letter grade equivalent of ");
 15         Console.WriteLine($"{student2.Average} is {student2.LetterGrade}");
 16     }
 17   }

Jane Green's letter grade equivalent of 93 is A
John Blue's letter grade equivalent of 72 is C
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset