nyximos.log

[Java] ์ œ์–ด๋ฌธ ๋ณธ๋ฌธ

Programming/JAVA

[Java] ์ œ์–ด๋ฌธ

nyximos 2022. 8. 4. 00:59

๐Ÿ‘ฉ‍๐Ÿš€ ๋ชฉํ‘œ

์ž๋ฐ”๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์ œ์–ด๋ฌธ์„ ํ•™์Šตํ•˜์„ธ์š”.

๐Ÿ‘ฉ‍๐Ÿ’ป ํ•™์Šตํ•  ๊ฒƒ

  • ์„ ํƒ๋ฌธ
  • ๋ฐ˜๋ณต๋ฌธ

๐Ÿ‘ฉ‍๐Ÿ”ฌ ๊ณผ์ œ

๊ณผ์ œ 0. JUnit 5 ํ•™์Šตํ•˜์„ธ์š”.

์ธํ…”๋ฆฌJ, ์ดํด๋ฆฝ์Šค, VS Code์—์„œ JUnit 5๋กœ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์— ์ต์ˆ™ํ•ด ์งˆ ๊ฒƒ.

์ด๋ฏธ JUnit ์•Œ๊ณ  ๊ณ„์‹ ๋ถ„๋“ค์€ ๋‹ค๋ฅธ ๊ฒƒ ์•„๋ฌด๊ฑฐ๋‚˜!

๋” ์ž๋ฐ”, ํ…Œ์ŠคํŠธ ๊ฐ•์˜๋„ ์žˆ์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜์„ธ์š”~

๊ณผ์ œ 1. live-study ๋Œ€์‹œ ๋ณด๋“œ๋ฅผ ๋งŒ๋“œ๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.

๊นƒํ—™ ์ด์Šˆ 1๋ฒˆ๋ถ€ํ„ฐ 18๋ฒˆ๊นŒ์ง€ ๋Œ“๊ธ€์„ ์ˆœํšŒํ•˜๋ฉฐ ๋Œ“๊ธ€์„ ๋‚จ๊ธด ์‚ฌ์šฉ์ž๋ฅผ ์ฒดํฌ ํ•  ๊ฒƒ.

์ฐธ์—ฌ์œจ์„ ๊ณ„์‚ฐํ•˜์„ธ์š”. ์ด 18ํšŒ์— ์ค‘์— ๋ช‡ %๋ฅผ ์ฐธ์—ฌํ–ˆ๋Š”์ง€ ์†Œ์ˆซ์  ๋‘์ž๋ฆฌ๊ฐ€์ง€ ๋ณด์—ฌ์ค„ ๊ฒƒ.

Github ์ž๋ฐ” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

๊นƒํ—™ API๋ฅผ ์ต๋ช…์œผ๋กœ ํ˜ธ์ถœํ•˜๋Š”๋ฐ ์ œํ•œ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ณธ์ธ์˜ ๊นƒํ—™ ํ”„๋กœ์ ํŠธ์— ์ด์Šˆ๋ฅผ ๋งŒ๋“ค๊ณ  ํ…Œ์ŠคํŠธ๋ฅผ ํ•˜์‹œ๋ฉด ๋” ์ž์ฃผ ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ณผ์ œ 2. LinkedList๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”.

LinkedList์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•˜์„ธ์š”.

์ •์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ListNode ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”.

ListNode add(ListNode head, ListNode nodeToAdd, int position)๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”.

ListNode remove(ListNode head, int positionToRemove)๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”.

boolean contains(ListNode head, ListNode nodeTocheck)๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”.

๊ณผ์ œ 3. Stack์„ ๊ตฌํ˜„ํ•˜์„ธ์š”.

int ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•ด์„œ ์ •์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” Stack์„ ๊ตฌํ˜„ํ•˜์„ธ์š”.

void push(int data)๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”.

int pop()์„ ๊ตฌํ˜„ํ•˜์„ธ์š”.

๊ณผ์ œ 4. ์•ž์„œ ๋งŒ๋“  ListNode๋ฅผ ์‚ฌ์šฉํ•ด์„œ Stack์„ ๊ตฌํ˜„ํ•˜์„ธ์š”.

ListNode head๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ListNodeStack ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”.

void push(int data)๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”.

int pop()์„ ๊ตฌํ˜„ํ•˜์„ธ์š”.

๊ณผ์ œ 5. Queue๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”.

๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•ด์„œ ํ•œ๋ฒˆ

ListNode๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•œ๋ฒˆ.

 

 

 

๐Ÿง—‍โ™€๏ธ ์„ ํƒ๋ฌธ (์กฐ๊ฑด๋ฌธ)

์กฐ๊ฑด์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๋ฌธ์žฅ์ด ์ˆ˜ํ–‰๋˜๋„๋ก ํ•œ๋‹ค.

if ๋ฌธ

์กฐ๊ฑด์‹๊ณผ ๊ด„ํ˜ธ { } ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

'๋งŒ์ผ(if) ์กฐ๊ฑด์‹์ด ์ฐธ true ๋ผ๋ฉด ๊ด„ํ˜ธ { } ์•ˆ์˜ ๋ฌธ์žฅ๋“ค์„ ์ˆ˜ํ–‰ํ•˜๋ผ' ๋ผ๋Š” ์˜๋ฏธ

if (์กฐ๊ฑด์‹) {
	// ์กฐ๊ฑด์‹์ด ์ฐธ true ์ผ ๋•Œ ์ˆ˜ํ–‰๋  ๋ฌธ์žฅ๋“ค
}

 

 

๋ธ”๋Ÿญ { }

๊ด„ํ˜ธ  { } ๋ฅผ ์ด์šฉํ•ด์„œ ์—ฌ๋Ÿฌ ๋ฌธ์žฅ์„ ํ•˜๋‚˜์˜ ๋‹จ์œ„๋กœ ๋ฌถ์„ ์ˆ˜ ์žˆ๋‹ค.

๋ธ”๋Ÿญ ๋‚ด์˜ ๋ฌธ์žฅ๋“ค์€ tab์œผ๋กœ ๋“ค์—ฌ์“ฐ๊ธฐ๋ฅผ ํ•ด์„œ ๋ธ”๋Ÿญ ์•ˆ์— ์†ํ•œ ๋ฌธ์žฅ์ด๋ผ๋Š” ๊ฒƒ์„ ์•Œ๊ธฐ ์‰ฝ๊ฒŒ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

๋ธ”๋Ÿญ ๋‚ด์˜ ๋ฌธ์žฅ์ด ํ•˜๋‚˜๋ฟ์ผ ๊ฒฝ์šฐ ๊ด„ํ˜ธ๋ฅผ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ๊ฐ€๋Šฅํ•˜๋ฉด ์ƒ๋žตํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

์ƒˆ๋กœ์šด ๋ฌธ์žฅ์„ ์ถ”๊ฐ€ํ•  ๊ฒฝ์šฐ ๊ด„ํ˜ธ๋ฅผ ์žŠ๊ธฐ ์‰ฝ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

 

if  - else ๋ฌธ

if ๋ฌธ์— else ๋ธ”๋Ÿญ์ด ์ถ”๊ฐ€ ๋œ๋‹ค.

์กฐ๊ฑด์‹์˜ ๊ฒฐ๊ณผ๊ฐ€ ์ฐธ์ด ์•„๋‹ ๋•Œ else ๋ธ”๋Ÿญ์˜ ๋ฌธ์žฅ์„ ์ˆ˜ํ–‰ํ•˜๋ผ๋Š” ๋œป

if (์กฐ๊ฑด์‹) {

	// ์กฐ๊ฑด์‹์ด ์ฐธ true ์ผ ๋•Œ ์ˆ˜ํ–‰ํ•  ๋ฌธ์žฅ๋“ค
    
} else {

	// ์กฐ๊ฑด์‹์ด ๊ฑฐ์ง“ false ์ผ ๋•Œ ์ˆ˜ํ–‰๋  ๋ฌธ์žฅ๋“ค
    
}

 

 

if - else if ๋ฌธ 

์ฒ˜๋ฆฌํ•ด์•ผ ํ•  ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ์…‹ ์ด์ƒ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค.

 

if (์กฐ๊ฑด์‹1) {

	// ์กฐ๊ฑด์‹1์˜ ์—ฐ์‚ฐ๊ฒฐ๊ณผ๊ฐ€ ์ฐธ์ผ ๋•Œ ์ˆ˜ํ–‰๋  ๋ฌธ์žฅ๋“ค
    
} else if (์กฐ๊ฑด์‹2) {

	// ์กฐ๊ฑด์‹2์˜ ์—ฐ์‚ฐ๊ฒฐ๊ณผ๊ฐ€ ์ฐธ์ผ ๋•Œ ์ˆ˜ํ–‰๋  ๋ฌธ์žฅ๋“ค
    
} else if (์กฐ๊ฑด์‹3) {

	// ์กฐ๊ฑด์‹3์˜ ์—ฐ์‚ฐ๊ฒฐ๊ณผ๊ฐ€ ์ฐธ์ผ ๋•Œ ์ˆ˜ํ–‰๋  ๋ฌธ์žฅ๋“ค
    
} else {

	// ๋งˆ์ง€๋ง‰์€ ๋ณดํ†ต else๋ธ”๋Ÿญ์œผ๋กœ ๋๋‚˜๋ฉฐ ์ƒ๋žต ๊ฐ€๋Šฅํ•˜๋‹ค.
	// ์œ„์˜ ์–ด๋–ค ์กฐ๊ฑด์‹๋„ ๋งŒ์กฑํ•˜์ง€ ์•Š์„ ๋•Œ ์ˆ˜ํ–‰๋  ๋ฌธ์žฅ๋“ค
    
}

 

 

์ค‘์ฒฉ if ๋ฌธ

if ๋ฌธ ๋ธ”๋Ÿญ ๋‚ด์— ๋˜ ๋‹ค๋ฅธ if ๋ฌธ์„ ํฌํ•จ์‹œํ‚จ ๋ฌธ์žฅ

๊ด„ํ˜ธ์™€ ๋“ค์—ฌ์“ฐ๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‘ if ๋ฌธ์˜ ๋ฒ”์œ„๊ฐ€ ๋ช…ํ™•ํžˆ ๊ตฌ๋ถ„๋  ์ˆ˜ ์žˆ๋„๋ก ์ž‘์„ฑํ•œ๋‹ค.

if (์กฐ๊ฑด์‹1) {

	// ์กฐ๊ฑด์‹1์˜ ์—ฐ์‚ฐ๊ฒฐ๊ณผ๊ฐ€ true์ผ ๋•Œ ์ˆ˜ํ–‰๋  ๋ฌธ์žฅ๋“ค
    
    if (์กฐ๊ฑด์‹2) {
    
    	// ์กฐ๊ฑด์‹1๊ณผ ์กฐ๊ฑด์‹2๊ฐ€ ๋ชจ๋‘ true์ผ ๋•Œ ์ˆ˜ํ–‰๋  ๋ฌธ์žฅ๋“ค
        
    } else {
    
    	// ์กฐ๊ฑด์‹1์ด true์ด๊ณ , ์กฐ๊ฑด์‹2๊ฐ€ false์ผ ๋•Œ ์ˆ˜ํ–‰๋˜๋Š” ๋ฌธ์žฅ๋“ค
        
    }
} else {

	// ์กฐ๊ฑด์‹1์ด false์ผ ๋•Œ ์ˆ˜ํ–‰๋˜๋Š” ๋ฌธ์žฅ๋“ค
    
}

 

 

switch ๋ฌธ

ํ•˜๋‚˜์˜ ์กฐ๊ฑด์‹์œผ๋กœ ๋งŽ์€ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ณ  ํ‘œํ˜„์ด ๊ฐ„๊ฒฐํ•˜๋‹ค.

 

1. ์กฐ๊ฑด์‹์„ ๊ณ„์‚ฐํ•œ๋‹ค.

2. ์กฐ๊ฑด์‹์˜ ๊ฒฐ๊ณผ์™€ ์ผ์น˜ํ•˜๋Š” case๋ฌธ์œผ๋กœ ์ด๋™ํ•œ๋‹ค.

3. ์ดํ›„์˜ ๋ฌธ์žฅ๋“ค์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

4. break๋ฌธ์ด๋‚˜ switch๋ฌธ์˜ ๋์„ ๋งŒ๋‚˜๋ฉด switch๋ฌธ ์ „์ฒด๋ฅผ ๋น ์ ธ๋‚˜๊ฐ„๋‹ค.

switch (์กฐ๊ฑด์‹) {

    case ๊ฐ’1 : 
    	// ์กฐ๊ฑด์‹์˜ ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ’1๊ณผ ๊ฐ™์„ ๊ฒฝ์šฐ ์ˆ˜ํ–‰๋  ๋ฌธ์žฅ๋“ค
        break;
        
    case ๊ฐ’2 :
    	// ์กฐ๊ฑด์‹์˜ ๊ฒฐ๊ณผ๊ฐ€ ๊ฐ’2๊ณผ ๊ฐ™์„ ๊ฒฝ์šฐ ์ˆ˜ํ–‰๋  ๋ฌธ์žฅ๋“ค
        break;

    default :
    	// ์กฐ๊ฑด์‹์˜ ๊ฒฐ๊ณผ์™€ ์ผ์น˜ํ•˜๋Š” case๋ฌธ์ด ์—†์„ ๋•Œ ์ˆ˜ํ–‰๋  ๋ฌธ์žฅ๋“ค
}

 

 

switch์˜ ์ œ์•ฝ์กฐ๊ฑด

  • switch๋ฌธ๊ณผ case๋ฌธ์˜ ๊ฒฐ๊ณผ๊ฐ’์ด ์ •์ˆ˜ ๋˜๋Š” ๋ฌธ์ž์—ด์ด์—ฌ ํ•œ๋‹ค.
  • case๋ฌธ ๊ฐ’์€ ๋ฐ˜๋“œ์‹œ ์ •์ˆ˜ ์ƒ์ˆ˜, ๋ฌธ์ž์—ด๋งŒ ๊ฐ€๋Šฅ, ์ค‘๋ณต๋˜๋ฉด ์•ˆ๋œ๋‹ค.
  • ๋ณ€์ˆ˜๋‚˜ ์‹ค์ˆ˜ ๋ฆฌํ„ฐ๋Ÿด์€ case๋ฌธ์˜ ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.
public static void main(String[] args) {
	int num, result;
    final int ONE = 1;
    
    sitch(result) {
    	case '1':			// ๋ฌธ์ž ๋ฆฌํ„ฐ๋Ÿด ๊ฐ€๋Šฅ (๋ฌธ์ž '1' == ์ •์ˆ˜ 49)
        case ONE:			// ์ •์ˆ˜ ์ƒ์ˆ˜ ๊ฐ€๋Šฅ
        case "YES":			// ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋  ๊ฐ€๋Šฅ
        
        case num:			// ๋ณ€์ˆ˜ ๋ถˆ๊ฐ€๋Šฅ
        case 1.0:			// ์‹ค์ˆ˜ ๋ถˆ๊ฐ€๋Šฅ
}

 

 

๐Ÿคน‍โ™€๏ธ ๋ฐ˜๋ณต๋ฌธ

์–ด๋–ค ์ž‘์—…์ด ๋ฐ˜๋ณต์ ์œผ๋กœ ์ˆ˜ํ–‰๋˜๋„๋ก ํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค.

for ๋ฌธ

์ดˆ๊ธฐํ™”, ์กฐ๊ฑด์‹, ์ฆ๊ฐ์‹, ๋ธ”๋Ÿญ { }  ์œผ๋กœ ๊ตฌ์„ฑ

์กฐ๊ฑด์‹์ด ์ฐธ์ธ ๋™์•ˆ ๋ธ”๋Ÿญ { } ๋‚ด์˜ ๋ฌธ์žฅ๋“ค์„ ๋ฐ˜๋ณตํ•˜๋‹ค ๊ฑฐ์ง“์ด ๋˜๋ฉด ๋ฐ˜๋ณต๋ฌธ์„ ๋ฒ—์–ด๋‚œ๋‹ค.

์ดˆ๊ธฐํ™”, ์กฐ๊ฑด์‹, ์ฆ๊ฐ์‹ ๋ถ€๋ถ„์„ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์กฐ๊ฑด์‹์ด ์ƒ๋žต๋œ ๊ฒฝ์šฐ ์ฐธ true๋กœ ๊ฐ„์ฃผ๋˜์–ด ๋ฌดํ•œ ๋ฐ˜๋ณต๋ฌธ์ด ๋œ๋‹ค.

๋ฌดํ•œ ๋ฐ•๋ณต์„ ํ•  ์‹œ ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด for๋ฌธ์„ ๋น ์ ธ ๋‚˜์˜ค๊ฒŒ ํ•ด์•ผ ํ•œ๋‹ค.

 

์ˆœ์„œ

1. ์ดˆ๊ธฐํ™”

2. ์กฐ๊ฑด์‹

3. ์ˆ˜ํ–‰๋  ๋ฌธ์žฅ๋“ค

4. ์ฆ๊ฐ์‹

for (์ดˆ๊ธฐํ™”; ์กฐ๊ฑด์‹; ์ฆ๊ฐ์‹) {
	
    // ์กฐ๊ฑด์‹์ด ์ฐธ true ์ธ ๋™์•ˆ ์ˆ˜ํ–‰๋  ๋ฌธ์žฅ๋“ค
    
}

 

 

์ค‘์ฒฉ for ๋ฌธ

for ๋ฌธ ์•ˆ์— ๋˜ ๋‹ค๋ฅธ for ๋ฌธ์„ ํฌํ•จ์‹œํ‚จ ๋ฌธ์žฅ

 

 

ํ–ฅ์ƒ๋œ for ๋ฌธ

JDK 1.5 ๋ถ€ํ„ฐ ๋ฐฐ์—ด๊ณผ ์ปฌ๋ ‰์…˜์— ์ €์žฅ๋œ ์š”์†Œ์— ์ ‘๊ทผํ•  ๋•Œ ํŽธ๋ฆฌํ•˜๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ์ƒˆ๋กœ์šด ๋ฌธ๋ฒ•์ด ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.

for(ํƒ€์ž… ๋ณ€์ˆ˜๋ช… : ๋ฐฐ์—ด์ด๋‚˜ ์ปฌ๋ ‰์…˜) {
	๋ฐ˜๋ณตํ•  ๋ฌธ์žฅ
}
String[] fruits = new String[]{"apple", "banana", "orange", "grapes", "kiwi",  "melon"}
for (String fruit : fruits) {
	System.out.println(fruit);
}

 

 

while ๋ฌธ

for ๋ฌธ์— ๋น„ํ•ด while ๋ฌธ์€ ๊ตฌ์กฐ๊ฐ€ ๊ฐ„๋‹จํ•˜๋‹ค.

if ๋ฌธ์ฒ˜๋Ÿผ ์กฐ๊ฑด์‹๊ณผ ๋ธ”๋Ÿญ { } ๋งŒ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

๋‹ค๋งŒ if ๋ฌธ๊ณผ ๋‹ฌ๋ฆฌ while ๋ฌธ์€ ์กฐ๊ฑด์‹์ด ์ฐธ true ์ธ ๋™์•ˆ ๋ฐ˜๋ณตํ•˜๋ฉฐ ์กฐ๊ฑด์‹์ด ๊ฑฐ์ง“์ด ๋˜๋ฉด ๋ฐ˜๋ณต์„ ๋ฉˆ์ถ˜๋‹ค.

 

์ˆœ์„œ

1. ์กฐ๊ฑด์‹์ด ์ฐธ true ์ด๋ฉด ๋ธ”๋Ÿญ { } ์•ˆ์œผ๋กœ ๋“ค์–ด๊ฐ€๊ณ , ๊ฑฐ์ง“ false ์ด๋ฉด while ๋ฌธ์„ ๋ฒ—์–ด๋‚œ๋‹ค.

2. ๋ธ”๋Ÿญ { } ์˜ ๋ฌธ์žฅ์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ๋‹ค์‹œ ์กฐ๊ฑด์‹์œผ๋กœ ๋Œ์•„๊ฐ„๋‹ค.

while (์กฐ๊ฑด์‹) {

	// ์กฐ๊ฑด์‹์˜ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ ์ฐธ true์ธ ๋™์•ˆ ๋ฐ˜๋ณต๋  ๋ฌธ์žฅ๋“ค

}

 

 

do - while ๋ฌธ

while ๋ฌธ๊ณผ ๋ฐ˜๋Œ€๋กœ ๋ธ”๋Ÿญ { }์„ ๋จผ์ € ์ˆ˜ํ–‰ํ•œ ํ›„์— ์กฐ๊ฑด์‹์„ ํ‰๊ฐ€ํ•œ๋‹ค.

๋”ฐ๋ผ์„œ ์ตœ์†Œํ•œ ํ•œ๋ฒˆ์€ ์ˆ˜ํ–‰๋  ๊ฒƒ์„ ๋ณด์žฅํ•œ๋‹ค.

๋์— ์Œ๋ฐ˜์  ; ์„ ์žŠ์ง€ ์•Š๋„๋ก ์ฃผ์˜ํ•˜์ž.

do {

	// ์กฐ๊ฑด์‹์˜ ์—ฐ์‚ฐ๊ฒฐ๊ณผ๊ฐ€ ์ฐธ์ผ ๋•Œ ์ˆ˜ํ–‰๋  ๋ฌธ์žฅ๋“ค์„ ์ ๋Š”๋‹ค. (์ฒ˜์Œ ๋ฌด์กฐ๊ฑด ํ•œ ๋ฒˆ์€ ์‹คํ–‰)

} while (์กฐ๊ฑด์‹);

 

 

break ๋ฌธ

์ž์‹ ์ด ํฌํ•จ๋œ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๋ฐ˜๋ณต๋ฌธ์„ ๋ฒ—์–ด๋‚œ๋‹ค.

์ฃผ๋กœ if ๋ฌธ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜์–ด ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•  ๋•Œ ๋ฐ˜๋ณต๋ฌธ์„ ๋ฒ—์–ด๋‚˜๊ฒŒ ๋œ๋‹ค.

 

 

continue ๋ฌธ

๋ฐ˜๋ณต๋ฌธ ๋‚ด์—์„œ๋งŒ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ฃผ๋กœ if ๋ฌธ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋œ๋‹ค.

๋ฐ˜๋ณต๋ฌธ์ด ์ง„ํ–‰๋˜๋Š” ๋„์ค‘์— continue๋ฌธ์„ ๋งŒ๋‚˜๋ฉด ๋ฐ˜๋ณต๋ฌธ์˜ ๋์œผ๋กœ ์ด๋™ํ•˜์—ฌ ๋‹ค์Œ ๋ฐ˜๋ณต์œผ๋กœ ๋„˜์–ด๊ฐ„๋‹ค.

for๋ฌธ์˜ ๊ฒฝ์šฐ ์ฆ๊ฐ์‹์œผ๋กœ ์ด๋™ํ•˜๋ฉฐ, while๋ฌธ๊ณผ do-while๋ฌธ์˜ ๊ฒฝ์šฐ ์กฐ๊ฑด์‹์œผ๋กœ ์ด๋™ํ•œ๋‹ค.

break๋ฌธ๊ณผ ๋‹ค๋ฅด๊ฒŒ ๋ฐ˜๋ณต๋ฌธ ์ „์ฒด๋ฅผ ๋ฒ—์–ด๋‚˜์ง€ ์•Š๊ณ  ๋‹ค์Œ ๋ฐ˜๋ณต์„ ๊ณ„์† ์ˆ˜ํ–‰ํ•œ๋‹ค.

 

 

 

 

 

 

๊ณผ์ œ 0. JUnit 5 ํ•™์Šตํ•˜์„ธ์š”.

์ธํ…”๋ฆฌJ, ์ดํด๋ฆฝ์Šค, VS Code์—์„œ JUnit 5๋กœ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์— ์ต์ˆ™ํ•ด ์งˆ ๊ฒƒ.

์ด๋ฏธ JUnit ์•Œ๊ณ  ๊ณ„์‹ ๋ถ„๋“ค์€ ๋‹ค๋ฅธ ๊ฒƒ ์•„๋ฌด๊ฑฐ๋‚˜!

๋” ์ž๋ฐ”, ํ…Œ์ŠคํŠธ ๊ฐ•์˜๋„ ์žˆ์œผ๋‹ˆ ์ฐธ๊ณ ํ•˜์„ธ์š”~

 

https://junit.org/junit5/docs/current/user-guide/

 

JUnit 5 User Guide

Although the JUnit Jupiter programming model and extension model do not support JUnit 4 features such as Rules and Runners natively, it is not expected that source code maintainers will need to update all of their existing tests, test extensions, and custo

junit.org

 

JUnit

์ž๋ฐ” ๊ฐœ๋ฐœ์ž 93%๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ๋‹จ์œ„ ํ…Œ์ŠคํŠธ ํ”„๋ ˆ์ž„ ์›Œํฌ

 

JUnit 5๋Š” Java 8(๋˜๋Š” ๊ทธ ์ด์ƒ)์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ์ด์ „ ๋ฒ„์ „์˜ JDK๋กœ ์ปดํŒŒ์ผ๋œ ์ฝ”๋“œ๋ฅผ ๊ณ„์† ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ด์ „ ๋ฒ„์ „๊ณผ ๋‹ฌ๋ฆฌ ์„ธ๊ฐ€์ง€ ํ•˜์œ„ ํ”„๋กœ์ ํŠธ์˜ ์—ฌ๋Ÿฌ ๋ชจ๋“ˆ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค.

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

 

JUnit Platform

JVM์—์„œ ํ…Œ์ŠคํŠธ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‹œ์ž‘ํ•˜๊ธฐ ์œ„ํ•œ ๊ธฐ๋ฐ˜ ์—ญํ• 

TestEngine Platform์—์„œ ์‹คํ–‰๋˜๋Š” ํ…Œ์ŠคํŠธ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ๊ฐœ๋ฐœํ•˜๊ธฐ ์œ„ํ•œ API ์ œ๊ณต

 

JUnit Jupiter

JUnit5๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

Jupiter ๊ธฐ๋ฐ˜ ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ํ…Œ์ŠคํŠธ ์—”์ง„ ์ œ๊ณต

 

JUnit Vintage

JUnit 3, JUnit 4 ํ…Œ์ŠคํŠธ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ํ…Œ์ŠคํŠธ ์—”์ง„ ์ œ๊ณต

 

 

 

์ธํ…”๋ฆฌ์ œ์ด๋กœ JUnit์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•ด๋ณด์ž.

 

Gradle ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋‹ˆ JUnit5 ์˜์กด์„ฑ์ด ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}

 

 

 

ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋ณด์ž.

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class Tests {

    @Test
    void addition() {
        assertEquals(2, 1+1);
    }

}

 

 

@Test

ํ…Œ์ŠคํŠธ ๋ฉ”์†Œ๋“œ์ž„์„ ์•Œ๋ ค์ฃผ๋Š” ํ…Œ์ŠคํŠธ

JUnit 4์˜ @Test์™€ ๋‹ฌ๋ฆฌ ์†์„ฑ์„ ์„ ์–ธํ•˜์ง€ ์•Š๋Š”๋‹ค.

JUnit Jupiter ์˜ ํ…Œ์ŠคํŠธ ํ™•์žฅ ํ”„๋กœ๊ทธ๋žจ์€ ์ž์ฒด ์ „์šฉ ์ฃผ์„์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ž‘๋™ํ•œ๋‹ค.

 

@BeforeEach

ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋˜๊ธฐ ์ „์— ๊ฐ ํ…Œ์ŠคํŠธ๋งˆ๋‹ค ์‹คํ–‰๋˜์–ด์•ผ ํ•œ๋‹ค.

@AfterEach

ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋œ ํ›„์— ๊ฐ ํ…Œ์ฆˆํŠธ๋งˆ๋‹ค ์‹คํ–‰๋˜์–ด์•ผ ํ•œ๋‹ค.

@BeforeAll

ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค๊ฐ€ ์‹คํ–‰๋˜๊ธฐ ์ „์— ์ „๋ถ€ ์‹คํ–‰๋˜์–ด์•ผ ํ•œ๋‹ค.

@AfterAll

ํ…Œ์ŠคํŠธ๊ฐ€ ๋ชจ๋‘ ์‹คํ–‰๋œ ํ›„์— ์‹คํ–‰๋˜์–ด์•ผ ํ•œ๋‹ค.

@Disabled

ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค ๋˜๋Š” ํ…Œ์ŠคํŠธ ๋ฉ”์„œ๋“œ๋ฅผ ๋น„ํ™œ์„ฑํ™” ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.

 

 

์‹คํ–‰ ์ˆœ์„œ

import org.junit.jupiter.api.*;

class Tests {


    @Test
    void test() {
        System.out.println("test");
    }

    @BeforeEach
    void beforeEach() {
        System.out.println("beforeEach");
    }

    @BeforeAll
    static void beforeSetup() {
        System.out.println("beforeAll");
    }

    @AfterAll
    static void afterAll() {
        System.out.println("afterAll");
    }

    @AfterEach
    void afterEach() {
        System.out.println("afterEach");
    }

}
beforeAll
beforeEach
test
afterEach
afterAll

 

 

 

@DisplayName

ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค ๋˜๋Š” ํ…Œ์ŠคํŠธ ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•œ ์‚ฌ์šฉ์ž ์ง€์ • ํ‘œ์‹œ ์ด๋ฆ„ ์„ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.

@DisplayNameGeneration

ํ…Œ์ŠคํŠธ ํด๋ž˜์Šค ๋˜๋Š” ํ…Œ์ŠคํŠธ ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•œ ์‚ฌ์šฉ์ž ์ง€์ • ํ‘œ์‹œ ์ด๋ฆ„ ์ƒ์„ฑ๊ธฐ ๋ฅผ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.

strategy๋งˆ๋‹ค ์ถœ๋ ฅ ๊ฒฐ๊ณผ ์ด๋ฆ„์ด ๋‹ฌ๋ผ์ง„๋‹ค.

 

 

 

JUnit 5 Assertions

JUnit 4๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฉ”์†Œ๋“œ์™€ Java 8์˜ ๋žŒ๋‹ค์™€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.

https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/Assertions.html

 

Assertions (JUnit 5.9.0 API)

Assert that all supplied executables do not throw exceptions. If any supplied Executable throws an exception (i.e., a Throwable or any subclass thereof), all remaining executables will still be executed, and all exceptions will be aggregated and reported i

junit.org

 

assertEquals(expected, actual)

๊ธฐ๋Œ€ํ•œ ๊ฐ’์ด ์‹ค์ œ ๊ฐ’๊ณผ ๊ฐ™์€์ง€ ํ™•์ธํ•˜๋Š” ๋ฉ”์„œ๋“œ

assertNotNull(Object actual)

๊ฒฐ๊ณผ ๊ฐ’์ด null ์ธ์ง€ ํ™•์ธํ•˜๋Š” ๋ฉ”์„œ๋“œ

assertTrue(boolean  condition)

์กฐ๊ฑด์ด ์ฐธ์ธ์ง€ ํ™•์ธํ•˜๋Š” ๋ฉ”์„œ๋“œ

assertThrows(Class<T> expectedType, Executable executable)

์˜ˆ์™ธ ๋ฐœ์ƒ ํ™•์ธํ•˜๋Š” ๋ฉ”์„œ๋“œ

assertTimeout(Duration timeout, Executable executable)

ํŠน์ • ์‹œ๊ฐ„ ์•ˆ์— ์‹คํ–‰์ด ์™„๋ฃŒ๋˜๋Š”์ง€ ํ™•์ธ

 

 

 

 

 

๊ณผ์ œ 1. live-study ๋Œ€์‹œ ๋ณด๋“œ๋ฅผ ๋งŒ๋“œ๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.

  • ๊นƒํ—™ ์ด์Šˆ 1๋ฒˆ๋ถ€ํ„ฐ 18๋ฒˆ๊นŒ์ง€ ๋Œ“๊ธ€์„ ์ˆœํšŒํ•˜๋ฉฐ ๋Œ“๊ธ€์„ ๋‚จ๊ธด ์‚ฌ์šฉ์ž๋ฅผ ์ฒดํฌ ํ•  ๊ฒƒ.
  • ์ฐธ์—ฌ์œจ์„ ๊ณ„์‚ฐํ•˜์„ธ์š”. ์ด 18ํšŒ์— ์ค‘์— ๋ช‡ %๋ฅผ ์ฐธ์—ฌํ–ˆ๋Š”์ง€ ์†Œ์ˆซ์  ๋‘์ž๋ฆฌ๊ฐ€์ง€ ๋ณด์—ฌ์ค„ ๊ฒƒ.
  • Github ์ž๋ฐ” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
  • ๊นƒํ—™ API๋ฅผ ์ต๋ช…์œผ๋กœ ํ˜ธ์ถœํ•˜๋Š”๋ฐ ์ œํ•œ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ณธ์ธ์˜ ๊นƒํ—™ ํ”„๋กœ์ ํŠธ์— ์ด์Šˆ๋ฅผ ๋งŒ๋“ค๊ณ  ํ…Œ์ŠคํŠธ๋ฅผ ํ•˜์‹œ๋ฉด ๋” ์ž์ฃผ ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

Access Token ์ƒ์„ฑ

GitHub - ์šฐ์ธก ์ƒ๋‹จ ํ”„๋กœํ•„ ํด๋ฆญ - Setting

 

 

 

Developer Setting - Personal access tokens -Generate

 

repo ๊ถŒํ•œ ์„ ํƒ - Generate token

๋ณต์‚ฌํ•ด์„œ ์‚ฌ์šฉํ•˜๊ณ  ๊นƒํ—ˆ๋ธŒ ์ ‘๊ทผ์‹œ ํŒจ์Šค์›Œ๋“œ์™€ ๊ฐ™์œผ๋ฏ€๋กœ ์ž˜ ์ €์žฅํ•ด๋‘์ž.

 

 

Postman์œผ๋กœ ํ† ํฐ์ด ์ •์ƒ์ ์œผ๋กœ ๋ฐœ๊ธ‰๋˜์—ˆ๋Š”์ง€ ํ™•์ธ

GET ๋ฐฉ์‹์œผ๋กœ https://api.github.com/user ๋กœ ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด 

Headers์— ์•„๋ž˜ ๋‚ด์šฉ๋„ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

key : Authorization

value :  token [๋ฐฉ๊ธˆ ๋ฐœ๊ธ‰๋ฐ›์€ ํ† ํฐ]

 

์‘๋‹ต์œผ๋กœ 200 OK, ๋ณธ์ธ GitHub ์ •๋ณด๊ฐ€ ๋œจ๋ฉด ์„ฑ๊ณต!

 

 

GitHub API for Java - Download

๋‚˜๋Š” Gradle ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— gradle ์„ ๋ˆŒ๋Ÿฌ ๋ณต์‚ฌํ•ด์„œ

 

 

 

 

build.gradle์— ๋ถ™์—ฌ๋„ฃ๊ณ  refresh ํ•ด์ฃผ์—ˆ๋‹ค.

 

 

 

์ฝ”๋“œ ์ž‘์„ฑ ํ›„ RUN !!!

 

 

 

์ฐธ๊ณ 

๊นƒํ—ˆ๋ธŒ 2FA Personal Access Token ์‚ฌ์šฉํ•˜๊ธฐ

 

 

 

 

 

๊ณผ์ œ 2. LinkedList๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”.

  • LinkedList์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•˜์„ธ์š”.
  • ์ •์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ListNode ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”.
  • ListNode add(ListNode head, ListNode nodeToAdd, int position)๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”.
  • ListNode remove(ListNode head, int positionToRemove)๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”.
  • boolean contains(ListNode head, ListNode nodeTocheck)๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”.

 

package week4.linkedList;

public class ListNode {
    private int data;
    public ListNode link;

    public ListNode() {
        this.data = 0;
        this.link = null;
    }

    public ListNode(int data){
        this.data = data;
        this.link = null;
    }

    public ListNode(int data, ListNode link) {
        this.data = data;
        this.link = link;
    }

    public int getData(){
        return this.data;
    }

    public ListNode add(ListNode head, ListNode nodeToAdd, int position) {
        ListNode node = head;

        for (int i=0; i < position-1; i++) {
            node = node.link;
        }

        nodeToAdd.link = node.link;
        node.link = nodeToAdd;
        return nodeToAdd;

    }

    public ListNode remove(ListNode head, int positionToRemove) {
        ListNode node = head;

        for (int i=0; i < positionToRemove-1; i++) {
            node = node.link;
        }

        ListNode temp = node.link;
        node.link = temp.link;
        temp.link = null;
        return temp;

    }

    public boolean contains(ListNode head, ListNode nodeTocheck) {
        ListNode node = head;

        while (node.link != null) {
            if (node == nodeTocheck){
                return true;
            }
            node = node.link;
        }
        return false;
    }

    public void printList(ListNode head) {
        ListNode temp = head;
        System.out.print(" ์‹œ์ž‘ : ");
        while (temp.link != null) {
            System.out.print(temp.data + ", ");
            temp = temp.link;
        }
        System.out.println(temp.data);
    }
}
import org.junit.jupiter.api.*;
import week4.linkedList.ListNode;

import static org.junit.jupiter.api.Assertions.*;

public class LinkedListTest {

    ListNode listNode = new ListNode();

    ListNode five = new ListNode(5);
    ListNode four = new ListNode(4, five);
    ListNode three = new ListNode(3, four);
    ListNode two = new ListNode(2, three);
    ListNode one = new ListNode(1, two);
    ListNode zero = new ListNode(0, one);


    @AfterEach
    void printAll(){
        listNode.printList(zero);
    }


    @Test()
    public void addTest() throws Exception {
        ListNode six = new ListNode(6);
        zero.add(zero, six, 3);
    }

    @Test
    public void removeTest() throws Exception {
        listNode.remove(listNode, 3);
    }

    @Test
    public void containTest() throws Exception {
        boolean containStatus = zero.contains(zero, three);
        assertTrue(containStatus);
    }

    @Test
    public void excludeTest() throws Exception {
        ListNode seven = new ListNode(7);
        boolean containStatus = zero.contains(zero, seven);
        assertEquals(false, containStatus);
    }


}

 

 

 

 

 

๊ณผ์ œ 3. Stack์„ ๊ตฌํ˜„ํ•˜์„ธ์š”.

  • int ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•ด์„œ ์ •์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” Stack์„ ๊ตฌํ˜„ํ•˜์„ธ์š”.
  • void push(int data)๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”.
  • int pop()์„ ๊ตฌํ˜„ํ•˜์„ธ์š”.

 

public class ArrayStack {

    public int[] arr;
    public int top = -1;

    public ArrayStack(int length){
        arr = new int[length];
    }

    public void push(int data) {
        if (top < arr.length-1) {
            arr[++top] = data;
        } else {
            throw new StackOverflowError();
        }
    }

    public int pop() {
        if (top>0) {
            int temp = arr[top--];
            return temp;
        } else {
            throw new NullPointerException();
        }
    }

}
public class ArrayStackTest {

    @Test @DisplayName("push O")
    public void pushTest() throws Exception {
        ArrayStack stack = new ArrayStack(5);

        stack.push(0);
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(45);


        Assertions.assertEquals(stack.arr[stack.top], stack.arr[4]);
    }

    @Test @DisplayName("ArrayIndexOutOfBoundsException")
    public void ArrayIndexOutOfBoundsExceptionTest() throws Exception {
        ArrayStack stack = new ArrayStack(5);

        stack.push(0);
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(45);
        stack.push(6);

    }

    @Test @DisplayName("pop O")
    public void popTest() throws Exception {
        ArrayStack stack = new ArrayStack(5);
        stack.push(0);
        stack.push(1);
        stack.push(2);
        stack.pop();
        stack.pop();
    }

    @Test @DisplayName("NullPointerException")
    public void popTestFail() throws Exception {
        ArrayStack stack = new ArrayStack(5);
        stack.push(0);
        stack.push(1);
        stack.push(2);
        stack.pop();
        stack.pop();
        stack.pop();
    }

}

 

 

 

 

 

๊ณผ์ œ 4. ์•ž์„œ ๋งŒ๋“  ListNode๋ฅผ ์‚ฌ์šฉํ•ด์„œ Stack์„ ๊ตฌํ˜„ํ•˜์„ธ์š”.

  • ListNode head๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ListNodeStack ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”.
  • void push(int data)๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”.
  • int pop()์„ ๊ตฌํ˜„ํ•˜์„ธ์š”.
public class ListNodeStack {

    public ListNode head;

    public boolean isEmpty(){
        return (head == null);
    }

    public void push(int data) {
        if (isEmpty()) {
            ListNode newNode = new ListNode(data);
            head = newNode;
        } else {
            ListNode newNode = new ListNode(data, head);
            head = newNode;
        }
    }

    public int pop() {
        if (isEmpty()) {
            throw new NullPointerException();
        } else {
            int temp = head.getData();
            head = head.link;
            return temp;
        }
    }
}
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import week4.ArrayStack;

import static org.junit.jupiter.api.Assertions.*;


public class ArrayStackTest {

    @Test @DisplayName("push O")
    public void pushTest() throws Exception {
        ArrayStack stack = new ArrayStack(5);

        stack.push(0);
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(45);


        assertEquals(stack.arr[stack.top], stack.arr[4]);
    }

    @Test @DisplayName("ArrayIndexOutOfBoundsException")
    public void ArrayIndexOutOfBoundsExceptionTest() throws Exception {
        ArrayStack stack = new ArrayStack(5);

        stack.push(0);
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(45);
        stack.push(6);


    }

    @Test @DisplayName("pop O")
    public void popTest() throws Exception {
        ArrayStack stack = new ArrayStack(5);
        stack.push(0);
        stack.push(1);
        stack.push(2);
        int pop = stack.pop();
        assertEquals(2, pop);
    }

    @Test @DisplayName("NullPointerException")
    public void popTestFail() throws Exception {
        ArrayStack stack = new ArrayStack(5);
        stack.push(0);
        stack.push(1);
        stack.push(2);
        stack.pop();
        stack.pop();
        stack.pop();
    }

}

 

 

 

 

 

๊ณผ์ œ 5. Queue๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”.

  • ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•ด์„œ ํ•œ๋ฒˆ
  • ListNode๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•œ๋ฒˆ.

 

  • ๋ฐฐ์—ด
public class ArrayQueue {

    public int head;
    public int tail;
    public int queueSize;
    public int[] arr;

    public ArrayQueue(int queueSize){
        head = -1;
        tail = -1;
        this.queueSize = queueSize;
        arr = new int[queueSize];
    }

    public boolean isEmpty() {
        return (head == tail);
    }

    public boolean isFull() {
        return (tail == queueSize - 1);
    }

    public void enQueue(int data) {
        if(isFull()){
            throw new IndexOutOfBoundsException();
        } else {
            arr[++tail] = data;
        }
    }

    public void deQueue() {
        if(isEmpty()){
            throw new NullPointerException();
        } else {
            ++head;
        }
    }
}
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class ArrayQueueTest {
    ArrayQueue queue = new ArrayQueue(3);

    @Test
    public void enQueueTest() {
        queue.enQueue(1);
        queue.enQueue(2);
        queue.enQueue(3);

        assertEquals(3, queue.arr[queue.tail]);
    }

    @Test
    public void IndexOutOfBoundsExceptionTest() {
        queue.enQueue(1);
        queue.enQueue(2);
        queue.enQueue(3);
        queue.enQueue(4);
    }

    @Test
    public void deQueue() {
        queue.enQueue(1);
        queue.enQueue(2);
        queue.enQueue(3);
        queue.deQueue();

        assertEquals(2, queue.tail);
    }

    @Test
    public void NullPointerExceptionTest() {
        queue.enQueue(1);
        queue.enQueue(2);
        queue.enQueue(3);
        queue.deQueue();
        queue.deQueue();
        queue.deQueue();
        queue.deQueue();
    }
}

 

  • ListNode
public class ListNodeQueue {

    public ListNode head;
    public ListNode tail;

    public boolean isEmpty() {
        return (head == null);
    }

    public void enQueue(int data) {
        ListNode newNode = new ListNode(data);

        if(isEmpty()){
            head = newNode;
            tail = newNode;
        } else {
            tail.link = newNode;
            tail = newNode;
        }
    }

    public void deQueue() {
        if (isEmpty()) {
            throw new NullPointerException();
        } else {
            head = head.link;
        }
    }
}
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class ListNodeQueueTest {

    ListNodeQueue queue = new ListNodeQueue();

    @Test
    public void enQueueTest() {
        queue.enQueue(1);
        queue.enQueue(2);
        queue.enQueue(3);

        assertEquals(1, queue.head.getData());
        assertEquals(3, queue.tail.getData());
    }

    @Test
    public void deQueueTest() {
        queue.enQueue(1);
        queue.enQueue(2);
        queue.enQueue(3);
        queue.deQueue();

        assertEquals(2, queue.head.getData());
    }

    @Test
    public void NullPointerExceptionTest() {
        queue.enQueue(1);
        queue.enQueue(2);
        queue.enQueue(3);
        queue.deQueue();
        queue.deQueue();
        queue.deQueue();
        queue.deQueue();
    }
}