Interface

Interface (인터페이스)
http://dis.dankook.ac.kr/lectures/hci09/entry/Interface

IEnumerable & IEnumerator Interface
-IEnumerable 인터페이스는 foreach를 사용하여 컬랙션을 반복하는 것을 지원하기 위해 구현하여 사용한다.
http://dis.dankook.ac.kr/lectures/hci11/entry/IEnumerable
http://dis.dankook.ac.kr/lectures/hci09/entry/Enumerator

IEquatable Interface
-IEquatable 인터페이스는 두 객체간에 서로 내부 내용이 같은 지 (예: if(a == b))를 비교하기 위해 구현하여 사용한다.
http://dis.dankook.ac.kr/lectures/hci11/entry/IEquatable
http://dis.dankook.ac.kr/lectures/hci10/72
http://dis.dankook.ac.kr/lectures/hci09/entry/Equals

IComparable Interface
-IComparable 인터페이스는 개체에 대한 기본 정렬(sort) 순서를 지정해주기 위해 구현하여 사용한다. 해당 개체를 배열이나 컬랙션에서 정렬하는데 필요하다.
http://dis.dankook.ac.kr/lectures/hci11/entry/IComparable

CLASS INDEXER, PROPERTY, ABSTRACT, SEALED

Indexer (인덱서)
http://dis.dankook.ac.kr/lectures/hci09/entry/Indexer

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

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

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

polymorphism (다형성)
– Shape/Circle/Rectangle/Triangle/Square 클래스
– class 및 inheritance
– abstract class 및 polymorphism

ShapePolymorphism

override vs new

namespace PersonTest
{
class Person
{
static int count = 0;   // static (class) variables

string name;    // instance variables
int age;        // instance variables

public string Name
{
get
{
return name;
}
set
{
name = value;
}
}

public int Age
{
get
{
return age;
}
set
{
age = value;
}
}

public Person() : this(“”, 0)
{
}

public Person(string name, int age)
{
count++;
this.name = name;
this.age = age;
}

public virtual void Print()    // instance methods
{
Console.WriteLine(“Person Name: {0} Age: {1}”, name, age);
}

public override string ToString()      // instance methods
{
return “Person Name: ” + name + ” Age: ” + age;
}

public static int GetCount() { return count; }  // static (class) methods
public static void PrintCount() { Console.WriteLine(“Person Count: ” + count); }  // static (class) methods
}
}

namespace PersonTest
{
class Student : Person
{
static int scount = 0; // static (class) variables
int id;
public int ID
{
get
{
return id;
}
set
{
id = value;
}
}

public Student() : this(“”, 0, 5208) // Student() => public Student : base()와 같은 의미, 생성자는 상속안됨
{
}

public Student(string name, int age, int id)
: base(name, age)
{
this.id = id;
scount++;
}

public void BasePrint()
{
base.Print();
}

public override void Print() // instance methods
{
Console.WriteLine(“Student Name: {0} Age: {1} Id: {2}”, Name, Age, ID);
}

public override string ToString()  // instance methods
{
return “Student Name: ” + Name + ” Age: ” + Age + ” Id: ” + ID;
}

// Person 클래스의 GetCount & PrintCount와 동일한 이름이므로, Student 감춤. new 키워드를 사용하여 새로정의함.
public static new int GetCount() { return scount; } // static (class) methods
public static new void PrintCount() { Console.WriteLine(“Student Count: ” + scount); }  // static (class) methods
}
}

namespace PersonTest
{
class Program
{
static void Main(string[] args)
{
Person p1 = new Student(“Kyoung”, 22, 1207); // up casting
p1.Age = 30;
p1.Print(); // dynamic binding Student Name: Kyoung Age: 30 ID: 1207
//p1.BasePrint(); // cannot call Student method due to Person
//p1.PrintCount(); // cannot call static method with instance name
Person.PrintCount(); // calls Person.PrintCount() 1
Student.PrintCount(); // calls Student.PrintCount() 1

Student s1 = (Student)p1; // down casting
s1.Name = “HCI”;
s1.Age = 2;
s1.ID = 2016;
s1.Print(); // Student Name: HCI Age: 2 ID: 2016
s1.BasePrint(); // Person Name: HCI Age: 2
Person.PrintCount(); // calls Person.PrintCount() 1
Student.PrintCount(); // calls Student.PrintCount() 1

Student s2 = new Student(“Shin”, 20, 1207);
s2.Print(); // Student Name: Shin Age: 20 ID: 1207
s2.BasePrint(); // Person Name: Shin Age: 20
Person.PrintCount(); // calls Person.PrintCount() 2
Student.PrintCount(); // calls Student.PrintCount() 2

Person p2 = new Person(“Park”, 10);
p2.Print(); // Person Name: Park Age: 10
Person.PrintCount(); // calls Person.PrintCount() 3
Student.PrintCount(); // calls Student.PrintCount() 2

Person p3 = new Person();
p3.Print(); // Person Name: Age: 0
Person.PrintCount(); // calls Person.PrintCount() 4
Student.PrintCount(); // calls Student.PrintCount() 2

Console.WriteLine(“Number of Person: {0}”, Person.GetCount());
Console.WriteLine(“Number of Student: {0}”, Student.GetCount());
}
}
}

 

 

Method Overloading vs Method Overriding

http://dis.dankook.ac.kr/lectures/hci10/entry/Method-Overloading-vs-Method-Overriding

Method Overloading: 동일한 함수명에 매개변수가 다른 함수를 둘 이상 정의하는 것으로, 동일한 함수 기능을 수행하지만 다른 매개변수의 경우를 처리할 때 사용

Method Overriding: 상속받은 파생 클래스에서 동일한 함수명에 동일한 매개변수로 정의하여 함수를 재정의하는 것으로 상속되어진 함수의 기능을 변경해서 재사용하고 싶을 때 사용

class Point : IEquatable<Point>, IComparable<Point>
{
// static member field
protected static int count = 0;
// instance member field
protected int x, y;
// property
public int X
{
get { return this.x; }
set { this.x = value; }
}
public int Y
{
get { return this.y; }
set { this.y = value; }
}
public static int Count
{
get { return count; } // error: this.count 는 사용할 수 없음
}
// constructor & destructor
public Point() : this(0, 0) { }
public Point(int x, int y) { this.x = x; this.y = y; count++; }
~Point() { count–; }
// method
public void SetPosition(int x, int y) { this.x = x; this.y = y; }
public void Move(int x, int y) { this.x += x; this.y += y; }
// override method
public override string ToString() { return (String.Format(“({0}, {1})”, x, y)); }
// virtual method
public virtual void Print() { Console.WriteLine(“X={0} Y={1}”, x, y); }public virtual void Sum(Point other) { x += other.X; y += other.Y; }
// static method
public static int GetCount() { return count; }
// operator++ overload
public static Point operator++(Point p) { ++p.x; ++p.y; return p; }
// operator== overload
public static bool operator==(Point p1, Point p2) { return p1.Equals(p2); }
// operator!= overload
public static bool operator!=(Point p1, Point p2) { return !p1.Equals(p2); }
public override int GetHashCode() { return x ^ y; }
public override bool Equals(object obj)
{
if (!(obj is Point))
return false;
return Equals((Point)obj);
}

// IEquatable
public virtual bool Equals(Point other)
{
if (object.ReferenceEquals(this, other)) // if reference is the same
return true;
return (this.x == other.x && this.y == other.y);
}
// IComparable
public int CompareTo(Point other)
{
if (this.x > other.x)
{
return 1;
}
else if (this.x < other.x)
{
return -1;
}
else
{
return 0;
}
}

}

class Point3D : Point, IEquatable<Point3D>
{// instance member field
protected int z;

// property
public int Z
{
get { return this.z; }
set { this.z = value; }
}

// constructor & destructor
public Point3D() : this(0, 0, 0) { }
public Point3D(int x, int y, int z) : base(x, y) { this.z = z; }
~Point3D() { }

public void SetPosition(int x, int y, int z) { base.SetPosition(x, y); this.z = z; }
public void Move(int x, int y, int z) { base.Move(x, y); this.z += z; }

// override method
public override string ToString() { return (String.Format(“({0}, {1}, {2})”, x, y, z)); }
public override void Print() { Console.WriteLine(“X={0} Y={1} Z={2}”, x, y, z); }

public override void Sum(Point other)
{

if (other is Point3D)
Sum((Point3D)other);
else base.Sum(other);
}


// overload method
public void Sum(Point3D other) { x += other.X; y += other.Y; z += other.Z; }
// operator++ overload
public static Point3D operator++(Point3D p) { ++p.x; ++p.y; ++p.z; return p; }
// operator== overload
public static bool operator ==(Point3D p1, Point3D p2) { return p1.Equals(p2); }
// operator!= overload
public static bool operator !=(Point3D p1, Point3D p2) { return !p1.Equals(p2); }
public override int GetHashCode() { return x ^ y ^ z; }
public override bool Equals(object obj)
{

if (!(obj is Point3D))

return false;

return Equals((Point3D)obj);
}
public override bool Equals(Point other)
{
if (other is Point3D))return Equals((Point3D)other);

else

return base.Equals(other);

}

// IEquatable
public bool Equals(Point3D other)
{

if (object.ReferenceEquals(this, other))
return true;
return this.x == other.x && this.y == other.y && this.z == other.z;
}
// IComparable
public int CompareTo(Point3D other)
{
if (this == other)
{
return 0;
}
else if (this.x < other.x)
{
return -1;
}
else if ((this.x == other.x) && (this.y < other.y))
{

return -1;
}

else if ((this.x == other.x) && (this.y == other.y) && (this.z < other.z))
{

return -1;
}

else
{

return 1;
}
}

}

class PointTest
{
// pass-by-value (value type)
static void Swap(int p1, int p2)
{
int p;
p = p1;
p1 = p2;
p2 = p;
}
// pass-by-reference (value type)
static void Swap(ref int p1, ref int p2)
{
int p;
p = p1;
p1 = p2;
p2 = p;
}
// pass-by-value (reference type)
static void Swap(int[] p1, int[] p2)
{
int[] p;
p = p1;
p1 = p2;
p2 = p;
}
// pass-by-value (reference type)
static void Swap(ref int[] p1, ref int[] p2)
{
int[] p;
p = p1;
p1 = p2;
p2 = p;
}
static void Swap(ref Point p1, ref Point p2)
{
Point p;
p = p1;
p1 = p2;
p2 = p;
}
static void Swap(ref Point3D p1, ref Point3D p2)
{
Point3D p;
p = p1;
p1 = p2;
p2 = p;
}
static void Main(string[] args)
{
// value type: pass-by-value/pass-by-reference
int i = 5, j = 10;
Console.WriteLine(“Before: i={0}, j={1}”, i, j); // i=5, j=10
Swap(i, j);
Console.WriteLine(“After Swap(i,j): i={0}, j={1}”, i, j); // i=5, j=10
Swap(ref i, ref j);
Console.WriteLine(“After Swap(ref i, ref j): i={0}, j={1}”, i, j); // i=10, j=5
// reference type: pass-by-value/pass-by-reference
int[] arr1 = { 1, 2, 3 };
int[] arr2 = { 4, 5, 6, 7, 8 };
Console.WriteLine(“Before array pass-by-value swap: “);
Console.Write(“arr1: “);
foreach (int a in arr1) Console.Write(“{0} “, a); // arr1: 1 2 3
Console.WriteLine();
Console.Write(“arr2: “);
foreach (int a in arr2) Console.Write(“{0} “, a); // arr2: 4 5 6 7 8
Console.WriteLine();
Swap(arr1, arr2);
Console.WriteLine(“After Swap(arr1, arr2): “);
Console.Write(“arr1: “);
foreach (int a in arr1) Console.Write(“{0} “, a); // arr1: 1 2 3
Console.WriteLine();
Console.Write(“arr2: “);
foreach (int a in arr2) Console.Write(“{0} “, a); // arr2: 4 5 6 7 8
Console.WriteLine();
Swap(ref arr1, ref arr2);
Console.WriteLine(“After Swap(ref arra1, ref arr2): “);
Console.Write(“arr1: “);
foreach (int a in arr1) Console.Write(“{0} “, a); // arr1: 4 5 6 7 8
Console.WriteLine();
Console.Write(“arr2: “);
foreach (int a in arr2) Console.Write(“{0} “, a); // arr2: 1 2 3
Console.WriteLine();
// Point
Console.Write(“p1: “);
Point p1 = new Point();
p1.Print(); // Point.Print() (0, 0)
Console.Write(“p2: “);
Point p2 = new Point();
p2.Print(); // Point.Print() (0, 0)
p1.SetPosition(10, 10); // Point.SetPosition(int, int)
Console.Write(“p1 SetPosition: “);
p1.Print(); // Point.Print() (10, 10)
p1.Move(20, 50); // Point.Move(int, int)
Console.Write(“p1 Move: “);
Console.WriteLine(“p1=” + p1); // Point.ToString() (30, 60)
p2.SetPosition(20, 30); // Point.SetPosition(int, int)
Console.Write(“p2 SetPosition: “);
p2.Print(); // Point.Print() (20, 30)
//포인터 객체 교환함수 호출
Console.WriteLine(“\nSwap”);
Swap(ref p1, ref p2);
Console.WriteLine(“p1: {0}”, p1); // Point.ToString() (20, 30)
Console.WriteLine(“p2: {0}”, p2); // Point.ToString() (30, 60)
Console.WriteLine();
Console.WriteLine(“p1.m_nX={0}, p1.m_nY={1}, Point.GetCount()={2}”, p1.X, p1.Y, Point.Count);
Console.WriteLine(“p2.m_nX={0}, p2.m_nY={1}, Point.GetCount()={2}”, p2.X, p2.Y, Point.Count);
// Point3D
Console.Write(“p3: “);
Point3D p3 = new Point3D();
p3.Print(); // Point3D.Print() (0, 0, 0)
Console.Write(“p4: “);
Point3D p4 = new Point3D();
p4.Print(); // Point3D.Print() (0, 0, 0)
Console.WriteLine();
p3.SetPosition(10, 20); // Point3D.SetPosition(int,int)
Console.Write(“p3 SetPosition: “);
p3.Print(); // Point3D.Print() (10, 20, 0)
p3.SetPosition(30, 40, 50); // Point3D.SetPosition(int,int,int)
Console.Write(“p3 SetPosition: “);
p3.Print(); // Point3D.Print() (30, 40, 50)
++p3; // Point3D.operator++
Console.Write(“++p3: “);
p3.Print(); // Point3D.Print() (31, 41, 51)
p3.Move(20, 30, 40); // Point3D.Move(int,int,int)
Console.Write(“p3 Move: “);
p3.Print(); // Point3D.Print() (51, 71, 91)
p3.Move(10, 10); // Point3D.Move(int,int)
Console.Write(“p3 Move: “);
Console.WriteLine(p3); // Point3D.ToString() (61, 81, 91)
Console.WriteLine();
p4.SetPosition(100, 200, 300); // Point3D.SetPosition(int,int,int)
Console.Write(“p4 SetPosition: “);
p4.Print(); // Point3D.Print() (100, 200, 300)
p4.Move(10, 10, 10); // Point.Move(int, int,int)
Console.Write(“p4 Move: “);
p4.Print(); // Point3D.Print() (110, 210, 310)
//SUM
Console.WriteLine(“\nSUM”);
p3.Sum(p4); // Point3D.Sum(Point3D) (61+110, 81+210, 91+310)
p3.Print(); // Point3D.Print() (171, 291, 401)
//포인터 객체 교환함수 호출
Console.WriteLine(“\nSwap”);
Swap(ref p3, ref p4); // (171,291,401) <-> (110,210,310)
Console.WriteLine(“p1: {0}”, p3); // Point3D.ToString() (110,210,310)
Console.WriteLine(“p2: {0}”, p4); // Point3D.ToString() (171,291,401)
Console.WriteLine();
Console.WriteLine(“p3.m_nX={0}, p3.m_nY={1}, p3.m_nZ={2}, Point.GetCount()={3}”, p3.X, p3.Y, p3.Z, Point.GetCount());
Console.WriteLine(“p4.m_nX={0}, p4.m_nY={1}, p4.m_nZ={2}, Point.count={3}”, p4.X, p4.Y, p4.Z, Point.Count);
if (p3 == p4) Console.WriteLine(“p3 == p4”); // Point3D.operator==
if (p3 != p4) Console.WriteLine(“p3 != p4”); // Point3D.operator!=
Console.Write(“p5: “);
// polymorphism
Point p5 = p3; // upcasting – P5는 Point 타입인 Point3D 객체
p5.Print(); // Point3D.Print() (late binding) (110,210,310)
p5.SetPosition(10,20); // Point.SetPosition(int,int)
p5.Print(); // Point3D.Print() (late binding) (10,20,310)
((Point3D)p5).SetPosition(10,20,30); // downcasting – Point3D.SetPosition(int,int,int)

p5.Print(); // Point3D.Print() (late binding) (10,20,30)
Console.Write(“p6: “);
Point p6 = new Point3D(110, 210, 310); // upcasting – p6도 Point 타입인 Point3D 객체
p6.Print(); // Point3D.Print() (late binding) (110,210,310)
p5.Sum(p6); // Point3D.Sum(Point) (late binding) (10+110, 20+210, 30+310)
p5.Print(); // Point3D.Print() (late binding) (110,230,340)
p6.Print(); // Point3D.Print() (late binding) (110,210,310)
Point3D p7 = (Point3D)p5; // downcasting
Console.WriteLine(p7); // Point3D.ToString()
Point3D p8 = p5 as Point3D;
Console.WriteLine(p7); // Point3D.ToString()
if (p7 == p8) Console.WriteLine(“p7 == p8”); // p7 == p8
}
}

HW2

단국대학교 멀티미디어공학과 HCI프로그래밍2 (2016년 가을학기) 실습
과목코드 : 300890
강사 : 박경신
——————————————————————
날짜: 2016년 10월 10일
– 실습번호 : HW1 (Due by 10/24)
– 실습제목 : class, abstract class, inheritance, property, ArrayList
– 실습요약 : 입체 도형의 겉넓이(surface area)와 부피(volume) 구하기 & 평면 도형의 넓이(area) 구하기
– 준비자료 : HW1 http://www.mathsisfun.com/area-calculation-tool.html
http://math.about.com/od/formulas/ss/surfaceareavol.htm

10/20까지 online.dankook.ac.kr 이러닝으로 실행파일(bin\*.exe)과 소스코드(*.cs)와 보고서(*.doc/*.hwp)를 전부 “학번_이름_HW1.zip”으로 묶어서 제출한다. 또한, 비주얼 스튜디오에서 만든 프로젝트 전체 파일(*.sln, *.csproj)을 폴더에 같이 넣어준다. 보고서는 출력해서 수업시간에 제출한다.

– 실습문제
0. GeometryType과 FigureType을 아래와 같이 정의한다.
enum GeometryType { SPHERE=1, CONE=2, CYLINDER=3, RECTANGULAR_PRISM=4, SQUARE_PYRAMID=5, ISOSCELES_TRIANGULAR_PRISM=6 }
enum FigureType { TRIANGLE=1, SQUARE=2, RECTANGLE=3, PARALLELOGRAM=4, RHOMBUS=5, TRAPEZOID=6 }

1. Geometry와 Figure 추상클래스를 정의한다.
public abstract class Geometry {
public abstract GeometryType Type { get; } // 도형타입 (GeometryType)
public abstract double SurfaceArea { get; } // 겉넓이
public abstract double Volume { get; } // 부피
public abstract void GetAdditionalUserInput(); // 추가적인 사용자 입력
public void PrintInfo() {
System.Console.WriteLine(ToString() + “ S.A.=” + SurfaceArea + “ Vol=” + Volume);
}
public abstract class Figure {
public abstract FigureType Type { get; } // 도형타입 (FigureType)
public abstract double Area { get; } // 넓이
public abstract void GetAdditionalUserInput(); // 추가적인 사용자 입력
public void PrintInfo() {
System.Console.WriteLine(ToString() + “Area=” + Area);
}

2. Geometry 추상클래스를 상속받은 Sphere, Cone, Cylinder, … 는 겉넓이(Surface Area), 부피(Volume)를 계산하여 출력한다. 그리고 Figure 추상클래스를 상속받은 Triangle, Square, Rectangle, … 등 클래스는 넓이(Area)를 계산하여 출력한다.

3. GeometryFactory 클래스와 FigureFactory 클래스는 사용자가 입력한 도형타입에 따라 원하는 실제 클래스 (즉, Sphere, Cone, … 그리고 Triangle, Square,.. 등등) 객체를 생성하여 계산한다. 이 클래스는 다음의 메소드 (Method)만을 갖는다.
+public static Geometry GetInstance(GeometryType type)
+public static Figure GetInstance(FigureType type)

4. GeometryCalculator 클래스와 FigureCalculator 클래스는 계산 메소드를 갖는다
+ public static void CalculateAll() // ArrayList에 Geometry/Figure 객체를 생성하여 넣고, 각각의 SurfaceArea&Volume/Area를 계산한다.
+ public static void CalculateByUserInput() // 사용자 입력으로 원하는 Geometry/Figure 객체를 생성하여 SurfaceArea&Volume/Area를 계산하고, ArrayList에 저장해 두었다가 프로그램을 종료할 시 전체 리스트를 출력한다.

5. Utility 클래스에서는 각종 유틸리티 메소드를 갖는다.
+public static GeometryType GetUserGeometry()
+public static FigureType GetUserFigure()
+public static double GetUserInputDouble()
+public static int GetUserInputBetween(int min, int max)
+public static bool GetUserExitKey()

6. GeoFigCalculator 클래스에서는 사용자의 입력에 따라 GeometryCalculator와 FigureCalculator를 이용하여 계산한 모든 결과를 출력한다.

7. 사용자의 잘못된 입력에 따른 처리를 반드시 포함해야 하며, 그 외에 본인이 더 테스트해보고 싶은 method나 routine을 추가하라. 실행 화면과 코드를 첨부하시오.