In C# this program output should display: Number of Students: 5 Student Name: Jo
ID: 3859884 • Letter: I
Question
In C# this program output should display:
Number of Students: 5
Student Name: John Smith, Student ID: 2560
Student Name: Peter, Student ID: rnd number
Student Name: Morgan Simons, Student ID: rnd number
Student Name: James Walters, Student ID: rnd number
Student Name: Linda Scott, Student ID: rnd number
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)
{
//Create Foreach loop here to go through list
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{
public int Count;
private string firstName, lastName;
private int studentID;
private static readonly Random rnd = new Random();
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
public int StudentID
{
get { return studentID; }
set { studentID = value; }
}
public Student()
{
this.FirstName=FirstName;
this.LastName=LastName;
this.StudentID=StudentID;
++Count;
}
public Student(string first)
{
this.FirstName=first;
this.LastName=LastName;
this.StudentID=StudentID;
++Count;
}
public Student(string first , string last , int id)
{
this.FirstName=first;
this.LastName=last;
this.StudentID=id;
++Count;
}
public Student(string first, string last)
{
this.FirstName=first;
this.LastName=last;
this.StudentID =rnd.Next(1000,9999);
++Count;
}
}
class Stud{
public static void Main(string[] args)
{
string firstName, lastName;
int sID;
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 ConsoleConsole.WriteLine to display Student Name and Student ID.
Console.WriteLine("Number of Students: "+myList.Count);
foreach (Student lt in myList)
{
Console.WriteLine("StudentName: "+lt.FirstName+" "+lt.LastName+", Student ID: "+lt.StudentID);
}
}}