Lập trình C# - Quản lý sinh viên
Viết chương trình C# thực hiện các công việc sau:
- Tạo lớp trừu tượng Student với các trường name, year, phương thức hiển thị, phương thức trừu tượng Average (tính trung bình điểm)
- Tạo lớp StudentCollege kế thừa từ Student và bổ sung 3 trường score1, score2, score3. Ghi đè phương thức Average và tạo mới phương thức hiển thị.
- Tạo lớp StudentUniversity kế thừa từ StudentCollege và bổ sung 1 trường score4. Ghi đè phương thức Average và tạo mới phương thức hiển thị.
- Trong Main viết code để test chương trình.
Bước 1: Kích chuột phải vào Solution chọn Add -> New Project ->Nhập tên( “AbstractStudent”) -> Chọn OK.
Bước 2: Tạo lớp Student và code theo gợi ý sau:
using System;
using System.Collections.Generic;
using System.Text;
namespace AbstractStudent
{
abstract class Student{
public string FullName { get; set; }
public int YearOfBirth { get; set; }
public Student(String fullName, int yearOfBirth)
{
this.FullName = fullName;
this.YearOfBirth = yearOfBirth;
}
public void Display()
{
Console.WriteLine("Họ và tên: {0}",FullName);
Console.WriteLine("Năm sinh: {0}", YearOfBirth);
}
public abstract double Average();
}
}
Bước 3: Tạo lớp StudentCollege và code theo gợi ý sau:
using System;
using System.Collections.Generic;
using System.Text;
namespace AbstractStudent
{ // Lop sinh vien cao dang ke thua lop Student
class StudentCollege : Student
{
private double _score1;
private double _score2;
private double _score3;
public double Score1
{
get
{
return _score1;
}
set
{
if (value < 0 || value > 10)
Console.WriteLine("Nhập điểm trong khoảng từ 0 đến 10");
else
_score1 = value;
}
}
public double Score2
{
get
{
return _score2;
}
set
{
if (value < 0 || value > 10)
Console.WriteLine("Nhập điểm trong khoảng từ 0 đến 10");
else
_score2 = value;
}
}
public double Score3
{
get
{
return _score3;
}
set
{
if (value < 0 || value > 10)
Console.WriteLine("Nhập điểm trong khoảng từ 0 đến 10");
else
_score3 = value;
}
}
public StudentCollege(String fullName, int yearOfBirth, double score1, double score2, double score3) : base(fullName, yearOfBirth)
{
this.Score1 = score1;
this.Score2 = score2;
this.Score3 = score3;
}
public override double Average()
{
return (Score1 + Score2 + Score3) / 3;
}
public new void Display()
{
base.Display();
Console.WriteLine("Điểm môn 1: {0}", Score1);
Console.WriteLine("Điểm môn 2: {0}", Score2);
Console.WriteLine("Điểm môn 3: {0}", Score3);
}
}
}
Bước 4: Tạo lớp StudentUniversity và code theo gợi ý sau:
using System;
using System.Collections.Generic;
using System.Text;
namespace AbstractStudent
{
class StudentUniversity:StudentCollege
{
private double _score4;
public double Score4
{
get
{
return _score4;
}
set
{
if (value < 0 || value > 10)
Console.WriteLine("Nhập điểm trong khoảng từ 0 đến 10");
else
_score4 = value;
}
}
public StudentUniversity(string fullName, int yearOfbirth, double score1, double score2, double score3, double score4):base(fullName, yearOfbirth, score1, score2, score3)
{
this.Score4 = score4;
}
public new void Display()
{
base.Display();
Console.WriteLine("Điểm môn 4: {0}",Score4);
}
public override double Average()
{
return (Score1+Score2+Score3+Score4)/4;
}
}
}
Bước 5: Trong Program code test như sau:
using System;
using System.Text;
namespace AbstractStudent
{
class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
// Nhap sinh vien cao dang va in ra thong tin
Console.WriteLine("Sinh viên cao đẳng");
StudentCollege sv1= new StudentCollege("Thích Học Lại",1987,6,7,8);
sv1.Display();
Console.WriteLine("Điểm trung bình là:{0}", sv1.Average());
Console.WriteLine("----------------------------------------------");
// Nhap sinh vien cao dang va in ra thong tin
Console.WriteLine("Sinh viên đại học");
StudentUniversity sv2 = new StudentUniversity("Thích Qua Môn", 1976, 6, 7, 8,10);
sv2.Display();
Console.WriteLine("Điểm trung bình là:{0}", sv2.Average());
Console.ReadKey();
}
}
}
Bước 6: Nhấn Ctrl+F5 để chạy và xem kết quả
Sinh viên cao đẳng
Họ và tên: Thích Học Lại
Năm sinh: 1987
Điểm môn 1: 6
Điểm môn 2: 7
Điểm môn 3: 8
Điểm trung bình là:7
----------------------------------------------
Sinh viên đại học
Họ và tên: Thích Qua Môn
Năm sinh: 1976
Điểm môn 1: 6
Điểm môn 2: 7
Điểm môn 3: 8
Điểm môn 4: 10
Điểm trung bình là:7.75