public class Program {
public static void main(String[] args) {
System.out.println(1);
System.out.println(2);
System.out.println(3);
}
}
위 코드는 화면에 1, 2, 3을 순서대로 출력한다.
- 이런 경우 컴퓨터 프로그래밍을 이용해 순차적인 실행으로, 사람이 잘 하지 못하는 일을 기계에게 위임하고, 자동화할 수 있다.
- 그리고 컴퓨터 프로그래밍을 할 수 있게하는 언어 중 가장 유명한 것이 자바이다.
컴퓨터 프로그래밍을 통해 사람이 하기 어려운 일들을 기계에게 위임하여 자동화 할 수 있음을 알게되었다. 이번 강의에서는 IoT 모의 프로그램을 만들어보며 이러한 기능을 체감해 보겠다!
IoT 모의 프로그램 제작을 위해 외부 패키지를 설치해보자.
패키지 설치링크
https://github.com/egoing/java-iot
https://gitlab.com/egoing/java-iot
https://bitbucket.org/egoing/java-iot/src/master/이 중 하나의 링크를 통해 org.opentutorials.iot 패키지를 다운받으면 된다.
org-opentutorials-iot
폴더 안의 자바 파일을 확인할 수 있다.Programming 프로젝트에 org.opentutorials.iot 패키지를 설치하기 위해,
org 폴더를 이클립스의 Navigator 뷰의 Programming 프로젝트로 드래그 앤 드랍 한다.
(Package Explorer 뷰에서도 가능함)
Copy files and folders
옵션을 선택하여 OK버튼을 클릭한다.우리가 집에갈 때 일어나는 일들을 정리해보자.
이러한 기능을 이전 시간에 설치했던 거짓 IoT 패키지를 이용하여 만들어 볼것이다.
import org.opentutorials.iot.Elevator;
import org.opentutorials.iot.Lighting;
import org.opentutorials.iot.Security;
public class OkJavaGoInHome {
public static void main(String[] args) {
String id = "JAVA APT 507";
// Elevator call
Elevator myElevator = new Elevator(id);
myElevator.callForUp(1);
// Security off
Security mySecurity = new Security(id);
mySecurity.off();
// Light on
Lighting hallLamp = new Lighting(id+" / Hall Lamp");
hallLamp.on();
Lighting floorLamp = new Lighting(id+" / floorLamp");
floorLamp.on();
}
}
import org.opentutorials.iot.Elevator; // org.opentutorials.iot.Elevator .... Elevator myElevator = new Elevator(id);
import 구문
을 이용하면org.opentutorials.Elevator
같이 입력해야 하는 구문을Elevator
로 단순하게 할 수 있다.String id = "JAVA APT 507";
JAVA APT 507
과 같이 반복적으로 사용되는 값은변수
로 지정하여 재사용할 수 있다.- 의미있는 이름(id)을 지정하여 코드의 가독성을 높일 수 있다.
Elevator myElevator = new Elevator(id);
- id라는 변수는 JAVA APT 507을 의미한다.
- myElevator라는 변수는 JAVA APT 507의 엘리베이터를 의미한다.
- myElevator는 자료형이 Elevator여야 한다.
myElevator.callForUp(1);
- 엘리베이터를 1층으로 부르는데, 위로 올라가는 엘리베이터를 부른다는 뜻의 코드이다.
Security mySecurity = new Security(id);
- id라는 변수는 JAVA APT 507을 의미한다.
- mySecurity라는 변수는 JAVA APT 507의 보안을 의미한다.
- mySecurity는 자료형이 Security여야 한다.
mySecurity.off();
- 현관문의 보안을 해제한다는 뜻의 코드이다.
Lighting hallLamp = new Lighting(id+" / Hall Lamp"); Lighting floorLamp = new Lighting(id+" / floorLamp");
- id라는 변수는 JAVA APT 507을 의미한다.
- hallLamp라는 변수는 JAVA APT 507의 hallLamp를 의미한다.
- floorLamp라는 변수는 JAVA APT 507의 floorLamp를 의미한다.
- hallLamp, floorLamp는 자료형이 Lighting여야 한다.
hallLamp.on(); floorLamp.on();
- 램프의 불을 켠다는 뜻의 코드이다.
패키지를 이용해 모의 IoT프로그램을 자바로 만들어 보았다.
이로써 우리가 집에 갈때 일어나는 일들을 자동화 할 수 있음을 확인해볼 수 있었다.
부스트코스 쉽게 배우는 자바 1
https://www.boostcourse.org/cs126