풀스택 개발자 과정 45일차

너구·2026년 7월 13일

풀스택 성장과정

목록 보기
48/79

AccessTest

package Day08;

class A {
    public int a;
    protected String str;
    private char ch;
    int b;
}

class B {
    A a = new A();
    void useA() {
        a.a = 10; // public이라서 공용으로 사용 가능
        a.str = "korea"; // 동일 패키지 내부와 이를 상속받은 자식 클래스만 사용 가능
        a.b = 30; // default 같은 패키지
        // a.ch private 접근 x
    }
}

public class AccessTest {
}

InheritanceTest

package Day08;

class C {
    int a, b, c;
    C() {
        System.out.println("C 생성");
    }

    void abc() {
        System.out.println("abc");
    }
}

class D extends C {
    D() {
        System.out.println("D 생성");
    }
}

class E extends D {

}

class F extends C {

}

public class InheritanceTest {
    static void main() {
        E e = new E(); // C -> D -> E
        e.a = 20; // 조부모 C 사용
        e.abc(); // 조부모 abc() 사용
    }
}

Q1

package Day08;

// 오박사 포켓몬 볼에 랜덤으로 포켓몬, 색깔, 크기가 정해져서 3개의 포켓몬 볼에 들어가 있다.
// 지우가 오박사한테 1, 2, 3 번 중 하나를 선택해서 하나의 포켓몬 볼을 받는다.
// 그 받은 포켓몬의 포켓몬 볼을 열어서 어떤 포켓몬인지 출력을 하면 된다.
// ex) 파란색 피카츄이고 크기는 중입니다.

import java.util.Random;
import java.util.Scanner;

class Poketmon {
    String name;
    String color;
    String size;

    Poketmon(String name, String color, String size) {
        this.name = name;
        this.color = color;
        this.size = size;
    }
}

class PoketmonBall {
    Poketmon poketmon;
    PoketmonBall(Poketmon poketmon) {
        this.poketmon = poketmon;
    }
}

class Jiwoo {
    PoketmonBall poketmonBall;
    Poketmon poketmon;
    Jiwoo() {
        this.poketmonBall = poketmonBall;
        this.poketmon = poketmon;
    }
}

class DrO {
    PoketmonBall poketmonBall1;
    PoketmonBall poketmonBall2;
    PoketmonBall poketmonBall3;

    DrO() {
        poketmonBall1 = randomBall();
        poketmonBall2 = randomBall();
        poketmonBall3 = randomBall();
    }

    PoketmonBall randomBall() {
        Random r = new Random();
        return null;
        // 미완성코드 오류나서 null 값 넣어놨다.
    }
}

public class Q1 {
    static void main() {
        Scanner sc = new Scanner(System.in);
        System.out.println("어떤 포켓몬 볼을 선택하시겠습니까?");
        sc.nextInt();
        
        
    }
}

미완성 코드다 시간이 부족했다.

Q1 선생님 풀이

package Day08;

import java.util.Random;
import java.util.Scanner;

class PokeMon {
    String name;
    String color;
    String size;

    PokeMon() {
        Random r = new Random();
        String[] names = {"잉어킹", "파이리", "꼬부기", "이상해씨"};
        String[] color = {"노란색", "파란색", "빨간색", "초록색"};
        String[] size = {"대", "중", "소", "미니"};
        this.name = names[r.nextInt(4)];
        this.color = color[r.nextInt(4)];
        this.size = size[r.nextInt(4)];
    }

    void talk() {
        System.out.println(color + " " + name + "이고 크기는 " + size + "입니다.");
    }
}

class PokeBall {
    PokeMon pm;
}

class Ziwoo {
    PokeBall pb;
}

class Oh {
    PokeBall[] pb = new PokeBall[3];
    Oh() {
        for (int i = 0; i < pb.length; i++) {
            pb[i] = new PokeBall();
        }
    }
}

public class Q1_1 {
    static void main() {
        // 1. 오박사가 포켓몬 잡아서 포켓몬 볼에 넣는다.
        Oh oh = new Oh();
        for (int i = 0; i < oh.pb.length; i++) {
            oh.pb[i].pm = new PokeMon();
            oh.pb[i].pm.talk();
        }
        // 2. 지우가 포켓몬 볼을 선택한다.
        Ziwoo ziwoo = new Ziwoo();
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("지우야 포켓몬을 선택하렴 (1, 2, 3) : ");
            int num = sc.nextInt();
            if (num > 0 && num < 4) {
                ziwoo.pb = oh.pb[num-1];
                break;
            } else {
                System.out.println("지우야 혼날래? 다시 골라라");
            }
        }
        // 3. 선택 후 안에 있는 포켓몬이 말한다.
        ziwoo.pb.pm.talk();
    }
}

Q2

package Day08;

/*
계산기 프로그램을 구현하시오.

조건
- 정수 연산과 실수 연산을 선택할 수 있다.
- 더하기, 빼기, 곱하기, 나누기 기능을 구현한다.
- 메서드 오버로딩을 사용하여 같은 이름의 연산 메서드를 만든다.
- 사용자가 종료를 선택하기 전까지 계속 실행한다.
*/

import java.util.Scanner;

class Controller {

    int add(int a, int b) {
        return a + b;
    }

    double add(double a, double b) {
        return a + b;
    }

    int sub(int a, int b) {
        return a - b;
    }

    double sub(double a, double b) {
        return a - b;
    }

    int mult(int a, int b) {
        return a * b;
    }

    double mult(double a, double b) {
        return a * b;
    }

    int div(int a, int b) {
        return a / b;
    }

    double div(double a, double b) {
        return a / b;
    }
}

public class Q2 {
    static void main() {

        Controller con = new Controller();

        Scanner sc = new Scanner(System.in);

        while (true) {
            System.out.println("1. 정수연산 2. 실수연산 3. 종료");
            System.out.print("숫자를 입력하세요: ");
            int num = sc.nextInt();

            if (num == 1) {
                System.out.println("1. 더하기 2. 빼기 3. 곱하기 4. 나누기");
                System.out.print("숫자를 입력하세요: ");
                int num2 = sc.nextInt();

                System.out.print("숫자 1 입력: ");
                int integerNum1 = sc.nextInt();

                System.out.print("숫자 2 입력: ");
                int integerNum2 = sc.nextInt();

                if (num2 == 1) {
                    System.out.println("결과값은: " + con.add(integerNum1, integerNum2) + "입니다.");
                } else if (num2 == 2) {
                    System.out.println("결과값은: " + con.sub(integerNum1, integerNum2) + "입니다.");
                } else if (num2 == 3) {
                    System.out.println("결과값은: " + con.mult(integerNum1, integerNum2) + "입니다.");
                } else if (num2 == 4) {
                    System.out.println("결과값은: " + con.div(integerNum1, integerNum2) + " 입니다.");
                } else {
                    System.out.println("숫자를 다시 입력하세요.");
                }

            } else if (num == 2) {
                System.out.println("1. 더하기 2. 빼기 3. 곱하기 4. 나누기");
                System.out.print("숫자를 입력하세요: ");
                int num2 = sc.nextInt();

                System.out.print("숫자 1 입력: ");
                double doubleNum1 = sc.nextDouble();

                System.out.print("숫자 2 입력: ");
                double doubleNum2 = sc.nextDouble();

                if (num2 == 1) {
                    System.out.println("결과값은: " + con.add(doubleNum1, doubleNum2));
                } else if (num2 == 2) {
                    System.out.println("결과값은: " + con.sub(doubleNum1, doubleNum2));
                } else if (num2 == 3) {
                    System.out.println("결과값은: " + con.mult(doubleNum1, doubleNum2));
                } else if (num2 == 4) {
                    System.out.println("결과값은: " + con.div(doubleNum1, doubleNum2));
                } else {
                    System.out.println("숫자를 다시 입력하세요.");
                }

            } else if (num == 3) {
                System.out.println("시스템을 종료합니다.");
                break;
            } else {
                System.out.println("숫자를 다시 입력해주세요.");
            }
        }
    }
}

Q2 선생님 풀이

package Day08;

import java.util.Scanner;

class Control {

    int[] intInput() {
        Scanner sc = new Scanner(System.in);
        int[] arry = new int[2];
        System.out.println("숫자 1 입력");
        arry[0] = sc.nextInt();
        System.out.println("숫자 2 입력");
        arry[1] = sc.nextInt();
        return arry;
    }

    double[] doubleInput() {
        Scanner sc = new Scanner(System.in);
        double[] arry = new double[2];
        System.out.println("숫자 1 입력");
        arry[0] = sc.nextDouble();
        System.out.println("숫자 2 입력");
        arry[1] = sc.nextDouble();
        return arry;
    }

    void print() {
        System.out.println("결과값  : ");
    }

    void add(int a, int b) {
        System.out.println(a + b);

    }

    void add(double a, double b) {
        System.out.println(a + b);
    }

    void mi(int a, int b) {
        System.out.println(a - b);
    }

    void mi(double a, double b) {
        System.out.println(a - b);
    }

    void mu(int a, int b) {
        System.out.println(a * b);
    }

    void mu(double a, double b) {
        System.out.println(a * b);
    }

    void div(int a, int b) {
        System.out.println(a / b);
    }

    void div(double a, double b) {
        System.out.println(a / b);
    }
}

public class Q2_1 {
    static void main() {

        Control c = new Control();
        Scanner sc = new Scanner(System.in);

        while (true) {
            System.out.println("1.정수 연산 2. 실수 연산 3. 종료 : ");
            int num = sc.nextInt();

            if (num == 1) {
                System.out.println("1.더하기 2. 빼기 3. 곱하기 4.나누기 : ");
                int op = sc.nextInt();
                if (op == 1) {
                    int[] arr = c.intInput();
                    c.add(arr[0], arr[1]);

                } else if (op == 2) {
                    int[] arr = c.intInput();
                    c.mi(arr[0], arr[1]);
                } else if (op == 3) {
                    int[] arr = c.intInput();
                    c.mu(arr[0], arr[1]);
                } else if (op == 4) {
                    int[] arr = c.intInput();
                    c.div(arr[0], arr[1]);
                } else {
                    System.out.println("잘못된 입력입니다");
                }
            } else if (num == 2) {
                System.out.println("1.더하기 2. 빼기 3. 곱하기 4.나누기 : ");
                int op = sc.nextInt();
                if (op == 1) {
                    double[] arr = c.doubleInput();
                    c.add(arr[0], arr[1]);

                } else if (op == 2) {
                    double[] arr = c.doubleInput();
                    c.mi(arr[0], arr[1]);
                } else if (op == 3) {
                    double[] arr = c.doubleInput();
                    c.mu(arr[0], arr[1]);
                } else if (op == 4) {
                    double[] arr = c.doubleInput();
                    c.div(arr[0], arr[1]);
                } else {
                    System.out.println("잘못된 입력입니다");
                }
            } else if (num == 3) {
                System.out.println("프로그램을 종료합니다");

            } else {
                System.out.println("잘못된 입력입니다");
            }
        }
    }
}

CastingTest

package Day08;

class X {
    int a;
}

class Y extends X {
    int b;
}

public class CastingTest {
    static void main() {
        X x = new Y(); // 업캐스팅
        // x.b = 20; X눈에 b는 안보인다.
        Y y = (Y)x; // 다운캐스팅
        y.b = 20;
    }
}

OverridingTest

package Day08;

class A1 {
    void abc() {
        System.out.println("ABC");
    }
}

class B1 extends A1 {
    @Override
    void abc() {
        System.out.println("DEF");
    }
}

public class OverridingTest {
    static void main() {
        A1 a1 = new A1();
        a1.abc();

        B1 b1 = new B1();
        b1.abc();

        A1 a2 = new B1();
        a2.abc();
    }
}

MethodAndCastingTest

package Day08;

class C1 {
    void abc() {

    }
}

class D1 extends C1{
    void abc() {
        System.out.println("D1");
    }
}

class E1 extends C1{
    void abc() {
        System.out.println("E1");
    }
}

class F1 extends C1{
    void abc() {
        System.out.println("F1");
    }
}

class G1 extends C1{
    void abc() {
        System.out.println("G1");
    }
}

class H1 extends C1{
    void abc() {
        System.out.println("H1");
    }
}

class X1 extends C1{
    void run(C1 c1) {
        c1.abc();
    }
}

public class MethodAndCastingTest {
    static void main() {
        D1 d1 = new D1();
        E1 e1 = new E1();

        X1 x1 = new X1();

        x1.run(d1);
        x1.run(e1);
    }
}

Q3 선생님 풀이

package Day08;

class Dino{
    int m;
    int kg;
    void talk(){
        System.out.println("나는 공룡이다.");
    }
}

class Trano1 extends Dino{
    Trano1(int m, int kg){
        super.m = m;
        super.kg = kg;
    }
    void talk(){
        System.out.println("나는 키 "+m+"m 몸무게 "+kg+"톤인 티라노사우르스다.");
    }
}
class Trica extends Dino{
    Trica(int m, int kg){
        super.m = m;
        super.kg = kg;
    }
    void talk(){
        System.out.println("나는 키 "+m+"m 몸무게 "+kg+"톤인 트리케라톱스다.");
    }
}
class Bugung extends Dino{
    Bugung(int m, int kg){
        super.m = m;
        super.kg = kg;
    }
    void talk(){
        System.out.println("나는 키 "+m+"m 몸무게 "+kg+"톤인 부경사우르스다.");
    }
}


public class Q3_1 {

    static void shout(Dino dino){
        dino.talk();
    }

    static void main() {
        Trano1 trano = new Trano1(2,1);
        Trica trica = new Trica(3,2);
        Bugung bu = new Bugung(5,3);

        shout(trano);
        shout(trica);
        shout(bu);

    }

}

개인공부

Q1

package Practice4;

/*
    RPG 전투 게임 2 ⚔️

    - Player: 이름, 체력, 공격력
    - Monster: 이름, 체력, 공격력

    Player
    - attack(Monster monster)
      → 몬스터 체력 감소
      → 체력이 0 이하이면 "쓰러졌습니다." 출력

    Monster
    - attack(Player player)
      → 플레이어 체력 감소
      → 체력이 0 이하이면 "쓰러졌습니다." 출력

    main
    - Player와 Monster 객체 생성
    - 서로 번갈아 공격하기

    제한
    - 배열 X
    - Random X
    - 객체를 매개변수로 전달하기
*/

class Player {
    String pName;
    int pHp;
    int pPower;

    Player(String pName, int pHp, int pPower) {
        this.pName = pName;
        this.pHp = pHp;
        this.pPower = pPower;
    }

    void attack(Monster monster) {
        monster.mHp = monster.mHp - pPower;
        System.out.println(pName + "이 " + monster.mName + "를 공격했다! 🏹");
        if (monster.mHp <= 0) {
            System.out.println(monster.mName + "가 쓰러졌습니다...");
        } else {
            System.out.println("📢 " + monster.mName + "의 체력: " + monster.mHp);
        }
    }
}

class Monster {
    String mName;
    int mHp;
    int mPower;

    Monster(String nName, int mHp, int mPower) {
        this.mName = nName;
        this.mHp = mHp;
        this.mPower = mPower;
    }

    void attack(Player player) {
        player.pHp = player.pHp - mPower;
        System.out.println(mName + "가 " + player.pName + "이를 공격했다! 💣");
        if (player.pHp <= 0) {
            System.out.println(player.pName + "가 쓰러졌습니다...");
        } else {
            System.out.println("📢 " + player.pName + "의 체력: " + player.pHp);
        }
    }
}

public class Q1 {
    static void main() {

        Player p1 = new Player("밥풀", 100, 30);
        Monster m1 = new Monster("대장 고양이", 100, 15);

        p1.attack(m1);
        System.out.println();
        m1.attack(p1);
        System.out.println();
        p1.attack(m1);
        System.out.println();
        m1.attack(p1);
        System.out.println();
        p1.attack(m1);
        System.out.println();
        m1.attack(p1);
        System.out.println();
        p1.attack(m1);
    }
}

Q2

package Practice4;

/*
    RPG 전투 게임 3

    [Player]
    - 필드: 이름, 현재 체력(hp), 최대 체력(maxHp), 공격력(power)
    - attack(Monster monster)
      → 몬스터 체력 감소
    - heal(int amount)
      → 체력 회복
      → 최대 체력을 넘으면 최대 체력으로 설정

    [Monster]
    - 필드: 이름, 체력(hp)
    - 생성자로 값 초기화

    [main]
    - Player와 Monster 객체 생성
    - 몬스터를 2번 공격
    - 플레이어 체력 40 회복
    - 현재 체력 출력

    제한
    - 배열 X
    - Random X
    - 객체를 매개변수로 전달하기
*/

class Player1 {
    String name;
    int hp;
    int maxHp = 100;
    int power;

    Player1(String name, int hp, int maxHp, int power) {
        this.name = name;
        this.hp = hp;
        this.maxHp = maxHp;
        this.power = power;
    }

    void attack(Monster1 monster1) {
        monster1.hp = monster1.hp - power;
        System.out.println(name + "가 " + monster1.name + "를 공격하였습니다! ⚔\uFE0F");
        if (monster1.hp <= 0) {
            System.out.println(monster1.name + "이 쓰러졌습니다...");
        } else {
            System.out.println(" 📣 " + monster1.name + "의 현재 체력: " + monster1.hp);
        }
    }

    void heal(int amount) {
        hp = hp + amount;
        System.out.println("✨체력을 " + amount + "만큼 회복하였습니다✨");
        if (hp >= maxHp) {
            hp = 100;
            System.out.println("최대 체력입니다.");
            System.out.println("더이상 회복할 수 없습니다.");
        } else {
            System.out.println("현재 체력: " + hp);
        }
    }
}

class Monster1 {
    String name;
    int hp;
    int power;

    Monster1(String name, int hp, int power) {
        this.name = name;
        this.hp = hp;
        this.power = power;
    }

    void attack(Player1 player1) {
        player1.hp = player1.hp - power;
        System.out.println(name + "가 " + player1.name + "를 공격하였습니다! 🔪");
        if (player1.hp <= 0) {
            System.out.println(player1.name + "가 쓰러졌습니다...");
        } else {
            System.out.println(" 📣 " + player1.name + " 의 현재 체력: " + player1.hp);
        }
    }
}

public class Q2 {
    static void main() {
        Player1 player1 = new Player1("밥풀이", 100, 100, 30);
        Monster1 monster1 = new Monster1("가오리 불량배", 150, 20);

        player1.attack(monster1);
        player1.attack(monster1);

        monster1.attack(player1);
        monster1.attack(player1);

        player1.heal(40);

    }
}

마무리

오늘도 자바공부 완

0개의 댓글