CODE
CODE
namespace ClassLibrary1
{
public class Student
{
public int StudentID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<int> Scores { get; set; }
}
CLASS LIBRARY
namespace ClassLibrary1
{
// Define a class that represents a student with ID, FirstName,
LastName, and scores
public class Student
{
public int StudentID { get; set; } // Property to store
unique identifier for each student
public string FirstName { get; set; } // Property to store
student’s first name
public string LastName { get; set; } // Property to store
student’s last name
public List<int> Scores { get; set; } // Property to store
a list of student’s subject score
}
// Defines a class that represent students with their full name
and average score
public class StudentWithAverage
{
public string FullName { get; set; } //Concatenation of
the student’s first and last name
public double AverageScore { get; set; } //Calculated average
score
}
// Defines a class to group the students who share the same first
name
public class FirstNameGroup
{
public string FirstName { get; set; } //Property to store
the same first name
public List<string> Students { get; set; } // Property to
store the list of student’s full name
}
//Defines a class to group student who have the same grade in a
particular subject
public class GradeGroup
{
public int SubjectIndex { get; set; } // Property to store
the index of the subject
public int Grade { get; set; } // Property to store
the grade of the subject
public List<string> FullNames { get; set; } // List of
student’s full name with the grade on the subject
}
// A static class that contains an analysis method for students
public static class StudentAnalyzer
{
// Finds students with an average score below 75
public static List<StudentWithAverage>
GetStudentsWithAverageBelow75(List<Student> students)
{
return students
.Where(s => s.Scores.Average() < 75) // Filter
students with an average score < 75
.Select(s => new StudentWithAverage // Project
each student into StudentWithAverage object
{
FullName = $"{s.FirstName} {s.LastName}",
// Concatenate first and last name for full name
AverageScore = s.Scores.Average()
// Calculate the student’s average score
})
.ToList(); // Convert the result to a list
}
1. `GetStudentsWithAverageBelow75`
Purpose:
This function identifies students whose average scores across all subjects are less than 75. It returns a list
of `StudentWithAverage` objects, each containing the student's full name and their average score.
How it works:
• It takes a list of `Student` objects as input.
• Uses LINQ `Where` to filter students where the average of their scores is less than 75.
• Projects each filtered student into a new `StudentWithAverage` object:
• `FullName`: Concatenates first and last names.
• `AverageScore`: Calculates the average of the scores.
• Converts the resulting sequence into a list.
2. `GetStudentsWithSameFirstName`
Purpose:
This function finds all students who share the same first name with at least one other student. It groups
such students by their first name into `FirstNameGroup` objects.
How it works:
• Groups the list of students by their `FirstName` using `GroupBy`.
• Filters groups where the count of students is greater than 1 (meaning multiple students share
that first name).
• Projects each group into a `FirstNameGroup`:
• `FirstName`: The shared first name.
• `Students`: A list of full names (`FirstName LastName`) of all students in that group.
• Returns a list of these groups.
3. `GetSameGradeGroups`
Purpose:
This function finds groups of students who share the same grade (score) in each subject. It examines
each subject separately and groups students who have the same score in that subject.
How it works:
• Assumes each student has scores for 4 subjects (indices 0 to 3).
• Creates a sequence of subject indices (0, 1, 2, 3) using `Enumerable.Range`.
• For each subject index:
• Groups students by their score in that subject.
• Filters groups with more than one student (i.e., multiple students with the same grade).
• Projects each group into a `GradeGroup`:
• `SubjectIndex`: The current subject.
• `Grade`: The shared score.
• `FullNames`: List of students' full names with that score.
• Uses `SelectMany` to flatten all these groups across all subjects into a single list.
SUMMARY
The provided code defines a class library that models student data and provides functionalities to
analyze this data. It consists of several classes and a static class with methods for data analysis.
1. Namespace Declaration
namespace ClassLibrary1
• This line declares a namespace called `ClassLibrary1`. Namespaces are used to organize code and
prevent naming conflicts. All classes and methods defined within this block belong to this
namespace.
2. Data Model Classes
The code defines four classes that represent different aspects of student data:
Student Class
public class Student
{
public int StudentID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<int> Scores { get; set; }
}
Purpose: Represents a student with properties for their ID, first name, last name, and a list of scores.
Properties:
• `StudentID`: An integer representing the unique identifier for the student.
• `FirstName`: A string representing the student's first name.
• `LastName`: A string representing the student's last name.
• `Scores`: A list of integers representing the scores the student has received in various subjects.
StudentWithAverage Class
public class StudentWithAverage
{
public string FullName { get; set; }
public double AverageScore { get; set; }
}
Purpose: Represents a student along with their average score.
Properties:
• `FullName`: A string that combines the student's first and last names.
• `AverageScore`: A double representing the average score of the student.
FirstNameGroup Class
public class FirstNameGroup
{
public string FirstName { get; set; }
public List<string> Students { get; set; }
}
Purpose: Represents a group of students who share the same first name.
Properties:
• `FirstName`: A string representing the common first name.
• `Students`: A list of strings containing the full names of students with that first name.
GradeGroup Class
public class GradeGroup
{
public int SubjectIndex { get; set; }
public int Grade { get; set; }
public List<string> FullNames { get; set; }
}
Purpose: Represents a group of students who received the same grade in a specific subject.
Properties:
• `SubjectIndex`: An integer representing the index of the subject.
• `Grade`: An integer representing the grade received by the students in that subject.
• `FullNames`: A list of strings containing the full names of students who received that grade.
3. StudentAnalyzer Static Class
Analysis Methods
GetStudentsWithAverageBelow75
• Functionality: Identifies students with an average score below 75 and returns their full names
and average scores.
GetStudentsWithSameFirstName
• Functionality: Groups students by their first names and returns groups that contain more than
one student with the same first name.
GetSameGradeGroups
• Functionality: Groups students by their grades in different subjects and returns groups that
contain more than one student with the same grade.
SUMMARY OF CODE
The overall purpose of this code is to provide a structured way to represent student data and perform
analyses on that data. The classes serve as data models, while the methods in the `StudentAnalyzer`
class allow for various analyses, such as:
• Finding students who are struggling academically (average score below 75).
• Identifying students with the same first name, which can be useful for social or administrative
purposes.
• Grouping students by their performance in specific subjects, which can help educators
understand trends in grades.
PROGRAM.cs CODE
using System;
using System.Collections.Generic;
using System.Linq;
using ClassLibrary1;
using static ClassLibrary1.GradeGroup;
students.AddRange(newstudents);
var avgBelow75 =
StudentAnalyzer.GetStudentsWithAverageBelow75(students);
var firstNameGroups =
StudentAnalyzer.GetStudentsWithSameFirstName(students);
{
List<Student> students = new List<Student> // Create a
list of students with their details
{
new Student { StudentID = 101, FirstName = "Alice",
LastName = "Smith", Scores = new List<int> { 85, 92, 78, 95 }},
new Student { StudentID = 102, FirstName = "Bob", LastName
= "Johnson", Scores = new List<int> { 76, 88, 90, 70 }},
new Student { StudentID = 103, FirstName = "Charlie",
LastName = "Brown", Scores = new List<int> { 92, 95, 90, 98 }},
new Student { StudentID = 104, FirstName = "David",
LastName = "Lee", Scores = new List<int> { 68, 75, 80, 70 }},
new Student { StudentID = 105, FirstName = "Eve", LastName
= "Davis", Scores = new List<int> { 95, 75, 92, 97 }}
};
var avgBelow75 =
StudentAnalyzer.GetStudentsWithAverageBelow75(students); // Call the
method to get students with an average score below 75
{
Console.WriteLine($"Grade {g.Grade}: {string.Join(",
", g.FullNames.Select(name => $"\"{name}\""))}"); // Print
the grade value and the list of students with that grade
}
}
}
}
students.AddRange(newstudents);
• Purpose: Combines the `newstudents` list into the existing `students` list.
• Functionality: The `AddRange` method appends all elements from `newstudents` to `students`,
resulting in a single list containing all student data.
SUMMARY
Purpose of the program
• Identifies students whose average score is below 75.
• Groups students who share the same first name.
• Groups students who received the same score in the same subject.
It uses a helper class library (`ClassLibrary1`) that defines models and analysis methods for students.
Step-by-Step Breakdown
1. Import Namespaces**
using System;
using System.Collections.Generic;
using System.Linq;
using ClassLibrary1;
using static ClassLibrary1.GradeGroup;
• Brings in necessary system and custom namespaces for collection, LINQ, and the class library.
* An ID,
* First and last name,
* A list of 4 integer scores (assumed to be scores in 4 subjects).
students.AddRange(newstudents);
• Appends the `newstudents` list to the original `students` list.
• Now `students` contains 15 students.
Print Results:
Print Results:
- Analyzes students to find those who received the exact same grade in a specific subject
Print Results:
foreach (var group in sameGradeGroups.GroupBy(g => g.SubjectIndex))
{
Console.WriteLine($"\nGrouped by same grade in Subject {group.Key + 1}:");
foreach (var g in group)
{
Console.WriteLine($"Grade {g.Grade}: {string.Join(", ", g.FullNames.Select(name =>
$"\"{name}\""))}");
}
}
```
Example of output:
```
Students with average below 75:
- David Lee = 73.25
- Dan Hall = 61.00
- Hank Walker = 58.00
1. Find underperformers.
2. Detect naming duplicates.
3. Group similar subject grades.
• It outputs all these insights in an organized way.
using System.Collections.Generic;
using System.Linq;
using ClassLibrary1;
namespace UnitTesting
[TestClass]
};
[TestMethod]
public void
studentsWithAverageBelow75_ShouldReturnCorrectNamesAndGrades()
var result =
StudentAnalyzer.GetStudentsWithAverageBelow75(students);
[TestMethod]
var result =
StudentAnalyzer.GetStudentsWithSameFirstName(students);
Assert.IsNotNull(aliceGroup);
Assert.AreEqual(2, aliceGroup.Students.Count);
[TestMethod]
Assert.IsNotNull(match);
using ClassLibrary1;
// Check that Bob Johnson and Dan Hall are in the result
CollectionAssert.Contains(fullNames, "Bob Johnson");
CollectionAssert.Contains(fullNames, "Dan Hall");
1. `GetStudentsWithAverageBelow75`
• Purpose: Finds and returns a list of students whose average score across all subjects is below 75.
• Inputs:
`students`: List<Student> — a list of `Student` objects, each containing scores.
Outputs:
• List of `StudentResult` objects (or similar), each representing a student with:
• FullName
• AverageScore
How it works:
1. Calculate each student's average score:
- For each `Student`, sum their scores and divide by the number of scores.
2. Filter students:
- Select only those students whose average score is less than 75.
3. Create result objects:
- For each filtered student, create an object containing their full name and average score.
2. `GetStudentsWithSameFirstName`
• Purpose: Group students by their first names, returning groups where more than one student
shares the same first name.
Inputs:
- `students`: List<Student> — list of students.
Outputs:
• List of `FirstNameGroup` objects, each containing:
• `FirstName`
• List of student full names (`Students` or similar)
How it works:
1. Group students by FirstName:
- Use LINQ `GroupBy` on `FirstName`.
2. Filter groups:
- Keep only groups where the count of students > 1.
3. Create group objects:
- For each group, store the first name and the list of full names of students sharing that name.
3. `GetSameGradeGroups`
• Purpose: Group students based on their grade in each subject, identifying students who share
the same score in each subject.
Inputs:
- `students`: List<Student>
Outputs:
• List of `GradeGroup` objects, each containing:
• `SubjectIndex` (which subject)
• `Grade` (the score)
• List of students who have that grade in that subject (`FullNames`)
How it works:
1. Iterate over subjects:
- For each subject index (assuming fixed number of subjects), examine each student's score.
2. Group by score:
- For each subject, group students by their score.
3. Create groups:
- For each score, create a `GradeGroup` with subject index, grade, and list of students' full names.
SUMMARY
Namespaces and Imports
These lines import necessary .NET and custom libraries. The `StudentAnalyzer` and `Student` classes are
assumed to be defined in `ClassLibrary1`.
-This private method returns a list of sample `Student` objects for reuse across multiple tests.
[TestMethod]
public void studentsWithAverageBelow75_ShouldReturnCorrectNamesAndGrades()
* This test checks if students with **average scores below 75** are properly returned.
* Ensures Bob and Dan (low scorers) are included, and Alice (a high scorer) is not.
* Confirms that Bob and Dan’s average scores are actually below 75.
* This test verifies if students with the **same first name** are grouped correctly.
* Confirms that the "Alice" group exists and contains both Alice students.
[TestMethod]
public void sameGradeGroups_ShouldIncludeCommonGrade()
* This test checks if students who received the **same grade in the same subject** are grouped.
* Looks for students who both scored **60 in Subject 2** (index 1).
* Ensures both Bob and Dan are included in that group.