티스토리 뷰

반응형

 

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

어제와 마찬가지로 Tutorial 강의를 먼저 수강하고

다음 포스팅에서 코드를 소개하고 설명하겠다.

 

우선 강의 실습 코드부터 확인하자.

 

우선 첫번째로는 이전 시간에 만든 Car클래스에

메서드를 추가하는 코드였다.

 

Car 클래스부터 확인해보자.

/*
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;
    int maxNumberOfPeopleInCar = 6;

    // f(x) = x + 1;
    // Say x = 5;
    // f(5) = 5 + 1 = 6;

    public Car() {

    }

    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() {
        // if there aren't too many people in the car
        if (numberOfPeopleInCar < maxNumberOfPeopleInCar) {
            // then someone can get in
            numberOfPeopleInCar++;
            System.out.println("Someone got in");
        }else {
            // otherwise print out the fact the call is full
            System.out.println("The Car is Full!" + numberOfPeopleInCar + " = " + maxNumberOfPeopleInCar);
        }
    }

    public void getOut() {
//        if there's people in the car
        if (numberOfPeopleInCar > 0) {
            // then tell one person to get out
            numberOfPeopleInCar--;
        }else {
            // otherwise no one can get out and we'll print that.
            System.out.println("No one is in the car" + numberOfPeopleInCar);
        }
    }

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

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

    public void turnTheCarOn() {
        // If the car isn't on...
        if (!isTheCarOn) {
            // turn it on
            isTheCarOn = true; // =와 ==차이 설명하기(블로그)
        } else {
            // otherwise print the fact it's on
            System.out.println("The Car is already on + " +  isTheCarOn);
        }
    }

    public static void main(String[] args) {
        Car tommyCar = new Car();
        tommyCar.getOut();
        tommyCar.getOut();
        tommyCar.getIn();
        tommyCar.getIn();
        tommyCar.getIn();
        tommyCar.getIn();
        tommyCar.getIn();
        tommyCar.getIn();
        tommyCar.getIn();
        tommyCar.turnTheCarOn();
        tommyCar.turnTheCarOn();
    }

}

/*
Day 3
CLASS : BLUEPRINT WITH TOOLS (PROPERTIES * METHODS)
INSTANCE : AN OBJECT CREATED FROM THE BLUEPRINT

IF STATEMENT :
-- IF THE CAR IS ALREADY ON -> DO NOTHING
-- IF THE CAR IS NOT ON -> SET ISTHECARON TO TRUE
 */

 

 

다음으로는 CoinToss라는 동전던지기 게임?을 만들었다.

(사실 그냥 random수 나오게 하는거)

 

CoinToss 코드도 살펴보자.

import java.util.Random;

public class CoinToss {
    public String tossACoin() {
        Random rand = new Random();
        int toss = Math.abs(rand.nextInt()) % 2;
        if(toss == 0) {
            return "HEADS";
        }else {
            return "TAILS";
        }
    }

    public static void main(String[] args) {
        CoinToss game = new CoinToss();
        System.out.println(game.tossACoin());
        System.out.println(game.tossACoin());
        System.out.println(game.tossACoin());
        System.out.println(game.tossACoin());
        System.out.println(game.tossACoin());
        System.out.println(game.tossACoin());

    }
}

 

 

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

 

 


오늘 수업에서는

IF문에 대해 배웠다.

 

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

오늘 하루도 열심히 공부하자. 😆

반응형