티스토리 뷰

반응형

 

Day 4 챌린지를 시작해보자.

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

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

 

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

 

우선 첫번째는 이전 시간에 만든 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;
    }

    // Getters and Setters
    public int getMaxSpeed() {
        return this.maxSpeed;
    }

    public void setMaxSpeed(int newMaxSpeed) {
        this.maxSpeed = newMaxSpeed;
    }

    public int getMinSpeed() {
        return this.minSpeed;
    }

    public double getWeight() {
        return this.weight;
    }

    public boolean getIsTheCarOn() {
        return this.isTheCarOn;
    }



    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 upgradeMaxSpeed() {
        setMaxSpeed(getMaxSpeed() + 10);
    }

    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
 */

/*
Day 4
GETTERS : GET VALUES OF OUT PROPERTIES
SETTERS : SET VALUES OF OUT PROPERTIES
PARAMETER : A VARIABLE IN A FUNCTION THAT REFERS TO INPUT DATA

BOOLEAN OPERATORS!
AND, OR, NOT
 */

 

 

다음으로 해본 실습은 Computer가 랜덤한 숫자를 생성하면

맞추는 게임이었다.

 

GuessTheNumber 소스코드를 보자.

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

public class GuessTheNumber {
    int theNUMBER;
    int max;
    Scanner scanner = new Scanner(System.in);

    public GuessTheNumber() {
        Random rand = new Random();
        max = 100;
        theNUMBER = Math.abs(rand.nextInt()) % (max + 1); // 0~100
    }

    public void play() {
        while (true) {
            int move = scanner.nextInt();
            if (move > theNUMBER) {
                System.out.println("Your number is too big");
            } else if (move < theNUMBER) {
                System.out.println("Your number is too small");
            } else {
                System.out.println("YOU GOT IT BRO");
                break;
            }
        }
    }

    public static void howBigIsMyNumber(int x) {
        if (x >= 0 && x <= 10) {
            System.out.println("Our number is pretty small");
        } else if (x >= 11 && x <= 100) {
            System.out.println("Our number is pretty big");
        } else {
            System.out.println("Our number is out of range");
        }
    }

    public static void main(String[] args) {
//        howBigIsMyNumber(0);
//        howBigIsMyNumber(11);

        GuessTheNumber guessTheNumber = new GuessTheNumber();
        System.out.println("Welcome to my game. Try and guess my impossible" + " number. It's between 0 and " + guessTheNumber.max + " inclusive. Just type a number to get Started.");
        guessTheNumber.play();
    }
}

/*
HOW TO PLAY :
1. COMPUTER PICKS RANDOM #
2. USER GUESSES
3. COMPUTER GIVES CLUES
4. WE GUESS UNTIL WE GET IN
 */

 

컴퓨터에게 random 숫자를 생성시키고

if문으로 random 숫자보다 내가 입력한 값이 클때, 작을 때, 같을 때 조건을 줘서

진행하는 게임이다.

 

 


오늘 수업에서는

if문의 조건과 getter, setter에 대해 배웠다.

 

 

 

영어 수업이라 이해하기 어려웠을텐데

한국어로 된 hackerrank 자료가 잘 없는 것 같아

소스 코드를 공유한다.

많은 분들이 도움되었으면 좋겠다. 😊

 

마찬가지로 궁금한 것이 있으면 댓글 남겨주길 바란다!!

 

 

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

오늘 하루도 열공하자. 😆

반응형