입력과 출력
OkJavaGoInHome의 코드를 변형하여, 유저가 입력값을 넣을 수 있는 OkJavaGoInHomeInput, OkJavaGoInHomeInputArg를 작성하였다.
<OkJavaGoInHome>
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();
hallLamp.off();
Lighting floorLamp = new Lighting(id+" / floorLamp");
floorLamp.on();
}
}
코드 상에서 id의 값을 지정해 주었다. id의 값을 변경하기 위해서는 코드를 변경하여야 한다는 불편함이 있다.
<OkJavaGoInHomeInput>
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 OkJavaGoInHomeInput {
//parameter, 매개변수
public static void main(String[] args) {
String id = JOptionPane.showInputDialog("Enter a id");
String bright = JOptionPane.showInputDialog("Enter a Bright level");
// alternative) double bright = Double.parseDouble(JOptionPane.showInputDialog("Enter a Bright level"));
// 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();
hallLamp.off();
Lighting floorLamp = new Lighting(id+" / floorLamp");
floorLamp.on();
DimmingLights moodLamp = new DimmingLights(id+" moodLamp");
moodLamp.setBright(Double.parseDouble(bright));
// alternative) moodLamp.setBright(bright);
moodLamp.on();
}
}
bright라는 새로운 변수를 선언하고, JOptionPane.showInputDialog라는 함수를 이용하여 id와 bright를 입력받을 수 있도록 하였다. 입력받은 상태는 string 자료형이지만, bright는 double 형이어야 하기 때문에 형변환을 하기 위해 Double.parseDouble(String ) 함수를 사용했다. 강의에서는 bright의 자료형을 string으로 지정한 후 setbright의 매개변수로 값을 넘길 때 형변환을 해주었는데, bright라는 변수를 setBright 이외의 함수에서도 사용한다면 bright 자체를 double로 선언하고 입력되는 값을 형변환하는 방법이 효율적일 것이라는 생각이 들어 alternative code를 작성하여 실행해 보았고, 이 또한 정상적으로 작동함을 확인하였다.
<OkJavaGoInHomeInputArg>
import org.opentutorials.iot.DimmingLights;
import org.opentutorials.iot.Elevator;
import org.opentutorials.iot.Lighting;
import org.opentutorials.iot.Security;
public class OkJavaGoInHomeInputArg {
public static void main(String[] args) {
String id = args[0];
String bright = args[1];
// 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();
hallLamp.off();
Lighting floorLamp = new Lighting(id+" / floorLamp");
floorLamp.on();
DimmingLights moodLamp = new DimmingLights(id+" moodLamp");
moodLamp.setBright(Double.parseDouble(bright));
moodLamp.on();
}
}
Run Configurations를 클릭하여 Argument 입력값을 조정하고, 새로운 파일로 저장하는 방법을 배웠다.
cf) Organize Favorites를 클릭하여 Run에서 상단에 즐겨찾는 파일이 표시되도록 설정할 수 있다.
디버거
더블클릭으로 중단점을 설정하고, eclipse 상의 벌레 그림을 클릭하면 디버거를 실행시킬 수 있음을 배웠다.
명령 프롬포트(cmd)를 이용한 컴파일
eclipse를 사용하지 않고 cmd를 이용해 직접 컴파일하고 실행하는 방법을 배웠다.
class 파일 생성과 실행을 위한 명령어는 다음과 같다.
--------
cd (경로 입력) : 경로 지정
javac Program.java (class file 생성)
dir :디렉토리의 파일 목록 확인
java Program : 클래스 파일 실행
패키지와 class 파일의 위치가 다를 경우
javac -cp ".;lib" OkJavaGoInHome.java (class file 생성)
java -cp ".;lib" OkJavaGoInHome (클래스 파일 실행)
변수 지정 시 Arg[]를 사용한 파일에 Arg 값을 입력해주기 위해서는
java -cp ".;lib" OkJavaGoInHomeInputArg "Java APT 507" "15.0"
--------
'java' 카테고리의 다른 글
java 공부 6일차(2주차-4) (0) | 2021.07.16 |
---|---|
java 공부 5일차(2주차-3) (0) | 2021.07.15 |
java 공부 4일차(2주차-2) (0) | 2021.07.14 |
java 공부 2일차(1주차-2) (0) | 2021.07.11 |
java 공부 1일차(1주차-1) (0) | 2021.07.10 |