In: Computer Science
Student
Attribute |
Data type |
(student) id |
String |
firstName |
String |
lastName |
String |
courses |
Array of Course objects |
Student class must have 2 constructors: one default (without parameters), another with 4 parameters (for setting the instance variables listed in the above table)
In addition Student class must have setter and getter methods for the 4 instance variables, and getGPA method that calculates GPA from the courses’ grades and returns it.
using System;
//creating a class courses
public class courses
{
//declaring Subject name and mark
public string SName;
public int Mark;
}
//creating the class student
public class Student
{
//declaring variable id,firstName,lastName
//and array of courses object
string Id;
string firstName;
string lastName;
courses[] course;
//constructor with no parameter
Student()
{
}
//constructor with parameter
//setting the 4 instance variable
Student(string Id,string firstName,string lastName,courses[]
course)
{
this.Id=Id;
this.firstName=firstName;
this.lastName=lastName;
this.course=course;
}
// getter method for Id
public string getId()
{
return Id;
}
// setter method for Id
public void setId(string Id)
{
this.Id = Id;
}
//getter method for firstName
public string getfirstName()
{
return firstName;
}
//setter method for firstName
public void setfirstName(string firstName)
{
this.firstName=firstName;
}
//getter method for lastName
public string getlastName()
{
return lastName;
}
//setter method for lastName
public void setlastName(string lastname)
{
this.lastName=lastName;
}
//getter method for array of course object
public courses[] getcourse()
{
return course;
}
//setter method for array of course object
public void setcourse(courses[] Course)
{
this.course=course;
}
//method to get GPA
public float getGPA()
{
//declaring and intializing
//total mark and count
int T_mark=0,count=0;
//calculating the total Mark
//counting number of mark
foreach(var item in course)
{
T_mark+=item.Mark;
count+=1;
}
//calculating GPA
float GPA=(T_mark/count);
return GPA;
}
public static void Main(string[] args)
{
//creating an array of courses class object
courses[] course=new courses[3];
//intializing the elements of courses class object
course[0]=new courses();
course[0].SName="computer";
course[0].Mark=85;
course[1]=new courses();
course[1].SName="chemistry";
course[1].Mark=70;
course[2]=new courses();
course[2].SName="physics";
course[2].Mark=75;
//creating the object student class
//constructor with no parameter
Student s1=new Student();
//constructor with 4 parameter
Student s2=new Student("s120","david","jepherson",course);
//printing the result of various getter method
Console.Write("FirstName is : ");
Console.WriteLine(s2.firstName);
Console.Write("LastName is : ");
Console.WriteLine(s2.lastName);
Console.Write("GPA is : ");
Console.WriteLine(s2.getGPA());
Console.WriteLine("Course and Marks are : ");
//getcourse return an array of courses class object
//each object is traversed by foreach loop
foreach(var item in s2.getcourse())
{
Console.Write(item.SName);
Console.Write(" : ");
Console.WriteLine(item.Mark);
}
}
}
PROGRAM FILE:-
OUTPUT:-