티스토리 뷰

반응형

 

오늘은 Day2 챌린지를 시작해보자.

 

우선 오늘 처음 알게된 사실인데

문제 가장 위에 Tutorial 강의가 있어서

강의 먼저 수강했다.

 

tutorial 강의 위치

 

이번 포스팅에서는 강의 내용을 정리하고,

다음 포스팅에서 코드를 진행하겠다.

(Day 2의 소스코드가 궁금한 사람은 여기 클릭(필자의 다음 포스팅으로 이동))

 

 

우선 코드부터 확인하자.

/*
VARIABLES:
1. DECLARE
2. ALLOCATE
3. INITIALIZE

INSTANCE METHODS & PROPERTIES

CONSTRUCTORS:
1. DEFAULT
2. CUSTOM
 */
public class Car {
    int maxSpeed = 100;
    int minSpeed = 0;

    double weight = 4079;

    boolean isTheCarOn = false;
    char condition = 'A';
    String nameOfCar = "Lucy";

    double maxFuel = 16;
    double currentFuel = 8;
    double mpg = 26.4;

    int numberOfPeopleInCar = 1;

    // f(x) = x + 1;
    // Say x = 5;
    // f(5) = 5 + 1 = 6;
    public Car(int customMaxSpeed, double customWeight, boolean customIsTheCarOn) { // 클래스 이름(public class Car)이랑 같아야 함.
        maxSpeed = customMaxSpeed;
        weight = customWeight;
        isTheCarOn = customIsTheCarOn;
    }

    public void printVariables() {
        System.out.println("This is the maxSpeed " + maxSpeed);
        System.out.println(minSpeed);
        System.out.println(weight);
        System.out.println(isTheCarOn);
        System.out.println(condition);
        System.out.println(nameOfCar);
        System.out.println(numberOfPeopleInCar);
    }

    public void upgradeMinSpeed() {
        minSpeed = maxSpeed;
        maxSpeed = maxSpeed + 1;
    }

    public void getIn() {
        numberOfPeopleInCar++;
    }

    public void getOut() {
        numberOfPeopleInCar--;
    }

    public double howManyMilesTillOutOfGas() {
        return currentFuel * mpg;
    }

    public double maxMilesPerFillUp() {
        return maxFuel * mpg;
    }

    public static void main(String[] args) {
//        Car familyCar = new Car();
//        System.out.println("Family's Car:");
//        familyCar.printVariables();
//        familyCar.upgradeMinSpeed();
//        familyCar.printVariables();

        System.out.println("Birthday Car");
        Car birthdayPresent = new Car(500, 5000.545, true);
        birthdayPresent.printVariables();
        birthdayPresent.getIn();
        birthdayPresent.getIn();
        birthdayPresent.getIn();
        System.out.println("Miles Left : " + birthdayPresent.howManyMilesTillOutOfGas());
        System.out.println("MaxMiles : " + birthdayPresent.maxMilesPerFillUp());
        System.out.println("Birthday Car v2");
        birthdayPresent.printVariables();
        birthdayPresent.getOut();
        System.out.println("Birthday Car v2");
        birthdayPresent.printVariables();
    }

}

 

코드는 딱히 이해하는데 어려움은 없을 것이다.

 

혹시 몰라 실행결과도 첨부해 놓는다.

Birthday Car
maxSpeed : 500
minSpeed = 0
weight = 5000.545
isTheCarOn = true
condition = A
nameOfCar = Lucy
numberOfPeopleInCar = 1
===============
Left : 211.2
Max : 211.2
Birthday Car v2
maxSpeed : 500
minSpeed = 0
weight = 5000.545
isTheCarOn = true
condition = A
nameOfCar = Lucy
numberOfPeopleInCar = 4
===============
Birthday Car v2
maxSpeed : 500
minSpeed = 0
weight = 5000.545
isTheCarOn = true
condition = A
nameOfCar = Lucy
numberOfPeopleInCar = 3
===============

 

 

오늘 강의의 핵심은

✅ 변수

1. 선언

2. 할당

3. 식별

 

✅ 인스턴스 메서드 & 속성

 

✅ 생성자

1. 디폴트 생성자

2. 커스텀 생성자

 

이 정도가 키워드 인 것 같다.

 

📢 Day 2 소스코드 보러 가기(클릭)

 

 


 

그럼 다음 포스팅에서 Day2 코드 리뷰로 만나자.

오늘 하루도 열공하자. 🔥

 

반응형