All posts by kpark

Class & Inheritance

Indexer
http://dis.dankook.ac.kr/lectures/hci09/entry/Indexer

Property (속성)
http://dis.dankook.ac.kr/lectures/hci09/entry/Property

Method overloading (메소드 오버로딩) Method overriding (메소드 오버라이딩)
http://dis.dankook.ac.kr/lectures/hci09/entry/Method-Overloading

Abstract Class (추상 클래스)
http://dis.dankook.ac.kr/lectures/hci09/entry/Abstract-class

Sealed Class (봉인 클래스)
http://dis.dankook.ac.kr/lectures/hci09/entry/Sealed-Class

C# Formatting Numeric String

http://dis.dankook.ac.kr/lectures/hci09/entry/C-String-Formatting

Formatting Standard Numeric Format Strings
http://msdn.microsoft.com/en-us/library/s8s7t687(VS.80).aspx

Formatting Custom Numeric Format Strings
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx

Formatting Numeric Format Strings (Alignment)
Integer    http://www.csharp-examples.net/string-format-int/
Double    http://www.csharp-examples.net/string-format-double/
DateTime http://www.csharp-examples.net/string-format-datetime/
Align String with Spaces  http://www.csharp-examples.net/align-string-with-spaces/
Indent String with Spaces http://www.csharp-examples.net/indent-string-with-spaces/
IFormatProvider               http://www.csharp-examples.net/iformatprovider-numbers/
Custom IFormatProvider   http://www.csharp-examples.net/custom-iformatprovider/

























































Character Description Examples Output
C or c Currency Console.Write(“{0:C}”, 2.5); $2.50
Console.Write(“{0:C}”, -2.5); ($2.50)
D or d Decimal Console.Write(“{0:D5}”, 25); 25
E or e Scientific Console.Write(“{0:E}”, 250000); 2.50E+05
F or f Fixed-point Console.Write(“{0:F2}”, 25); 25
Console.Write(“{0:F0}”, 25); 25
G or g General Console.Write(“{0:G}”, 2.5); 2.5
N or n Number Console.Write(“{0:N}”, 2500000); 2,500,000.00
X or x Hexadecimal Console.Write(“{0:X}”, 250); FA
Console.Write(“{0:X}”, 0xffff); FFFF

 

C# Array/ArrayList

Person[] pArray = new Person[5];

// 만약 Person 객체를 하나만 생성한 후 for문에서 공유해 사용할 경우
// 마지막으로 입력된 데이터로 모든 데이터값이 치환됨
Person p = new Person();
for (int i = 0; i < 5; i++) {
    p.Name = Console.ReadLine();           // 입력정보
    p.age = int.Parse(Console.ReadLine()); // 입력정보
    pArray[i] = p;                                  // 리스트에 들어간 모든 원소는 동일한 p
}

Person[] pArray = new Person[5];

// 아래와 같이 for문 안에 Person p = new Person()와같이 새로운 객체를 생성해야
// 각자 다르게 입력된 정보가 들어가게 됨
for (int i = 0; i < 5; i++) {
    Person p = new Person();
    p.Name = Console.ReadLine();           // 입력정보
    p.age = int.Parse(Console.ReadLine()); // 입력정보
    pArray[i] = p;                                  // 이때 p는 새로운 Person객체
}


http://dis.dankook.ac.kr/lectures/hci10/entry/C-ArrayList
 (ArrayList 사용예)

Assignment 2

연습문제 (2)


□ 단원 : C# 기초
□ 목표 : C# 프로그램 기초
□ 주요 연습 내용 : array (또는 ArrayList), foreach, 속성, 클래스, 상속 연습9481023359.cs연습문제 Ex2 (Due by 10/14 금 24시까지)
-cyber 강의실 (cyber.dku.edu)로 source code, executable file, solution/project VC# file, 보고서를 학번_이름_Ex2.zip으로 묶어서 낼 것. 보고서 (30%)


[연습문제]
1. Person 클래스를 정의하라 (그리고, Person.cs 파일로 저장한다). (20%)
Person 클래스는 이름, 나이, 신장, 체중, 성별을 데이터 멤버로 포함한다.
– private string name;
– private int age;
– private float height;
– private float weight;
– private Gender gender; // Gender는 enum 타입 {Female, Male}
– private Activity activity; // Activity는 enum 타입 {Low, Medium, High}


각 멤버 필드에 대한 속성(Property)를 지정하여 외부에서 사용할 수 있도록 한다.
– public string Name { get; set;} // get과 set에 대한 내부정의 필요
– public int Age { get; set;}
– public float Height { get; set;}
– public float Weight { get; set;}
– public Gender Gender { get; set;}
– public Activity Activity { get; set;}


그리고 메소드는 다음을 포함한다.
– public Person()                // 기본 생성자
– public Person(string, int, float, float, Gender)  // 형변환 생성자
– public virtual void Print() // Person의 이름, 나이, 신장, 몸무게, 성별, 활동량을 출력
– public override string ToString() // Print와 동일하게 string으로 출력


2. Person 클래스에서 상속받은  PersonDailyCaloricIntakeCalculator 클래스를 정의하라 (그리고, PersonDailyCaloricIntakeCalculator.cs 파일로 저장한다). 이 클래스는 아래의 메소드를 추가로 포함한다. (20%)
– public float GetNormalWeight();  // Person의 표준체중 (Ex1과 동일)
– public BMI GetBMI();     // Person의 비만도 (Ex1과 동일)
– public float GetDailyCaloricIntake();    // Person의 하루권장섭취량
        // 한국인 하루권장섭취량 남성 2600 kcal 여성 2100 kcal
        // 기초대사량산출공식 (Herris Benedict Equation)
        // 기초대사량(남성) = 66.47 + (13.75*weight) + (5*height) – (6.76*age)
        // 기초대사량(여성) = 655.1 + (9.05*weight) + (1.85*height) – (4.68*age)
        // 활동대사량 = 저활동적(1.3)/중간활동적(1.5)/활동적(1.75)
        // 개인별 하루권장섭취량(kcal) = 기초대사량 * 활동대사량
      – public override void Print();  // Person(베이스클래스)정보와 추가적으로
     // 표준체중, 비만도, 하루권장섭취량을 출력
– public override string ToString()   // Print와 동일하게 string으로 출력


3. DailyCaloricIntakeCalculator 클래스의 Main 함수에서는 PersonDailyCaloricIntakeCalculator 객체를 5개 이상입력 받아서 array 또는 ArrayList pArray에 Add하고 난 후 pArray에 있는 모든 정보에 대해서 Print 출력한다. (20%)
http://msdn.microsoft.com/en-us/library/system.collections.arraylist(VS.71).aspx
– for 문을 사용하여 5명 이상 PersonDailyCaloricIntakeCalculator에 개인 정보를 입력받아서 추가한 후에
– 전체 배열에 있는 개인별 나이/신장/체중/성별/활동량 및 표준체중/비만도/하루권장섭취량을 출력한다. (foreach구문과 PersonDailyCaloricIntakeCalculator 클래스의 ToString() 을 사용할 것)


4. 프로그램에 본인이 더 테스트해보고 싶은 Method나 routine을 추가하라. (10%)

OOP

Class – defines the grouping of data and code, the “type” of an object
Instance – a specific allocation of a class
Message – sent to objects to make them act
Method – a “function” that an object knows how to perform
Instance Variables – a specific piece of data belonging to an object
Property – away to access instance variables and other attributes of an object
Encapsulation – keep implementation private and seperate from interface
Polymorphism – different objects, same interface
Inheritance – hierarchical organization. share code, customize or extend behaviors

pass by value vs pass by reference vs pass by output

http://dis.dankook.ac.kr/lectures/hci10/entry/pass-by-value와-pass-by-reference와-pass-by-output-비교

pass value type by value (값형식을 값에 의한 전달)
pass reference type by value (참조형식을 값에 의한 전달)
-copy of reference가 전달

pass value type by reference (값형식을 참조에 의한 전달)
ref 키워드 사용
pass reference type by reference (참조형식을 참조에 의한 전달)
ref 키워드 사용

pass value type by output (값형식을 output에 의한 전달)
out 키워드 사용
pass reference type by output (참조형식을 output에 의한 전달)
out 키워드 사용

static class vs singleton design pattern

공용 데이터를 저장하여 사용하고자 할 때, singleton 패턴이나 static 클래스를 사용한다.

싱글톤 패턴 (Singleton design pattern)
-싱글톤 패턴이란 single instance object(해당 클래스의 인스턴스 하나)가 만들어지고, 어디서든지 그 싱글톤에 접근할 수 있도록 하기 위한 패턴

정적 클래스 (Static class)
-static 키워드를 사용한 정적 클래스는 정적 멤버만 포함 가능
-정적 클래스는 new 키워드를 사용하여 정적 클래스의 인스턴스를 생성할 수 없음
-정적 클래스는 봉인 클래스 (sealed class) 임
-static 클래스는 single-instance, global 데이터를 저장하여 사용하는 용도로 적당
-그러나, 싱글톤은 인자(parameter)나 객체(object)로 사용가능하나 정적클래스는 불가능

http://dis.dankook.ac.kr/lectures/hci10/entry/singleton-pattern과-static-class-비교