본문 바로가기
Java_language

클래스 만들기 연습문제

by 헬로제이콥 2025. 11. 26.

🧑‍🎓 Student 클래스 작성 (Java 버전)

이 예제는 Java에서 클래스의 필드, 생성자, 메서드를 정의하는 가장 기본적인 구조를 보여줍니다.


❓ 문제 (Quiz)

다음 요구사항을 만족하는 간단한 Student 클래스를 Java로 작성하세요.

Student 클래스 작성:

  • 필드: name (String), grade (int), score (int)
  • 생성자: 학생 객체를 만들 때, **이름(name)**과 **학년(grade)**을 매개변수로 받아 필드를 설정합니다. **점수(score)**는 처음 0점으로 초기 설정해야 합니다.
  • 메서드:
    • setScore(int newScore): 학생의 점수 필드(score)를 새로운 점수(newScore)로 업데이트하는 기능.
    • showInfo(): 학생의 이름, 학년, 점수를 모두 출력하여 보여주는 기능.

💡 정답 (Answer)

1. Student 클래스 코드 작성 (Student.java)

Java
 
public class Student {
    // 1. 🛠️ 필드 정의
    String name;
    int grade;
    int score; 

    // 2. 🌟 생성자: 객체 생성 시 이름과 학년을 받아 초기 설정합니다.
    public Student(String name, int grade) {
        this.name = name;     // 이름 설정
        this.grade = grade;   // 학년 설정
        this.score = 0;       // 점수 필드를 0으로 초기 설정
        System.out.println("✅ " + this.name + " 학생 객체가 생성되었습니다! (학년: " + this.grade + "학년)");
    }

    // 3. 📝 메서드: 점수를 설정하는 기능
    public void setScore(int newScore) {
        this.score = newScore;
        System.out.println("📝 " + this.name + " 학생의 점수가 " + newScore + "점으로 설정되었습니다.");
    }

    // 4. ℹ️ 메서드: 정보를 보여주는 기능
    public void showInfo() {
        System.out.println("--------------------");
        System.out.println("🧑‍🎓 이름: " + this.name);
        System.out.println("🏫 학년: " + this.grade + "학년");
        System.out.println("💯 점수: " + this.score + "점");
        System.out.println("--------------------");
    }
}

2. 클래스 사용 예시 (StudentMain.java)

Java
 
public class StudentMain {
    public static void main(String[] args) {
        // 1. 학생 객체 생성
        Student student1 = new Student("김철수", 4);
        Student student2 = new Student("박영희", 6);
        System.out.println(); // 줄 바꿈

        // 2. 메서드를 사용해 점수 설정
        student1.setScore(85);
        student2.setScore(92);
        System.out.println(); // 줄 바꿈

        // 3. 메서드를 사용해 정보 출력
        student1.showInfo();
        student2.showInfo();
    }
}


📺 RemoteControl 클래스 작성 (Java 버전)

이 예제는 메서드를 호출하여 객체의 **상태(필드)**를 변경하고 유지하는 객체 지향의 핵심 개념을 연습합니다.


❓ 문제 (Quiz)

다음 요구사항을 만족하는 간단한 RemoteControl (리모컨) 클래스를 Java로 작성해 보세요.

RemoteControl 클래스 작성:

  • 필드: channel (int), volume (int)
  • 생성자: 리모컨 객체를 만들 때, **채널(channel)**은 1번으로, **볼륨(volume)**은 5로 초기 설정합니다.
  • 메서드:
    • channelUp(): 현재 채널을 1씩 증가시키는 기능.
    • volumeUp(): 현재 볼륨을 1씩 증가시키는 기능.
    • showStatus(): 현재 채널볼륨 상태를 출력하여 보여주는 기능.

💡 정답 (Answer)

1. RemoteControl 클래스 코드 작성 (RemoteControl.java)

Java
 
public class RemoteControl {
    // 1. 🛠️ 필드 정의
    int channel;
    int volume;

    // 2. 🌟 생성자: 채널 1, 볼륨 5로 초기 설정합니다.
    public RemoteControl() {
        this.channel = 1;   // 채널 필드를 1로 시작
        this.volume = 5;    // 볼륨 필드를 5로 시작
        System.out.println("✅ 새 리모컨 객체가 준비되었습니다.");
    }

    // 3. ➕ 메서드: 채널을 1 올리는 기능
    public void channelUp() {
        this.channel++; // this.channel = this.channel + 1; 와 동일
        System.out.println("📡 채널이 " + this.channel + "번으로 변경되었습니다.");
    }

    // 4. 🔊 메서드: 볼륨을 1 올리는 기능
    public void volumeUp() {
        this.volume++; // this.volume = this.volume + 1; 와 동일
        System.out.println("🎶 볼륨이 " + this.volume + "로 커졌습니다.");
    }

    // 5. ℹ️ 메서드: 현재 상태를 보여주는 기능
    public void showStatus() {
        System.out.println("--------------------");
        System.out.println("📺 현재 채널: " + this.channel + "번");
        System.out.println("📢 현재 볼륨: " + this.volume);
        System.out.println("--------------------");
    }
}

2. 클래스 사용 예시 (RemoteControlMain.java)

Java
 
public class RemoteControlMain {
    public static void main(String[] args) {
        // 1. 리모컨 객체 생성
        RemoteControl myRemote = new RemoteControl();
        System.out.println();

        // 2. 초기 상태 확인
        myRemote.showStatus();
        System.out.println();

        // 3. 객체 사용: 채널과 볼륨 올리기
        myRemote.channelUp();
        myRemote.channelUp(); // 채널 두 번 올리기
        myRemote.volumeUp();  // 볼륨 한 번 올리기
        System.out.println();

        // 4. 변경된 상태 확인
        myRemote.showStatus();
    }
}

이 두 예제를 통해 Java에서 **객체 지향 프로그래밍(OOP)**의 기본 요소인 클래스, 필드, 생성자, 메서드를 구성하고 사용하는 방법을 익힐 수 있습니다.