[Java] Solution to puzzles

Joy๐ŸŒฑยท2022๋…„ 12์›” 22์ผ
0

๐Ÿงฉ Coding Challenges

๋ชฉ๋ก ๋ณด๊ธฐ
1/20
post-thumbnail
post-custom-banner

๐Ÿงฉ ๊ฐ„๋‹จํ•œ ๊ณ„์‚ฐ method๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ๋งŒ๋“  ํ›„, ๋‹ค๋ฅธ ํด๋ž˜์Šค์—์„œ call ํ•˜๊ธฐ

โ—ผ Application Class

package com.greedy.level01.basic;

public class Application {

	public static void main(String[] args) {

		int a = 10;
		int b = 20;
		
		Calculator calc = new Calculator(); 
		
		// ํ•จ์ˆ˜ ํ˜ธ์ถœ, ํ™•์ธ์šฉ ๋ฉ”์†Œ๋“œ
		calc.checkMethod();
		
		// 1๋ถ€ํ„ฐ 10๊นŒ์ง€ ์ˆ˜๋ฅผ ๋”ํ•˜์—ฌ ๊ฐ’์„ ๋ฆฌํ„ดํ•จ
		int sum = calc.sum1to10();
		System.out.println("1๋ถ€ํ„ฐ 10๊นŒ์ง€์˜ ํ•ฉ : " + sum);
		>>> ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋ณ€์ˆ˜ sum์— ์ €์žฅ ํ›„ ์ถœ๋ ฅ
		
		// ๋‘ ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ํฐ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•จ
		calc.checkMaxNumber(a, b);
		int max = a > b ? a : b;
		System.out.println("๋‘ ์ˆ˜ ์ค‘ ํฐ ์ˆ˜๋Š” " + max + "์ด๋‹ค.");
		>>> ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ > ๋น„๊ต ์—ฐ์‚ฐ์ž ์‚ฌ์šฉํ•˜์—ฌ ๋ณ€์ˆ˜ max์— ์ €์žฅ > ์ถœ๋ ฅ
		
		// ๋‘ ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ํ•ฉ์„ ๋ฆฌํ„ดํ•จ
		int sumTwo = calc.sumTwoNumber(a, b);
		System.out.println("10๊ณผ 20์˜ ํ•ฉ์€ : " + sumTwo);
		
		
		// ๋‘ ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์ฐจ๋ฅผ ๋ฆฌํ„ดํ•จ
		System.out.println("10๊ณผ 5์˜ ์ฐจ๋Š” : " + calc.minusTwoNumber(10, 5));
		>>> ์ˆ˜ ์ž…๋ ฅ, ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ, ์ถœ๋ ฅ์„ ๋™์‹œ์— ์ง„ํ–‰
	}

}

โ—ผ Calculator Class

package com.greedy.level01.basic;

public class Calculator {

	public void checkMethod() {
		
		System.out.println("๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ ํ™•์ธ");
	}
	
	public int sum1to10() {
		
		int result = 0;
		
		for(int num = 1; num <= 10; num++) {
			result = result + num;
			>>> num : 1์ด 10์ด ๋  ๋•Œ๊นŒ์ง€ ์ฆ๊ฐ€ํ•˜๋Š” ๊ฒƒ์„ ๋ฐ˜๋ณต
			>>> result : num์ด 1์”ฉ ์ฆ๊ฐ€๋˜๋ฉด์„œ result์— ๊ณ„์† ๊ฐ’์ด ์Œ“์ž„ 
		    >>> 0 = 0 + 1 + 2 + ... + 10 = 55
		}	
		
		return result;
	}
	
	public void checkMaxNumber(int a, int b) { 
		
	}
	
	public int sumTwoNumber(int a, int b) {
		
		return a + b;
	}
	
	public int minusTwoNumber(int a , int b) {
			
		return a - b;
	}
}

๐Ÿ”‘ Used Keywords
method for return

profile
Tiny little habits make me
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€