자바 IoT 프로그래밍 (import), 파라미터 (paramter), 아규먼트(argument)
본문 바로가기
programming/Java

자바 IoT 프로그래밍 (import), 파라미터 (paramter), 아규먼트(argument)

by 코딩하는 핑가 2020. 11. 4.
반응형

* 본 포스팅은 생활코딩님의 자바1 수업을 학습하면서 정리한 내용입니다.

* 주소

- opentutorials.org/module/4294

- www.youtube.com/playlist?list=PLuHgQVnccGMAIluRRVsC1e79ri-dwnBmR

 

< File - new - java project > Programming 패키지 생성

1. class Program 생성

public class Program {

	public static void main(String[] args) {
		System.out.println(1);
		System.out.println(2);
		System.out.println(3);

	}

}

- 프로그램 : 시간의 순서에 따라 어떤 일이 일어나는 것

- JAVA는 자동화를 가능하게 만들어주는 언어 중 하나

 

- 이 수업은 만들어놓은 라이브러리를 활용하여 수업을 진행

- IoT 라이브러리 설치

github.com/egoing/java-iot

- 압축 풀고 org 폴더 복사 > Navigator의 Programming 패키지 클릭 후 붙여넣기

- Package Explorer에 org.opentutorials.lot 생성된 것을 확인할 수 있음

 

2. class OkJavaGoInHome 생성

Package는 지우고 생성하기

- 자동화 프로그램 만들기

< 내용 >

홍길동은 집 안에서 엘리베이터를 호출하고 집 안에서 밖으로 나갈 때 보안 경보가 자동으로 꺼지는 프로그램을 만들고 싶어한다.

추가로 엘리베이터를 탔을 때 엘리베이터 조명이 자동으로 켜졌으면 좋겠고,

1층을 도착했을 때 1층 불이 켜지는 기능도 추가하고 싶다.

import org.opentutorials.iot.Elevator;
import org.opentutorials.iot.Lighting;
import org.opentutorials.iot.Security;;

public class OkJavaGolnHome {

	public static void main(String[] args) {
		
		String id = "JAVA APT 507";
		// Elevator call
		Elevator myElevator = new Elevator(id);
		myElevator.callForUp(1); // 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 + " / floor Lamp");
		floorLamp.on();
		
		

	}

}

- 엘리베이터 호출 ( Elevator call )

- 보안 끄기 ( Security off )

- 불 켜기 ( Light on )

- import org.opentutorials.iot.(); : 미리 만들어 놓은 라이브러리를 사용한다. ( import 시킨다라고 함 )

- Elevator : 데이터 타입, myElevator : 변수명

- myElevator 변수는 반드시 Elevator 데이터 타입에 해당하는 데이터만 볼 수 있다.

 

<실행화면>

JAVA APT 507 -> Elevator callForUp stopFloor : 1
JAVA APT 507-> Security off
JAVA APT 507 / Hall Lamp -> Lighting on
JAVA APT 507 / floor Lamp -> Lighting on

 

2. class OkJavaGoInHome 복사 붙이기해서 OkJavaGoInHomeInput 생성

- 변수 값을 고정된 값이 아닌 사용자가 직접 입력해서 자동화 프로그램 만들기

" java popup input text swing " 검색

stackoverflow.com/questions/17180023/how-do-i-make-a-popup-window-with-just-a-textfield

위의 링크의 답변에 해당하는 코드 적용

- ID 직접 입력

import javax.swing.JOptionPane; // 추가됨

import org.opentutorials.iot.Elevator;
import org.opentutorials.iot.Lighting;
import org.opentutorials.iot.Security;;

public class OkJavaGolnHomeInput{

	public static void main(String[] args) {
		
		String id = JOptionPane.showInputDialog("Enter a ID");
		
		// 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 + " / floor Lamp");
		floorLamp.on();
		
		

	}

}

<실행화면>

Input Box에 'Busan' 입력 후 결과

Busan -> Elevator callForUp stopFloor : 1
Busan -> Security off
Busan / Hall Lamp -> Lighting on
Busan / floor Lamp -> Lighting on

- 이 코드 수정으로 코드를 고치지 못하는 사용자도 이 프로그램을 사용할 수 있게 되었다.

 

- ID, 밝기 조절 직접 입력

import javax.swing.JOptionPane;

import org.opentutorials.iot.DimmingLights;
import org.opentutorials.iot.Elevator;
import org.opentutorials.iot.Lighting;
import org.opentutorials.iot.Security;;

public class OkJavaGolnHomeInput{

	public static void main(String[] args) {
		
		String id = JOptionPane.showInputDialog("Enter a ID");
		String bright = JOptionPane.showInputDialog("Enter a Bright level");
		
		// Elevator call
		Elevator myElevator = new Elevator(id);
		myElevator.callForUp(1); // 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 + " / floor Lamp");
		floorLamp.on();
		
		DimmingLights moodLamp = new DimmingLights(id + "moodLamp");
		moodLamp.setBright(Double.parseDouble(bright)); // n퍼센트 밝기 켜기
		moodLamp.on();
		
		

	}

}

- DimmingLights import 후 원래 코드

String bright = JOptionPane.showInputDialog("Enter a Bright level");

DimmingLights moodLamp = new DimmingLight(id+ "moodLamp");
moodLamp.setBright(bright); // 변경할 부분
moodLamp.on();

DimmingLights의 데이터 타입은 Double형이다. String bright를 가져오기 위해선 casting 작업이 필요하다.

 

<실행화면>

<실행결과>

JAVA APT -> Elevator callForUp stopFloor : 1
JAVA APT -> Security off
JAVA APT / Hall Lamp -> Lighting on
JAVA APT / floor Lamp -> Lighting on
JAVA APT moodLamp -> DimmingLights bright : 10.0
JAVA APT moodLamp -> Lighting on

 

- 프로그램 상에서 미리 입력해놓은 ID, bright 값 불러오기 ( 변수에 직접 입력 X )

- RUN 옆의 작은 세모 클릭 > Run Configurations 클릭

- 좌측은 현재 내가 실행중인 애플리케이션을 보여주고 있다.

- 애플리케이션 선택 후 우측 화면에서 Arguments 클릭

- Program arguments에 내가 자동으로 넣어 줄 값을 차례로 입력한다.

- "Java APT 507" "15.0" 입력

- Apply > Run

* 아직 소스코드를 수정하지 않았기 때문에 에러가 나는 것은 당연하다.

 

< 소스코드 >

import javax.swing.JOptionPane;

import org.opentutorials.iot.DimmingLights;
import org.opentutorials.iot.Elevator;
import org.opentutorials.iot.Lighting;
import org.opentutorials.iot.Security;;

public class OkJavaGolnHomeInput{

	public static void main(String[] args) {
		
		String id = args[0]; //JOptionPane.showInputDialog("Enter a ID");
		String bright = args[1]; //JOptionPane.showInputDialog("Enter a Bright level");
		
		// Elevator call
		Elevator myElevator = new Elevator(id);
		myElevator.callForUp(1); // 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 + " / floor Lamp");
		floorLamp.on();
		
		DimmingLights moodLamp = new DimmingLights(id + "moodLamp");
		moodLamp.setBright(Double.parseDouble(bright)); // n퍼센트 밝기 켜기
		moodLamp.on();
		
		

	}

}

- void main(String[] args)의 의미

args는 프로그램과 사용자가 입력한 값을 매개해준다고 해서 매개변수, 파라미터(paramter)라고 한다.

위에서 Program argument값 "Java APT 507" "15.0"을 2개 입력했기 때문에 배열로 넘겨줘야해서 String[]을 썼다.

- 수정된 부분

String id = args[0];
String bright = args[1];

( 에러나면 큰 따옴표가 아닌 작은 따옴표로 감싸기 )

 

* 수업 소스

github.com/sso-jeong/JAVA.git

 

sso-jeong/JAVA

Contribute to sso-jeong/JAVA development by creating an account on GitHub.

github.com

 

반응형

댓글