>>>>>>>>>>>>>>>Programing C#,<<<<<<<<<<<<<<<<<<< Create a Student class. Do foll
ID: 3604408 • Letter: #
Question
>>>>>>>>>>>>>>>Programing C#,<<<<<<<<<<<<<<<<<<<
Create a Student class. Do followings in Student class.
Declare these data members:
1. a static int Count.
2. a private static readonly Random variable rnd.
private static readonly Random rnd = new Random();
3. private string firstName, lastName, int sID, create public properties for these three data members.
4. Two constructors
a). Constructor 1: public Student(string first , string last , int id)
----this constructor set values for firstName, lastName, sID, and increase Count value by 1.
b). Constructor 2: public Student(string first="", string last = "")
----This constructor set values for firstName, lastName, and increase Count value by 1.
----Create StudentID by calling rnd.Next(1000,9999).
----Note this constructor is called when
Student s1 = new Student();
Student s2 = new Student("Peter");
Student s3 = new Student("Morgan", "Simmons");
5. In main()
static void Main(string[] args)
{
List<Student> myList = new List<Student>();
Student s1 = new Student();
s1.FirstName = "John";
s1.LastName = "Smith";
s1.StudentID = 2560;
myList.Add(s1);
Student s2 = new Student("Peter");
myList.Add(s2);
Student s3 = new Student("Morgan", "Simmons");
myList.Add(s3);
Student s4 = new Student("James", "Walters");
myList.Add(s4);
Student s5 = new Student("Linda", "Scott", 1005);
myList.Add(s5);
Console.WriteLine();
Console.WriteLine("Total students: {0}", Student.Count);
//Create foreach loop to iterate through list,
//use Console.WriteLine to display Student Name and Student ID.
}
Explanation / Answer
using System;
using System.Collections.Generic;
class Student {
//data members
static int Count;//this stores the total number of students .this is a static variable
private static readonly Random rnd = new Random();//This a random type obj to randomly assign student id
private string firstName, lastName;////personal data
private int sID;//personal data
//getter and setter function for first name ,lastname ,sID
public void setfirstName( string fName ) {
firstName = fName;
}
public void setlastName( string lName ) {
lastName = lName;
}
public void setID( int id ) {
sID = id;
}
public string getfirstName() {
return firstName ;
}
public string getlastName() {
return lastName ;
}
public int getID( ) {
return sID ;
}
// parameterized constructor for student
public Student(string first , string last , int id)
{
//it uses setter function to assign formal arguments to data members
setfirstName(first );
setlastName(last);
setID(id);
Count++; //this increaes count value by one each time a new student obj is created
}
public Student(string first="", string last = "")//this handles when null strings are given as arguments
{
setfirstName(first);
setlastName(last);
setID(rnd.Next(1000,9999));//it sets id as a random number between 1000 to 9999
Count++;
}
static void Main(string[] args)
{
List<Student> myList = new List<Student>();//student type of list is created.
//s1 object is created
Student s1 = new Student();
s1.firstName = "John";
s1.lastName = "Smith";
s1.sID = 2560;
myList.Add(s1);//s1 is added to mylist
//constructor 2 is used
Student s2 = new Student("Peter");
myList.Add(s2);
//constructor 2 is used
Student s3 = new Student("Morgan", "Simmons");
myList.Add(s3);
//constructor 2 is used
Student s4 = new Student("James", "Walters");
myList.Add(s4);
//constructor 1 is used
Student s5 = new Student("Linda", "Scott", 1005);
myList.Add(s5);
Console.WriteLine();
Console.WriteLine("Total students: {0}", Student.Count);
for(int i=0;i<Student.Count;i++)
Console.WriteLine("student id : {0} FirstName : {1} LastName : {2} ",myList[i].getID(),myList[i].getfirstName(),myList[i].getlastName());
//Create foreach loop to iterate through list,
//use Console.WriteLine to display Student Name and Student ID.
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.