Code Final
Code Final
CLASS LIBRARY
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
}
// Find group of students who have the same grade in each subject
public static List<GradeGroup> GetSameGradeGroups(List<Student> students)
{
return Enumerable.Range(0, 4) //Loop through subjects (0-3)
.SelectMany(subjectIndex => // For each subject index, group students by their
score
students
.GroupBy(s => s.Scores[subjectIndex]) // Group students by their score in the
current subject
.Where(g => g.Count() > 1) // Filter groups that have more than
one student
.Select(g => new GradeGroup // Projefct each group into a
GradeGroup object
{
SubjectIndex = subjectIndex, // Set the subject index
Grade = g.Key, // Set the grade for this group
// List of full name of students with this grade
FullNames = g.Select(s => $"{s.FirstName} {s.LastName}").ToList()
}))
.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:
- Uses LINQ `Where` to filter students where the average of their scores is less than 75.
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:
- Filters groups where the count of students is greater than 1 (meaning multiple students share that first
name).
- `Students`: A list of full names (`FirstName LastName`) of all students in that group.
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:
- Filters groups with more than one student (i.e., multiple students with the same grade).
- 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.
The code defines four classes that represent different aspects of student data:
- Student Class
{
public int StudentID { get; set; }
Purpose: Represents a student with properties for their ID, first name, last name, and a list of scores.
Properties:
- `Scores`: A list of integers representing the scores the student has received in various subjects.
StudentWithAverage Class
Properties:
- `FullName`: A string that combines the student's first and last names.
FirstNameGroup Class
}
Purpose: Represents a group of students who share the same first name.
Properties:
- `Students`: A list of strings containing the full names of students with that first name.
GradeGroup Class
Purpose: Represents a group of students who received the same grade in a specific subject.
Properties:
- `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.
- This static class contains methods for analyzing student data. Being static means that it cannot be
instantiated, and its methods can be called directly on the class itself.
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.
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
using System;
using System.Collections.Generic;
using System.Linq;
using ClassLibrary1;
using static ClassLibrary1.GradeGroup;
students.AddRange(newstudents);
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 }}
};
new Student { StudentID = 106, FirstName = "Amy", LastName = "Clark", Scores = new List<int>
{ 90, 85, 88, 91 } },
new Student { StudentID = 107, FirstName = "Ivy", LastName = "Wright", Scores = new List<int>
{ 76, 81, 74, 80 } },
new Student { StudentID = 108, FirstName = "Cathy", LastName = "Moore", Scores = new List<int>
{ 95, 97, 96, 98 } },
new Student { StudentID = 109, FirstName = "Dan", LastName = "Hall", Scores = new List<int>
{ 64, 60, 62, 58 } },
new Student { StudentID = 110, FirstName = "Ella", LastName = "Green", Scores = new List<int>
{ 85, 86, 89, 90 } },
new Student { StudentID = 111, FirstName = "Frank", LastName = "Young", Scores = new List<int>
{ 70, 72, 68, 75 } },
new Student { StudentID = 112, FirstName = "Grace", LastName = "Baker", Scores = new List<int>
{ 91, 93, 92, 90 } },
new Student { StudentID = 113, FirstName = "Hank", LastName = "Walker", Scores = new List<int>
{ 55, 60, 58, 59 } },
new Student { StudentID = 114, FirstName = "Ivy", LastName = "Scott", Scores = new List<int>
{ 78, 82, 79, 81 } },
new Student { StudentID = 115, FirstName = "Jack", LastName = "Adams", Scores = new List<int> {
88, 85, 87, 89 } }
};
Console.WriteLine("Students with average below 75:"); // Output the results for students with
average scores below 75
// Print each student's full name and average score formatted to two decimal places
// Call the method to get groups of students with the same first name
Console.WriteLine("\nStudents with the same first name:"); // Output the results for students with
the same first name
foreach (var g in group) // Loop through each grade group for this subject
1. `Main` Method
Purpose: This is the entry point of the program where execution begins. It orchestrates the creation of
student data, calls analysis methods, and outputs results to the console.
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
It uses a helper class library (`ClassLibrary1`) that defines models and analysis methods for students.
Core Components Used
Student: A class representing each student with ID, name, and a list of scores.
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).
4. Add More Students
students.AddRange(newstudents);
Print Results:
- Displays the full name and average score (formatted to 2 decimal places) of each student found above.
- Finds students who share the same first name (e.g., two students named "Ivy").
Print Results:
Console.WriteLine("\nStudents with the same first name:");
foreach (var group in firstNameGroups)
{
Console.WriteLine($"{group.FirstName}: {string.Join(", ", group.Students.Select(name =>
$"\"{name}\""))}");
}
```
- Analyzes students to find those who received the exact same grade in a specific subject
Print Results:
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.
UNIT TEST
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Linq;
using ClassLibrary1;
namespace UnitTesting
{
[TestClass]
public class StudentAnalyzerTests
{
private List<Student> getSampleStudents()
{
return new List<Student>
{
new Student { FirstName = "Alice", LastName = "Smith", Scores = new List<int> { 85,
92, 78, 95 } },
new Student { FirstName = "Bob", LastName = "Johnson", Scores = new List<int>
{ 50, 60, 65, 70 } },
new Student { FirstName = "Alice", LastName = "Jones", Scores = new List<int> { 88,
90, 85, 87 } },
new Student { FirstName = "Charlie", LastName = "Brown", Scores = new List<int>
{ 92, 95, 90, 98 } },
new Student { FirstName = "Dan", LastName = "Hall", Scores = new List<int> { 64,
60, 62, 58 } }
};
}
[TestMethod]
public void studentsWithAverageBelow75_ShouldReturnCorrectNamesAndGrades()
{
var students = getSampleStudents();
var result = StudentAnalyzer.GetStudentsWithAverageBelow75(students);
[TestMethod]
public void studentsWithSameFirstName_ShouldReturnAliceGroup()
{
var students = getSampleStudents();
var result = StudentAnalyzer.GetStudentsWithSameFirstName(students);
[TestMethod]
public void sameGradeGroups_ShouldIncludeCommonGrade()
{
var students = getSampleStudents();
var result = StudentAnalyzer.GetSameGradeGroups(students);
using ClassLibrary1;
// First unit test: Checks that only students with an average score below 75 are returned
[TestMethod]
public void studentsWithAverageBelow75ShouldReturnCorrectNamesAndGrades()
{
var students = getSampleStudents(); // Get sample data
var result = StudentAnalyzer.GetStudentsWithAverageBelow75(students); // Call method under
test
var fullNames = result.Select(r => r.FullName).ToList(); // Get full names from result
// Check that Bob Johnson and Dan Hall are in the result
CollectionAssert.Contains(fullNames, "Bob Johnson");
CollectionAssert.Contains(fullNames, "Dan Hall");
// Check that Alice Smith is NOT in the result (since her average is above 75)
CollectionAssert.DoesNotContain(fullNames, "Alice Smith");
// Additional checks: Verify that the average scores are actually below 75
var bob = result.FirstOrDefault(r => r.FullName == "Bob Johnson");
var dan = result.FirstOrDefault(r => r.FullName == "Dan Hall");
Assert.IsTrue(bob.AverageScore < 75); // Check Bob's average
Assert.IsTrue(dan.AverageScore < 75); // Check Dan's average
}
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
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.
[TestMethod]
public void studentsWithSameFirstName_ShouldReturnAliceGroup()
```
* 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.