풀스택 개발자 과정 43일차

너구·2026년 7월 8일

풀스택 성장과정

목록 보기
46/79

ClassTest

package Day06;

class A {
    int a;
    void print() {
        System.out.println("a는 " + a + "입니다.");
    }
}

public class ClassTest {
    static void main() {

        A a = new A();
        a.a = 10;
        a.print();

        A a1 = new A();
        a.print();
    }
}

Q1

package Day06;

class Pencil {
    String color;

    void print() {
        System.out.println("쓰는 기능은 " + color + "으로 적습니다.");
    }
}

class Eraser {
    String name;
    void print() {
        System.out.println("지우는 기능은 " + name + "의 제품으로 지웁니다.");
    }
}

class Pen {
    String penName;
    String penColor;

    void print() {
        System.out.println("펜을 " + penName + " 제품의 " + penColor + "으로 씁니다.");
    }
}

public class Q1 {
    static void main() {

        Pencil red = new Pencil();
        Pencil blue = new Pencil();
        Pencil orange = new Pencil();
        Pencil yellow = new Pencil();

        red.color = "빨간색";
        blue.color = "파란색";
        orange.color = "주황색";
        yellow.color = "노란색";

        Eraser jamjari = new Eraser();
        Eraser moning = new Eraser();

        jamjari.name = "잠자리";
        moning.name = "모나미";

        Pen monami = new Pen();
        Pen jet = new Pen();
        Pen waterman = new Pen();

        monami.penName = "모나미";
        jet.penName = "제트스트림";
        waterman.penName = "워터맨";

        monami.penColor = "검정색";
        jet.penColor = "초록색";
        waterman.penColor = "베이비블루";

        red.print();
        blue.print();
        orange.print();
        yellow.print();

        System.out.println();

        jamjari.print();
        moning.print();

        System.out.println();

        monami.print();
        jet.print();
        waterman.print();
    }
}

클래스 객체화 연습

ConstructTest

package Day06;

class C {
    C(int a, int b){
        for (int i = 0; i < a; i++) {
            System.out.println("안녕");
        }
        for (int j = 0; j < b; j++) {
            System.out.println("하세요");
        }
    }
}

public class ConstructTest {
    static void main() {
        // C c = new C(); // 디폴트 생성자
        C c1 = new C(5, 6);
    }
}

HasTast

package Day06;

class D{
    int a;
    String str;
    E e; // has
}

class E{
    int k;
}

class F{
    E e = new E(); // has
}

public class HasTest {
    static void main() {
        D d = new D();
        d.e.k = 20;
        F f = new F();
        f.e.k = 30;
        System.out.println(f.e.k);
    }
}

Q2

package Day06;

class Person1 {
    String firstCar;
    String secondCar;
    Car car1;
    Car car2;
}

class Person2 {
    String firstBike;
    String firstMotor;
    Bike bike1;
    Motorcycle motorcycle1;
}

class Car {
    String carColor;
    String carMaker;
    int carMax;

    void carRide(String carName) {
        System.out.println("첫번재 사람이 " + carName + carColor + "색 " + carMaker + " 브랜드, " + "최고시속 " + carMax + "인 차를 탑니다.");
    }
}

class Bike {
    String bikeMaker;
    String bikeOption;

    void bikeRide(String firstBike) {
        System.out.println("두번째 사람이 " + bikeMaker + " 브랜드, " + bikeOption + " 자전거를 탑니다.");
    }
}

class Motorcycle {
    String motorMaker;
    String motorColor;
    int motorMax;

    void motorRide(String firstMotor) {
        System.out.println("두번째 사람이 " + motorMaker + "브랜드, " + motorColor + " 색깔인 " + "최고시속 " + motorMax + "인 오토바이를 탑니다.");
    }
}

public class Q2 {
    static void main() {
        Car car1 = new Car();
        Car car2 = new Car();
        Car car3 = new Car();
        Car car4 = new Car();

        car1.carColor = "은";
        car2.carColor = "검정";
        car3.carColor = "하양";
        car4.carColor = "형광";
        car1.carMaker = "Ford";
        car2.carMaker = "BMW";
        car3.carMaker = "삼성";
        car4.carMaker = "현대";

        car1.carMax = 220;
        car2.carMax = 240;
        car3.carMax = 200;
        car4.carMax = 180;

        Bike bike1 = new Bike();
        Bike bike2 = new Bike();

        bike1.bikeMaker = "삼천리";
        bike2.bikeMaker = "산악용";
        bike1.bikeOption = "산악용";
        bike2.bikeOption = "접이식용";

        Motorcycle motorcycle = new Motorcycle();
        motorcycle.motorMaker = "대림";
        motorcycle.motorColor = "빨강";
        motorcycle.motorMax = 180;

        Person1 p1 = new Person1();
        p1.firstCar = "1번차, ";

        p1.car1 = car1;
        p1.car1.carRide(p1.firstCar);
        Person1 p2 = new Person1();

        p2.secondCar = "2번차, ";
        p2.car2 = car2;
        p2.car2.carRide(p2.secondCar);

        Person2 bikeP = new Person2();
        bikeP.firstBike = "첫번째 자전거";
        bikeP.bike1 = bike1;
        bikeP.bike1.bikeRide(bikeP.firstBike);

        Person2 motorP = new Person2();
        motorP.firstMotor = "첫번째 오토바이";
        motorP.motorcycle1 = motorcycle;
        motorP.motorcycle1.motorRide(motorP.firstMotor);
    }
}

개인공부

Q1

package Practice.Day6;

// 생성자를 이용해 Book 정의하기

class Book {
    String title;
    String author;

    Book(String title, String author) {
        this.title = title;
        this.author = author;
    }

    void printInfo() {
        System.out.println("책 제목: " + title);
        System.out.println("저자: " + author);
    }
}

public class Q1 {
    static void main() {

        Book book = new Book("자바의 정석", "남궁성");

        book.printInfo();
    }
}

Q2

package Practice.Day6;

// 생성자와 메서드를 통해 Student라는 객체를 설계하기

class Student{
    String name;
    int age;
    int score;

    Student(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    void printInfo() {
        System.out.println("이름: " + name + " 나이: " + age + " 점수: " + score);
    }

    boolean isPass() {
        if (score >= 60) {
            return true;
        }
        return false;
    }
}

public class Q2 {
    static void main() {
        Student s1 = new Student("철수", 20, 20);
        s1.printInfo();
        System.out.println(s1.isPass());
    }
}

Q3

package Practice.Day6;

// 은행 계좌 입출금 문제

class BankAccount {
    String owner;
    int balance;

    BankAccount(String owner, int balance) {
        this.owner = owner;
        this.balance = balance;
    }

    void deposit(int amount) {
        balance = balance + amount;
        System.out.println(amount + "원 입금되었습니다.");
        System.out.println("현재 잔액은 " + balance + "원 입니다.");
    }

    void withdraw(int amount) {
        if (balance < amount) {
            System.out.println("잔액 부족");
        } else {
            balance = balance - amount;
            System.out.println(amount + "원 출금하였습니다.");
            System.out.println("현재 잔액은 " + balance + "원 입니다.");
        }
    }

    void printInfo() {
        System.out.println("소유자: " + owner);
        System.out.println("잔액: " + balance + "원");
    }
}

public class Q3 {
    static void main() {

        BankAccount account = new BankAccount("철수", 10000);

        account.printInfo();

        account.deposit(5000);

        account.withdraw(3000);

        account.withdraw(1000000);

    }
}

Q4

package Practice2;

// 도서관 대출 문제

class Book {
    String title;
    String author;
    boolean borrow;

    Book(String title, String author) {
        this.title = title;
        this.author = author;
        this.borrow = false;
    }

    void isBorrow() {
        if (!borrow) {
            borrow = true;
            System.out.println("대여가 완료되었습니다.");
        } else {
            System.out.println("이미 대여중인 책입니다.");
        }
    }

    void bookReturn() {
        if (borrow) {
            borrow = false;
            System.out.println("반납이 완료되었습니다.");
        } else {
            System.out.println("이미 반납된 책입니다.");
        }
    }

    void printInfo() {
        System.out.println("책 제목: " + title + " 저자: " + author + " 대여상태: " + borrow);
    }
}

public class Q4 {
    static void main() {

        Book book = new Book("개미", "베르나르 베르베르");

        book.printInfo();

        book.isBorrow();

        book.isBorrow();

        book.bookReturn();
    }
}

Q5

package Practice2;

// 자동차 속도 up down 문제

class Car{
    String model;
    int speed;
    int maxSpeed;

    Car(String model, int speed, int maxSpeed) {
        this.model = model;
        this.speed = speed;
        this.maxSpeed = maxSpeed;
        this.speed = 0;
    }

    void speedUp(int num) {
        speed = speed + num;
        if (speed >= maxSpeed) {
            speed = maxSpeed;
        }
        System.out.println("현재 속도: " + speed);
    }

    void speedDown(int num) {
        speed = speed - num;
        if (speed <= 0) {
            speed = 0;
        }
        System.out.println("현재 속도: " + speed);
    }

    void printInfo() {
        System.out.println("모델명: " + model);
        System.out.println("현재 속도: " + speed);
        System.out.println("최고 속도: " + maxSpeed);
    }
}

public class Q5 {
    static void main() {

        Car car = new Car("포르쉐", 130, 300);

        car.printInfo();

        car.speedUp(30);

        car.speedDown(50);

        car.printInfo();
    }
}

마무리

class에 대해 배웠다. 메서드랑 클래스가 제일 재밌는 거 같다.
문제도 전에 좀 풀었었어서 꽤 순조롭게 풀었다.
그래도 아직 선생님이 출제해주시는 문제는 어렵다.
내일도 열심히 공부해야 될 거 같다.

0개의 댓글