«   2019/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30          
Statistics Graph
관리 메뉴

언제나 휴일

[C#] 캡슐화 실습 - 학생 클래스 본문

언어 자료구조 알고리즘/프로그래밍 실습

[C#] 캡슐화 실습 - 학생 클래스

언제나 휴일 언제나휴일 2019.08.08 11:33

실습) 다음 내용을 만족하는 학생 클래스를 정의하시오.

목적: 멤버 속성의 이해

학생은 생성할 때 학생 번호와 학생 이름을 전달받습니다. 
이 외에 학생의 국어, 영어, 수학 성적을 -1로 설정합니다.
학생을 생성한 후에는 학생의 국어, 영어, 수학 성적을 입력할 수 있습니다. 
만약 학생 성적이 0에서 100을 벗어나면 -1로 설정합니다.
학생 이름과 번호, 각 과목의 성적과 총점 및 평균을 확인할 수 있습니다. 
그리고 학생 정보를 확인하는 기능을 제공합니다.

*2019년 공주대에서
예제 소스) StudentA.cs (자바 스타일)

using System;

 

 

namespace 캡슐화_실습2___학생

{

    //자바 혹은 CPP 스타일

    class StudentA

    {

        int num;

        public int GetNum()

        {

            return num;

        }

 

        string name;

        public string GetName()

        {

            return name;

        }

 

        int[] scores = new int[3];

        public void SetKorea(int score)

        {

            if (score > 100)

            {

                score = -1;

            }

            if (score < 0)

            {

                score = -1;

            }

            scores[0] = score;

        }

        public int GetKorea()

        {

            return scores[0];

        }

 

        public void SetMath(int score)

        {

            if (score > 100)

            {

                score = -1;

            }

            if (score < 0)

            {

                score = -1;

            }

            scores[1] = score;

        }

        public void SetEnglish(int score)

        {

            if (score > 100)

            {

                score = -1;

            }

            if (score < 0)

            {

                score = -1;

            }

            scores[2] = score;

        }

       

        public int GetMath()

        {

            return scores[1];

        }

        public int GetEnglish()

        {

            return scores[2];

        }

        public StudentA(int num, string name)

        {

            this.num = num;

            this.name = name;

            scores[0] = -1;

            scores[1] = -1;

            scores[2] = -1;

        }

 

       

       

       

        public int GetTotal()

        {

            int sum = 0;

            for(int i=0;i<3;i++)

            {

                if(scores[i]!=-1)

                {

                    sum += scores[i];

                }

            }

            return sum;

        }

        public int GetAverage()

        {

            return GetTotal() / 3;// cnt;

        }

        public override string ToString()

        {

            return string.Format("{0},{1}", num, name);

        }

    }

}

 

Student.cs

using System;

 

namespace 캡슐화_실습2___학생

{

    class Student

    {

        //int num;

        public int Num

        {

            get;

            private set;

        }

        //string name;

        public string Name

        {

            get;

            private set;

        }

        int[] scores = new int[3];

        public int Korea

        {

            get

            {

                return scores[0];

            }

            set

            {

                if(value>100)

                {

                    value = -1;

                }

                if(value<0)

                {

                    value = -1;

                }

                scores[0] = value;

            }

        }

        public int English

        {

            get

            {

                return scores[1];

            }

            set

            {

                if (value > 100)

                {

                    value = -1;

                }

                if (value < 0)

                {

                    value = -1;

                }

                scores[1] = value;

            }

        }

        public int Math

        {

            get

            {

                return scores[2];

            }

            set

            {

                if (value > 100)

                {

                    value = -1;

                }

                if (value < 0)

                {

                    value = -1;

                }

                scores[2] = value;

            }

        }

        public int Total

        {

            get

            {

                int sum = 0;

                for (int i = 0; i < 3; i++)

                {

                    if (scores[i] != -1)

                    {

                        sum += scores[i];

                    }

                }

                return sum;

            }

        }

        public int Average

        {

            get

            {

                return Total / 3;

            }

        }

 

        public Student(int num,string name)

        {

            Num = num;

            Name = name;

            scores[0] = -1;

            scores[1] = -1;

            scores[2] = -1;

        }

        public override string ToString()

        {

            return string.Format("{0},{1}", Num, Name);

        }

    }

}

 

Program.cs

using System;

 

namespace 캡슐화_실습2___학생

{

    class Program

    {

        static void Main(string[] args)

        {

            StudentA stua = new StudentA(3, "홍길동");

            stua.SetKorea(80);

            stua.SetEnglish(120);

            stua.SetMath(-23);

            Console.WriteLine(stua);

            Console.WriteLine(":{0}", stua.GetKorea());

            Console.WriteLine(":{0}", stua.GetEnglish());

            Console.WriteLine(":{0}", stua.GetMath());

            Console.WriteLine(":{0}", stua.GetTotal());

            Console.WriteLine(":{0}", stua.GetAverage());

 

 

            Student stu = new Student(3, "홍길동");

            stu.Korea = 80;// stua.SetKorea(80);

            stu.English = 120; // stua.SetEnglish(120);

            stu.Math = -23; // stua.SetMath(-23);

            Console.WriteLine(stu);//Console.WriteLine(stua);

            Console.WriteLine(":{0}", stu.Korea); //Console.WriteLine(":{0}", stua.GetKorea());

            Console.WriteLine(":{0}", stu.English); //Console.WriteLine(":{0}", stua.GetEnglish());

            Console.WriteLine(":{0}", stu.Math); //Console.WriteLine(":{0}", stua.GetMath());

            Console.WriteLine(":{0}", stu.Total); //Console.WriteLine(":{0}", stua.GetTotal());

            Console.WriteLine(":{0}", stu.Average); //Console.WriteLine(":{0}", stua.GetAverage());

        }

    }

}

 

실행 화면

0 Comments
댓글쓰기 폼