🐯[TIL] 250612-009

byoΒ·2025λ…„ 6μ›” 12일

πŸ’« JAVA

βœ”οΈ scanner μΆ”κ°€λ‚΄μš©

βœ… scanner λ¦¬μ†ŒμŠ€ ν•΄μ œ

scanner도 buffer μž₯μΉ˜κ°€ μžˆλ‹€!
scanner μ‚¬μš© ν›„ scanner.close ν•΄μ£Όμ–΄μ•Ό ν•œλ‹€.

βœ… scanner μž…λ ₯ μ‹œ \n λ„˜μ–΄κ°€λŠ” 문제

  • μž…λ ₯ 후에 μž…λ ₯ν•˜λŠ” Enterκ°€ λ‹€μŒ μŠ€μΊλ„ˆμ— λ„˜μ–΄κ°€κΈ° λ•Œλ¬Έμ— \n을 λ°›μ•„μ£ΌλŠ” scanner ν•˜λ‚˜ 더 λ§Œλ“€λ©΄ λœλ‹€.
πŸ”΄πŸŸ‘πŸŸ’

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.print("λ‚˜μ΄λ₯Ό μž…λ ₯ν•˜μ„Έμš” : ");
        int age = sc.nextInt();
        System.out.print("이름을 μž…λ ₯ν•˜μ„Έμš” : ");
        sc.nextLine();
        String name = sc.nextLine();
        System.out.printf("λ‚˜μ΄ : %d, 이름 : %s\n", age, name);
    }
}

πŸ€” ν€΄μ¦ˆβ— μž…λ ₯κ°’μ˜ μ œκ³±μ„ 좜λ ₯ν•΄ 보자

πŸ”΄πŸŸ‘πŸŸ’

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        System.out.printf("%.2f", Math.pow(num, 2));
    }
}

βœ”οΈ 상속 | Inheritance extends

상속 : λΆ€λͺ¨ 클래슀의 ν•„λ“œ(κ°’), λ©”μ†Œλ“œ(ν•¨μˆ˜)λ₯Ό λ¬Όλ €λ°›λŠ” 것
λ³„λ„μ˜ κ΅¬ν˜„ 없이 λΆ€λͺ¨μ˜ λ©”μ†Œλ“œλ₯Ό μ‚¬μš©ν•  수 μžˆλ‹€!

βœ… extends | super | this

extends : 상속을 ν‘œν˜„ν•˜λŠ” ν‚€μ›Œλ“œ
super : λΆ€λͺ¨ 클래슀의 ν•„λ“œ/μƒμ„±μž/λ©”μ†Œλ“œ μ°Έμ‘°
this : ν˜„μž¬ 클래슀의 ν•„λ“œ/λ©”μ†Œλ“œ μ°Έμ‘°

  • μ˜ˆμ‹œ
πŸ”΄πŸŸ‘πŸŸ’

class Animal {
    String name;
    public Animal(String name) {
        this.name = name; //ν˜„μž¬ μΈμŠ€ν„΄μŠ€ κ°’ μ΄ˆκΈ°ν™”
    }
    public void printName(String name) {
        System.out.println("Animal name : " + name + this.name);
    }
}
class Dog extends Animal {
    String breed;
    //λΆ€λͺ¨ ν΄λž˜μŠ€κ°€ μƒμ„±μžμ—μ„œ λ§€κ°œλ³€μˆ˜λ₯Ό μš”κ΅¬ν•˜λ©΄, μžμ‹λ„ λ°˜λ“œμ‹œ ν˜ΈμΆœν•΄μ•Ό 함
    //String nameμ—†μœΌλ©΄ -> There is no no-arg constructor available in 'Animal'
    public Dog(❗String name) {
        super(name);
    }
    public Dog(String name, String breed) {
        super(name); // β—μˆœμ„œ μ€‘μš”! λΆ€λͺ¨κ°€ μš”κ΅¬ν•˜λŠ” ν•„λ“œλ₯Ό λ¨Όμ € μ±„μ›Œμ•Ό ν•œλ‹€. (νƒλ°°λ°•μŠ€ 전달)
        this.breed = breed;
    }
}

βœ”οΈ μΈν„°νŽ˜μ΄μŠ€ | Interface implements

↔️ ν΄λž˜μŠ€μ™€ μΈν„°νŽ˜μ΄μŠ€μ˜ 차이

μ’…λ₯˜μ„€λͺ…
ν΄λž˜μŠ€κ°’ + κΈ°λŠ₯(κ΅¬ν˜„λ¨)
μΈν„°νŽ˜μ΄μŠ€κΈ°λŠ₯에 λŒ€ν•œ μ •μ˜(κ΅¬ν˜„μ€ ν•˜μ²­)

πŸ’‘νš¨κ³Ό : 개발의 μœ μ—°μ„±μ΄ μ¦κ°€λœλ‹€!

✏️ μΈν„°νŽ˜μ΄μŠ€ μ˜ˆμ‹œ

πŸ”΄πŸŸ‘πŸŸ’

public interface Player {
    // λ‚˜μ˜ κ°’ ν‘œμ‹œν•˜λŠ” ν•¨μˆ˜
    boolean cooperate(int round);
    // μƒλŒ€μ˜ 이전 κ°’ 기둝
    void recordOpponentMove(boolean opponentMove);
}
public class Pavlov implements Player {
    // Payoff μƒμˆ˜ (Main.java와 λ™μΌν•˜κ²Œ μ„€μ •)
    private static final int R = 3; // μ–‘μͺ½ ν˜‘λ ₯
    private static final int T = 5; // λ‚΄κ°€ λ°°μ‹ , μƒλŒ€ ν˜‘λ ₯
    private static final int P = 1; // μ–‘μͺ½ λ°°μ‹ 
    private static final int S = 0; // λ‚΄κ°€ ν˜‘λ ₯, μƒλŒ€ λ°°μ‹ 

    // λ§ˆμ§€λ§‰ λ‚΄ 선택과 κ·Έλ•Œ 받은 보상을 μ €μž₯
    private boolean lastMyMove = true;
    private int lastPayoff = R;

    /**
     * @param maxRounds 전체 λΌμš΄λ“œ 수 (Pavlov μ•Œκ³ λ¦¬μ¦˜ μžμ²΄λŠ” λΆˆν•„μš”ν•˜λ‚˜
     *                  Mainμ—μ„œ μƒμ„±μž μ‹œκ·Έλ‹ˆμ²˜λ₯Ό λ§žμΆ”κΈ° μœ„ν•΄ λ°›μŠ΅λ‹ˆλ‹€)
     */
    public Pavlov(int maxRounds) {
        // μ „λž΅ μœ μ§€μ— 별도 νžˆμŠ€ν† λ¦¬ 배열은 ν•„μš” μ—†μŠ΅λ‹ˆλ‹€
    }

    @Override
    public boolean cooperate(int round) {
        if (round == 1) {
            // 첫 λΌμš΄λ“œλŠ” 무쑰건 ν˜‘λ ₯
            lastMyMove = true;
        } else {
            // β€œWin‑Stay, Lose‑Shift”
            // 보상 R λ˜λŠ” P(= 곡동 ν˜‘λ ₯ λ˜λŠ” 곡동 λ°°μ‹ )일 λ•ŒλŠ” ν˜„μž¬ μ „λž΅ μœ μ§€
            if (lastPayoff == R || lastPayoff == P) {
                // lastMyMove κ·ΈλŒ€λ‘œ μœ μ§€
            } else {
                // κ·Έ μ™Έ(λ‚΄κ°€ ν˜‘λ ₯ν–ˆμœΌλ‚˜ μƒλŒ€κ°€ λ°°μ‹ ν•΄ Sλ₯Ό μ–»μ—ˆκ±°λ‚˜,
                // λ‚΄κ°€ λ°°μ‹ ν–ˆμœΌλ‚˜ μƒλŒ€λ„ λ°°μ‹ ν•΄ Pλ₯Ό μ œμ™Έν•œ Tλ₯Ό μ–»μ—ˆμ„ λ•Œ)
                // μ „λž΅ μ „ν™˜
                lastMyMove = !lastMyMove;
            }
        }
        return lastMyMove;
    }

    @Override
    public void recordOpponentMove(boolean opponentMove) {
        // 직전 λΌμš΄λ“œ 보상을 κ³„μ‚°ν•˜μ—¬ μ €μž₯
        if (lastMyMove && opponentMove) {
            lastPayoff = R;
        } else if (lastMyMove && !opponentMove) {
            lastPayoff = S;
        } else if (!lastMyMove && opponentMove) {
            lastPayoff = T;
        } else {
            lastPayoff = P;
        }
    }
}

Pavlovν•¨μˆ˜λŠ” Player μΈν„°νŽ˜μ΄μŠ€λ₯Ό implement ν–ˆκΈ° λ•Œλ¬Έμ— λ°˜λ“œμ‹œ cooperate ν•¨μˆ˜μ™€ recordOpponentMove ν•¨μˆ˜λ₯Ό κ΅¬ν˜„ν•΄μ•Ό ν•œλ‹€.

βœ”οΈ μ£„μˆ˜μ˜ λ”œλ ˆλ§ˆ μ•Œκ³ λ¦¬μ¦˜

profile
πŸ—‚οΈ hamstern

0개의 λŒ“κΈ€