Implementing StudentInfo Class in C# for Grade Calculation Tutorial

Assignment Question

Use C# programming language. Create a class StudentInfo and include students first name, last name, and three exams grades. It should also include the following functions: constructors, properties, and two functions: calculateAve() which calculate the average of three exams, and getLetterGrade() which determine whether an A, B, C, D, or F should be returned. Create a testing program StudentInfoTest. In the main function, the program prompts the user to enter name and three grades between 0 and 100 (include input validation). The program creates two objects based on the StudentInfo class, one object will pass user information via constructor, and another object will be initialized using default constructor and pass user information via properties. The program calculates the average, determine the letter grade, and displays two students information on the screen, name, grades, average, and the letter grade. Example: Student Name: Bob Ho Exam1: 95 Exam2: 80 Exam3: 82 Average: 85.67 Letter grade: B A: 100-90; B: 89.99-80; C: 79.99-70; D: 69.99-60; F

Assignment Answer

Introduction

In this C# programming tutorial, we will create a class called StudentInfo to manage student information, including their first name, last name, and three exam grades. The class will have constructors, properties, and two functions: calculateAve() to calculate the average of the three exams and getLetterGrade() to determine the letter grade based on the average. We will also create a testing program, StudentInfoTest, to demonstrate the usage of the StudentInfo class [1].

StudentInfo Class

Let’s start by defining the StudentInfo class. It will have private fields for first name, last name, and exam grades. We will also include constructors and properties to set and retrieve this information.

class StudentInfo
{
private string firstName;
private string lastName;
private int[] examGrades = new int[3];

public StudentInfo(string firstName, string lastName, int[] grades)
{
this.firstName = firstName;
this.lastName = lastName;

if (grades.Length == 3)
{
for (int i = 0; i < 3; i++)
{
if (grades[i] >= 0 && grades[i] <= 100)
{
examGrades[i] = grades[i];
}
else
{
// Handle input validation here if needed
}
}
}
}

// Properties to get first name, last name, and exam grades
public string FirstName
{
get { return firstName; }
}

public string LastName
{
get { return lastName; }
}

public int[] ExamGrades
{
get { return examGrades; }
}

// Function to calculate the average of exam grades
public double CalculateAve()
{
return examGrades.Average();
}

// Function to determine the letter grade
public string GetLetterGrade()
{
double average = CalculateAve();
if (average >= 90)
{
return "A";
}
else if (average >= 80)
{
return "B";
}
else if (average >= 70)
{
return "C";
}
else if (average >= 60)
{
return "D";
}
else
{
return "F";
}
}
}

Now we have a StudentInfo class that can store student information and perform grade-related calculations. Next, let’s create the testing program, StudentInfoTest.

StudentInfoTest Program

In this program, we will prompt the user to enter the student’s name and three exam grades, ensuring input validation for the grade values. We will then create two StudentInfo objects, one using a constructor and the other initializing via properties. Finally, we will calculate the average and determine the letter grade for each student and display their information [1].

using System;

class StudentInfoTest
{
static void Main(string[] args)
{
// Prompt the user to enter student information
Console.Write("Enter the student's first name: ");
string firstName = Console.ReadLine();

Console.Write("Enter the student's last name: ");
string lastName = Console.ReadLine();

int[] grades = new int[3];
for (int i = 0; i < 3; i++)
{
Console.Write($"Enter grade for Exam {i + 1} (0-100): ");
if (int.TryParse(Console.ReadLine(), out int grade) && grade >= 0 && grade <= 100)
{
grades[i] = grade;
}
else
{
Console.WriteLine("Invalid input. Please enter a valid grade.");
i--;
}
}

// Create StudentInfo objects
StudentInfo student1 = new StudentInfo(firstName, lastName, grades);
StudentInfo student2 = new StudentInfo("", ""); // Using default constructor
student2.FirstName = firstName;
student2.LastName = lastName;
student2.ExamGrades = grades;

// Calculate average and determine letter grade
double average1 = student1.CalculateAve();
string letterGrade1 = student1.GetLetterGrade();

double average2 = student2.CalculateAve();
string letterGrade2 = student2.GetLetterGrade();

// Display student information
Console.WriteLine("Student Information:");
Console.WriteLine($"Student Name: {student1.FirstName} {student1.LastName}");
Console.WriteLine($"Exam1: {student1.ExamGrades[0]} Exam2: {student1.ExamGrades[1]} Exam3: {student1.ExamGrades[2]}");
Console.WriteLine($"Average: {average1:F2} Letter grade: {letterGrade1}");
Console.WriteLine($"A: 100-90; B: 89.99-80; C: 79.99-70; D: 69.99-60; F: <60");

Console.WriteLine("\nStudent Information (Using Properties):");
Console.WriteLine($"Student Name: {student2.FirstName} {student2.LastName}");
Console.WriteLine($"Exam1: {student2.ExamGrades[0]} Exam2: {student2.ExamGrades[1]} Exam3: {student2.ExamGrades[2]}");
Console.WriteLine($"Average: {average2:F2} Letter grade: {letterGrade2}");
}
}

In this testing program, we first collect the user’s input for the student’s information and grades. Then, we create two StudentInfo objects, one using the constructor and the other initializing properties with the collected information. After that, we calculate the average and determine the letter grade for both students and display their information.

Conclusion

In this tutorial, we’ve created a C# class called StudentInfo to manage student information and perform grade-related calculations. We’ve also developed a testing program, StudentInfoTest, to demonstrate how to use the StudentInfo class to store and process student data. This program includes input validation to ensure that the grades fall within the valid range. Students’ averages and letter grades are calculated and displayed, providing a complete solution for managing student information and grades in C# [1].

References

Microsoft Docs – C# Programming Guide

C# Yellow Book by Rob Miles – Chapter 4

C# Programming for the Absolute Beginner by Andy Harris – Chapter 5

Frequently Asked Questions (FAQs)

What is the purpose of creating a StudentInfo class in C# for grade calculation?

The StudentInfo class in C# serves the purpose of managing and storing student information, including their first name, last name, and exam grades. It also calculates the average of these grades and determines the letter grade. This is useful for educational applications and systems where student data needs to be processed and analyzed.

Why is input validation important in the StudentInfoTest program?

Input validation is essential to ensure that the grades entered by the user are within the valid range (0-100). Without validation, the program could accept incorrect or out-of-range values, leading to inaccurate grade calculations. Validating input helps maintain data integrity and reliability.

How is the letter grade determined in the StudentInfo class?

The letter grade is determined based on the average of the three exam grades. The class uses a series of conditional statements to check the average and assigns a letter grade accordingly. For example, an average score of 90 or higher results in an “A” grade, while an average score below 60 results in an “F” grade.

Can the StudentInfo class be used in other C# applications?

Yes, the StudentInfo class is designed to be a reusable component that can be incorporated into various C# applications that require student data management and grade calculations. It provides a convenient way to work with student information and simplify grade-related tasks.

What are the benefits of using properties in the StudentInfo class for accessing data?

Properties in the StudentInfo class provide controlled access to the private fields, such as first name, last name, and exam grades. This encapsulation allows for better data protection and ensures that data is accessed and modified according to specified rules. It also enhances code maintainability and readability.






Discount Button



Get 15% off discount on your first order. Order now!


Last Completed Projects

topic title academic level Writer delivered

2024 Copyright ©, TopClassEssay ® All rights reserved