Question #55303

Using the concept of classes,methods attributes create a class of students that accepts the number of courses taken by a student and outputs the terminal grade of the student.
1

Expert's answer

2015-10-13T03:01:01-0400

Answer on Question #55303, Programming / C#

Using the concept of classes, methods attributes create a class of students that accepts the number of courses taken by a student and outputs the terminal grade of the student.

Solution

Create Course class:

public class Course
{
// enum that represent grades
public enum Grades
{
A=5,
B=4,
C=3,
D=2,
F=1
}
// name of course
public string Title { get; set; }
// grade for this course
public Grades Grade { get; set; }
// constructor
public Course(string title, Grades grade)
{
Grade = grade;
Title = title;
}
}

Create Student class

class Student
{
public string FirstName { get; set; }
public string LastName { get; set; }
// return full student name
public string FullName
{
get
{
return LastName + ", " + FirstName;
}
}
// constructor
public Student(string first, string last)
{
FirstName = first;
LastName = last;
}
Courses = new List<Course>();
}
// list of courses, that student enroll
public List<Course> Courses;
// return a terminal grade. Calculating as arithmetic average of courses grades
// and return a Grade as enum
public Course.Grades Grade
{
get
{
double sum = 0;
foreach (Course course in Courses)
{
sum += (int)course.Grade;
}
int grade= (int)(sum / Courses.Count);
return (Course.Grades) grade;
}
}
}

Example of program work


static void Main(string[] args)
{
// create new courses list and annd 3 courses
List<Course> courses= new List<Course>();
courses.Add(new Course("Math", Course.Grades.A));
courses.Add(new Course("Programming", Course.Grades.B));
courses.Add(new Course("Phisics", Course.Grades.C));
// create 2 students
Student student = new Student("John", "Ts");
Student student2 = new Student("Bill", "Gates");
// assigned student to coursed
student.Courses = courses;
// display information
Console.WriteLine("{0} has terminal grade: {1}. Courses:", student.FullName, student.Grade);
foreach (Course course in student.Courses)
{
Console.WriteLine("{0} - {1}", course.Grade, course.Title);
}
Console.WriteLine();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS