Microsoft PowerPoint - hci2-lecture4 [호환 모드]

Similar documents
Microsoft PowerPoint - hci2-lecture4.ppt [호환 모드]

PowerPoint 프레젠테이션

JAVA 프로그래밍실습 실습 1) 실습목표 - 메소드개념이해하기 - 매개변수이해하기 - 새메소드만들기 - Math 클래스의기존메소드이용하기 ( ) 문제 - 직사각형모양의땅이있다. 이땅의둘레, 면적과대각

슬라이드 1

PowerPoint Presentation

JAVA PROGRAMMING 실습 02. 표준 입출력

학습목차 2.1 다차원배열이란 차원배열의주소와값의참조

<322EBCF8C8AF28BFACBDC0B9AEC1A6292E687770>

목차 포인터의개요 배열과포인터 포인터의구조 실무응용예제 C 2

Microsoft PowerPoint - chap02-C프로그램시작하기.pptx

PowerPoint Presentation

[ 마이크로프로세서 1] 2 주차 3 차시. 포인터와구조체 2 주차 3 차시포인터와구조체 학습목표 1. C 언어에서가장어려운포인터와구조체를설명할수있다. 2. Call By Value 와 Call By Reference 를구분할수있다. 학습내용 1 : 함수 (Functi

Microsoft PowerPoint - chap10-함수의활용.pptx

Microsoft PowerPoint 자바-기본문법(Ch2).pptx

PowerPoint Presentation

11장 포인터

Microsoft PowerPoint - CSharp-2-기초문법

C# Programming Guide - Types

(8) getpi() 함수는정적함수이므로 main() 에서호출할수있다. (9) class Circle private double radius; static final double PI= ; // PI 이름으로 로초기화된정적상수 public

Microsoft PowerPoint - ch07 - 포인터 pm0415

A Dynamic Grid Services Deployment Mechanism for On-Demand Resource Provisioning

Microsoft PowerPoint - chap06-1Array.ppt

JAVA PROGRAMMING 실습 08.다형성

금오공대 컴퓨터공학전공 강의자료

PowerPoint Presentation

adfasdfasfdasfasfadf

Design Issues

JUNIT 실습및발표

Microsoft PowerPoint - additional01.ppt [호환 모드]

<4D F736F F F696E74202D20C1A63038C0E520C5ACB7A1BDBABFCD20B0B4C3BC4928B0ADC0C729205BC8A3C8AF20B8F0B5E55D>

설계란 무엇인가?

JVM 메모리구조

PowerPoint 프레젠테이션

PowerPoint Template

설계란 무엇인가?

PowerPoint Presentation

슬라이드 1

PowerPoint Presentation

chap10.PDF

PowerPoint 프레젠테이션

10.0pt1height.7depth.3width±â10.0pt1height.7depth.3widthÃÊ10.0pt1height.7depth.3widthÅë10.0pt1height.7depth.3width°è10.0pt1height.7depth.3widthÇÁ10.0pt1height.7depth.3width·Î10.0pt1height.7depth.3width±×10.0pt1height.7depth.3width·¡10.0pt1height.7depth.3width¹Ö pt1height.7depth.3widthŬ10.0pt1height.7depth.3width·¡10.0pt1height.7depth.3width½º, 10.0pt1height.7depth.3width°´10.0pt1height.7depth.3widthü, 10.0pt1height.7depth.3widthº¯10.0pt1height.7depth.3width¼ö, 10.0pt1height.7depth.3width¸Þ10.0pt1height.7depth.3width¼Ò10.0pt1height.7depth.3widthµå

Microsoft PowerPoint - C++ 5 .pptx

Microsoft PowerPoint - chap06-5 [호환 모드]

PowerPoint Presentation

JAVA PROGRAMMING 실습 05. 객체의 활용

목차 배열의개요 배열사용하기 다차원배열 배열을이용한문자열다루기 실무응용예제 C 2

슬라이드 1

chap 5: Trees

Microsoft PowerPoint - 2강

슬라이드 1

Microsoft PowerPoint - Chapter 6.ppt

Microsoft PowerPoint - chap06-2pointer.ppt

제 11 장포인터 유준범 (JUNBEOM YOO) Ver 본강의자료는생능출판사의 PPT 강의자료 를기반으로제작되었습니다.

PowerPoint Presentation

Microsoft PowerPoint 장강의노트.ppt

OCW_C언어 기초

쉽게 풀어쓴 C 프로그래밍

chap x: G입력

Microsoft PowerPoint - chap11-포인터의활용.pptx

PowerPoint 프레젠테이션

구조체정의 자료형 (data types) 기본자료형 (primitive data types) : char, int, float 등과같이 C 언어에서제공하는자료형. 사용자정의자료형 (user-defined data types) : 다양한자료형을묶어서목적에따라새로운자료형을

q 이장에서다룰내용 1 객체지향프로그래밍의이해 2 객체지향언어 : 자바 2

02장.배열과 클래스

Microsoft PowerPoint - chap-11.pptx

Microsoft PowerPoint - 8ÀÏ°_Æ÷ÀÎÅÍ.ppt

Microsoft PowerPoint - 3ÀÏ°_º¯¼ö¿Í »ó¼ö.ppt

선형대수학 Linear Algebra

Frama-C/JESSIS 사용법 소개

KNK_C_05_Pointers_Arrays_structures_summary_v02

PowerPoint Presentation

학습목표 함수프로시저, 서브프로시저의의미를안다. 매개변수전달방식을학습한다. 함수를이용한프로그래밍한다. 2

Microsoft PowerPoint - a10.ppt [호환 모드]

Microsoft Word - FunctionCall

임베디드시스템설계강의자료 6 system call 2/2 (2014 년도 1 학기 ) 김영진 아주대학교전자공학과

Slide 1

쉽게 풀어쓴 C 프로그래밍

Microsoft PowerPoint - 07-C#-2-기초문법.ppt [호환 모드]

(Microsoft PowerPoint - java1-lecture11.ppt [\310\243\310\257 \270\360\265\345])

교육자료

쉽게 풀어쓴 C 프로그래밍

PowerPoint Template

JAVA PROGRAMMING 실습 02. 표준 입출력

예제 1.1 ( 관계연산자 ) >> A=1:9, B=9-A A = B = >> tf = A>4 % 4 보다큰 A 의원소들을찾을경우 tf = >> tf = (A==B) % A

<4D F736F F F696E74202D2036C0CFC2B05FB0B4C3BCC1F6C7E2C7C1B7CEB1D7B7A1B9D62E707074>

C++ Programming

JAVA PROGRAMMING 실습 09. 예외처리

PowerPoint 프레젠테이션

C 언어 프로그래밊 과제 풀이

Microsoft PowerPoint - CSharp-10-예외처리

PowerPoint 프레젠테이션

Data Structure

윤성우의 열혈 TCP/IP 소켓 프로그래밍

Microsoft Word - java19-1-midterm-answer.doc

Microsoft PowerPoint - 제11장 포인터(강의)

Cluster management software

untitled

기초컴퓨터프로그래밍

Microsoft PowerPoint - ch10 - 이진트리, AVL 트리, 트리 응용 pm0600

C 프로그래밊 개요

(Microsoft PowerPoint - 07\300\345.ppt [\310\243\310\257 \270\360\265\345])

Transcription:

Overview 메소드와인자 & 배열 321190 2012 년가을학기 9/19/2012 박경신 효과적인메소드호출의방법 중첩메소드와재귀메소드 메소드와변수와의관계 메소드에인자를전달하여호출 메소드오버로딩 배열의의미와필요성 1 차원배열, 다차원배열 배열의요소에접근하는방법에대한이해 배열에서지원하는속성과메소드활용 Class/Method C# FCL 에는많은클래스들이정의되어있음 - 예를들어, Console MessageBox Int32 Math 클래스의정의는메소드 (Method) 와자료속성 (Data Properties) 를포함 Method 예 -Console.Write(), Console.WriteLine(), Int32.Parse() Property 예 Int32.MinValue, Int32.MaxValue, Math.PI, Math.E Method 메소드 (method) 란정해진작업을수행하기위해그룹으로묶어, 이름을붙인명령문의집합 접근지정자리턴형함수이름 ( 인자리스트 ) 명령 1; 명령 2; return 리턴값 ; Console.WriteLine("Hello?"); class method (parameters) dot

Method Examples: Math Class Method Description Example Abs( x ) absolute value of x Abs( 23.7 ) is 23.7 Abs( -23.7 ) is 23.7 Ceiling( x ) rounds x to the smallest integer not less than x Ceiling( 9.2 ) is 10.0 Ceiling( -9.8 ) is -9.0 Cos( x ) trigonometric cosine of x (x in radians) Cos( 0.00 ) is 1.0 Exp( x ) exponential method ex Exp( 1.0 ) is approximately 2.7182818284590451 Exp( 2.0 ) is approximately 7.3890560989306504 Floor( x ) rounds x to the largest integer not greater than x Floor( 9.2 ) is 9.0 Floor( -9.8 ) is -10.0 Log( x ) natural logarithm of x (base e) Log( 2.7182818284590451 ) is approximately 1.0 Log( 7.3890560989306504 ) is approximately 2.0 Max( x, y ) larger value of x and y (also has versions for float, int and long values) Max( 2.3, 12.7 ) is 12.7 Max( -2.3, -12.7 ) is -2.3 Min( x, y ) smaller value of x and y (also has versions for float, int and long values) Min( 2.3, 12.7 ) is 2.3 Min( -2.3, -12.7 ) is -12.7 Pow( x, y ) x raised to power y (xy) Pow( 2.0, 7.0 ) is 128.0 Pow( 9.0,.5 ) is 3.0 Sin( x ) trigonometric sine of x (x in radians) Sin( 0.0 ) is 0.0 Sqrt( x ) square root of x Sqrt( 900.00 ) is 30.00 Sqrt( 9.0 ) is 3.0 Tan( x ) trigonometric tangent of x (x in radians) Tan( 0.0 ) is 0.0 Method 정의와호출 접근지정자 함수를호출할수있는범위를지정한다. 반환형 함수가돌려주는값의형식을지정한다. 함수이름 함수를호출할때사용하는함수의이름 인자리스트 인자리스트란함수에게실행전에전달되는변수를의미하며, 변수의형식과실제값이전달된다. 명령문 실제함수가실행하는문장 반환값 함수가종료될때되돌려주는값으로위에선언한리턴형에맞는형식의값이어야한다. 리턴값은없을수도있으며이때의리턴형은 void 로선언한다 Method Declaration 메소드선언 class MyClass return properties type method parameter list name static int SquareSum( int num1, int num2 ) int sum = num1 + num2; return sum * sum; Method Call static method ( 정적메소드 ) 호출 같은클래스내에서의 static method 호출 : 메소드명 (); 다른클래스내에서의 static method 호출 : 클래스명. 메소드명 (); 7 8

Method Call instance method ( 인스턴스메소드 ) 호출 같은클래스내에서의 instance method 호출 : 메소드명 (); 다른클래스내에서의 instance method 호출 : 인스턴스명. 메소드명 (); 9 Method 정의와호출 메소드호출예제 MethodA() in class A namespace MethodExample MethodC() in class A public class A 콘솔창에출력 public void MethodC() Console.WriteLine("MethodC() in class A"); public static void MethodA() Console.WriteLine("MethodA() in class A"); public class B public A.MethodA(); A a = new A(); a.methodc(); Nested Method 메소드를호출할경우, 호출된메소드내에서또다른메소드를계속호출해서사용하는메소드 중첩메소드를사용할때는메소드끼리서로를계속호출하여, 프로그램이종료되지않는무한루프에빠지지않도록주의할것 11 Nested Method 중첩메소스예제 using System; namespace NestedMethodExample MethodA. MethodB. MethodA. MethodA. public class NestedMethod public static void MethodA() Console.WriteLine( MethodA."); public static void MethodB() MethodA(); Console.WriteLine( MethodB."); MethodA(); public MethodB(); MethodA();

Recursive Call 재귀메소드 자기자신을호출하는메소드로서같은반복된작업이필요한경우를구현한메소드 ( 예제 ) 재귀메소드를이용하여 N!(factorial) 구하기 N- 팩토리얼 = N * (N-1) * (N-2) * (N-3) *. * 1 Nested Method 재귀메소스예제 using System; namespace RecursiveCallExample public class RecursiveCall public static ulong Factorial(ulong number) if (number <= 1) return 1; else return number * Factorial(number 1); public ulong nfact = Factorial(5); Console.WriteLine( 5 * 4 * 3 * 2 * 1 = + nfact); 5 * 4 * 3 * 2 * 1 = 120 Local Variables 지역변수란메소스내에서선언된변수 메소드가실행될때변수를저장하기위한메모리가생성 선언된메소드내부에서만사용이가능 메소드의실행이종료될때메모리가해제 변수를선언한후, 초기값을부여하는초기화가반드시필요함 Class Variables 클래스변수란클래스내에하나만존재하며, 클래스내의모든개체가사용할수있는변수 클래스가로딩될때변수를저장하기위한메모리를생성 클래스이름을이용하여접근함 모든클래스내의인스턴스 ( 개체 ) 가공유함 값을지정하지않은경우에자동으로 0으로초기화함 15 16

Variables 지역변수 (local variable) 과클래스변수 (class variable 또는 static variable) class MyClass public static int value = 10; public static int SquareSum( int num1, int num2 ) int sum = num1 + num2; // 지역변수 sum은초기화해서사용해야함 return sum * sum; class Program public int a = 0; // 지역변수 a는초기화해서사용해야함 a = MyClass.SquareSum(2, 3); Console.WriteLine( SquareSum = + a); a += MyClass.value; // 클래스변수 value는클래스명. 변수명으로사용 Console.WriteLine( a = + a); 17 배열 (array) 이란같은형식의데이터를그룹화해서사용하거나편집할때유용하게사용할수있도록하는데이터타입 System. 라는클래스에서파생되었으며배열 (array) 의모든 element 는형 (type) 이같아야함 구조체 (structure) 란서로연관성이있는데이터이지만형식 (type) 이다른경우의그룹화에사용 배열 (array) 는서로연관성이있고형식도같은데이터를그룹화할때사용 배열의특징 같은데이터형의변수를한꺼번에여러개생성 배열의크기는배열의첨자로결정 첨자에해당하는만큼의같은데이터형을가진메모리생성 배열의메모리는연속적으로지정 배열의참조값을이용하여핸들할수있음 배열의이름은연속된변수들을참조하기위한참조값 배열의요소는변수 배열선언 type [ ] name; // 선언 type 배열을실제로구성하는요소의형식 (type) 을나타냄 [] 배열의차원 (rank) 를나타냄 name 배열변수의이름을나타남 int[] my; // 초기화 my = new int[5];

배열의초기화 타입 숫자 (int,long,float) 등 0 초기값 문자 (char) Null( 빈값을의미 ) 문자열 (string) Null( 빈값을의미 ) enum 0 참조형 (reference) Null( 빈값을의미 ) 배열의요소값지정하는방법 int[] my = new int[5]0,1,2,3,4; int[] my = 01234 0,1,2,3,4; int[] my; my = new int[5]0,1,2,3,4; 0 1 2 3 4 int[] my; my = new int[5]; my ( 배열명 ) 0 0 0 0 0 my ( 배열명 ) 배열의초기화후의모습 int형의 5개의요소를가진배열 my가정상적으로초기화가되면, 메모리에배열이저장되는데, 배열명은 Stack에, 각요소들의값은 Heap 에저장이됨 다차원 다차원배열선언 // 2 차원배열의예 int[,] [] my = new int [2,5] 0,1,2,3,4, 5,6,7,8,9 ; 0 1 2 3 4 Stack 영역 Heap 영역 my ( 배열명 ) 5 6 7 8 9

다차원 다차원배열선언시의에러예제 int[,] my = new int[2,5] 0,1,2,, 56789 5,6,7,8,9 ; X 에러발생 0 1 2 X X 다차원 2차원배열의크기 int[,] my = new int[rows, columns]; Rows * columns 만큼의배열크기가생성됨 실제사용할배열의크기보다훨씬큰배열을선언하고사용한다면, 메모리낭비임 반대로사용할배열보다선언한배열의크기가작다면배열의크기가가변적이지않기때문에다시다른변수명으로배열을새로선언하고사용해야함 0 0 0 column my ( 배열명 ) 5 6 7 8 9 my ( 배열명 ) 0 0 0 row 불규칙적 다차원배열 vs. 불규칙적배열 다차원배열의경우는반드시값을채워줘야함 불규칙적인배열 (Jagged, a.k.a of s) 은 [][] 형태로해주어야함 int[,] my = new int[2,5] 012 0,1,2,, // 에러발생 5,6,7,8,9 ; int[][] my =newint[2][]; my[0] = new int[] 0,1,2,; my[1] = new int[] 5,6,7,8,9; 의복사 배열의복사 class Copy static void Main (string [] args ) long [] Original = new long[4] 2, 1, 4, 5; long [] Copy = Original; Console.WriteLine( 1 : + Copy[3]); // 1 : 5 ; Original[3] = Original[0] + Original[2]; Console.WriteLine( 2 : + CopyValue); // 2 : 6

의복사 배열의복사 배열의복사가이루어지면, 복사된배열명은새로배열을생성하는것이아니라, 원래있는배열의값들을참조하고있는것 그렇기때문에원래의배열의요소값에변화가생기면당연히복사된배열에서도바뀐값을참조하는것임 Oi Originali Copy 2 1 4 5 의활용 배열의차원 (rank) class Test int[] 1 = new int[4]; int[,] 2 = new int[2,3]; int[,,] 3 = new int[2,4,3]; // 배열의차원은배열선언시의각요소의갯수 // 1 의차원 :1 Console.WriteLine("1의차원 : " + 1.Rank); // 2의차원 : 2 Console.WriteLine("2 의차원 : " + 2.Rank); // 3의차원 : 3 Console.WriteLine("3의차원 : " + 3.Rank); 의활용 배열의크기 (length) class Test int[] 1 = new int[4]; int[,] 2 = new int[2,3]; int[,,] 3 = new int[2,4,3]; // 배열의크기는배열의각요소크기의곱셈 // 1 의크기 :4 Console.WriteLine("1의크기 : " + 1.Length); // 2의크기 : 6 Console.WriteLine("2 의크기 : " + 2.Length); // 3의크기 : 24 Console.WriteLine("3의크기 : " + 3.Length); 의활용 배열의인덱스 (index) 배열명 [index] 라고쓰면그배열의 index 순서에있는 element를뜻하며, 첫번째 element 의 index는 0 임 배열의유효 index 범위를넘는인덱스를사용하면 IndexOutOfRangeException 예외가발생함 class Test class Test int[] 1 = new int[4] 1, 2, 3, 4; for (int i = 0; i < 1.Length; i++) Console.WriteLine("1[0]=1", i, 1[i]);

의활용 정렬 (Sort) 메소드 System..Sort() 정렬메소드는배열의요소값들을크기의순서대로작은순서부터큰순서대로정렬을해주고이를배열에반영해주는메소드 초기화 (Clear) 메소드 System..Clear() 배열의각요소들의값을초기화하는메소드 복제 (Clone) 메소드 System..Clone() 배열의크기와요소값을모두같게하여새로운배열생성하는메소드 색인 (IndexOf) 메소드 System..IndexOf() 찾으려는값이배열의몇번째요소인지를반환하는메소드 의활용 복제 (Clone), 색인 (IndexOf), 정렬 (Sort) 메소드 int[] one = new int[] 2, 1, 4, 5; // one 2, 1, 4, 5 int[] clone = (int[])one.clone(); // clone 2, 1, 4, 5 one[3] = one[0] + one[2]; // one 2, 1, 4, 6 clone 2, 1, 4, 5 foreach(int i in clone) Console.Write("0 ", i); //2145 int where =.IndexOf(clone, 4); // 4 is located in 2 Console.WriteLine("\n4 is located in 0, where);.sort(clone); Console.Write( After sort: "); foreach(int i in clone) Console.Write("0 ", i); //Aftersort:1245 의활용 메소드의리턴값으로의배열 class Return int[] My = CreateInt(10); // My 의크기 :10 Console.WriteLine("My의크기 : " + My.Length); static int[] CreateInt(int size) int[] int = new int[size]; return int; 의활용 메소드의인자로서의배열 class Param int[] My = 2,6,5,4,1; MyMethod(My); // 3, 7, 6, 5, 2 for(int i=0;i<my.length;i++) Console.WriteLine (My[i]); static void MyMethod(int[] parameter) for(int j=0;j<parameter.length;j++) parameter[j]++;

의예제 int[] 1 = new int[4] 2, 1, 4, 5; // one-dimensional array for (int i =0;i < 1.Length; i++) Console.WriteLine("1[0]=1", i, 1[i]); 2 1 4 5 int[,] 2 = new int[2,3]; // two-dimensional array int a = 1; for (int i = 0; i < 2.GetLength(0); i++) for (int j = 0; j < 2.GetLength(1); j++) 1 2 3 2[i, j] = a++; 4 5 7 2[1, 2] = 7; int[,,] 3 = new int[2, 4, 3] // three-dimensional array 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 1 2 3 4 5 ; 1 2 3 6 7 8 9 for (int i = 0; i < 3.GetLength(0); i++) 4 5 6 10 11 120 for (int j = 0; j < 3.GetLength(1); j++) 7 8 9 for (int k = 0; k < 3.GetLength(2); 3G k++) 10 11 12 Console.WriteLine("3[0,1,2]=3", i, j, k, 3[i, j, k]); 의예제 int[][] 4 = new int[2][]; // jagged array 4[0] = new int[] 0, 1, 2 ; 4[1] = new int[] 5, 6, 7, 8, 9 ; 0 1 2 5 6 7 8 9 for (int i = 0; i < 4.GetLength(0); i++) for (int j = 0; j < 4[i].GetLength(0); j++) Console.WriteLine("4[0][1]=2", i, j, 4[i][j]); int[][] 5 = new int[][] // jagged array new int[] 1, 2, new int[] 3, 4, 5, new int[] 6, 7, 8, 9, new int[] 10, 11, 12 ; 1 2 3 4 5 6 7 8 9 10 11 12 for (int i = 0; i < 5.Length; i++) for (int j = 0; j < 5[i].Length; j++) Console.WriteLine("5[0][1]=2", 2" i, j, 5[i][j]);