JAVA1 ~9.3. IoT 프로그램 만들기

2022. 5. 27. 08:51·📗 self-study/📗 생활코딩

*JAVA의 기본 문법

-- project는 큰 틀, class는 미니 프로젝트 주제 정도? 한 화면에 표시되는 것을 모두 모아 놓은 폴더 같은 넉낌으로 생각하면 되려나 싶다

-- 범위: (상위) Project - Class (하위)

public class HelloWorldApp {                // public class 뒤의 문자열은 이클립스에서 생성한 클래스 이름과 동일해야 함 
  public static void main(String[] args) {  // main은 모든 실행 프로그램의 기본이 되는 함수이며
                    main 안에 출력할 값들을 넣어야 함
     System.out.println("Hello World!!");     // 큰 따옴표(" ") 사이에 출력하고자 하는 문자열을 적으면 됨
   }
}

 

*System.out.println 단축키

syso + ctrl + space bar

sout + ctrl + space bar

 

*문자열과 숫자열의 구분

System.out.println(6); // Number

System.out.println("six"); // String

 

System.out.println("6"); // String 6

 

System.out.println(6+6); // 12

System.out.println("6"+"6"); // 66

 

System.out.println(6*6); // 36

// System.out.println("6"*"6"); 실행될 수 없는 값임!

 

System.out.println("1111".length()); // 4

// System.out.println(1111.length()); 실행될 수 없는 값임!

 

*숫자와 연산

System.out.println(6+2); // 8

System.out.println(6-2); // 4

System.out.println(6*2); // 12

System.out.println(6/2); // 3

 

System.out.println(Math.PI); // 3.1415926...

System.out.println(Math.floor(Math.PI); // 3.0 floor 소수점 이하 내림

System.out.println(Math.ceil(Math.PI); // 4.0 ceil 소수점 이하 올림

 

*문자열

// Character VS String

System.out.println("Hello World"); // String 문자열, 큰 따옴표("")

System.out.println('H'); // Character  문자(하나의 문자), 작은 따옴표('')

System.out.println("H"); // 하나의 문자라도 String으로 표현 가능함

 

// 엔터를 눌러도 변하는 게 없습니다 ^^... 똑똑이 자바가 + "" 를 만들어 줄 뿐 결과값 동일

System.out.println("Hello "

    + "World");

⇒ 출력값: Hello World

 

// new line:  \n을 쓰면 줄바꿈이 됨

System.out.println("Hello \nWorld");

⇒ 출력값: Hello

     World

 

// escape:  \뒤에 오는 것을 강제로 문자로 인식하게 함

System.out.println("Hello \"World\"");

⇒ 출력값: Hello "World"

 

 *문자열 연산

System.out.println("Hello World".length()); // 11

System.out.println("Hello, [[[name]]] ... bye.".replace("[[[name]]]", "duru")); //  Hello duru .. bye.

 

*변수(variable): 특정 데이터 값을 저장하는 메모리 영역(하나의 값만 저장 가능, 수시로 변경 가능)

변수를 지정하는 이유

1) 값을 넣을 때는 int, double 등으로 한정하고 그 외의 값은 오류가 나기 때문에 값을 넣기 까다롭지만, 일단 값을 넣었을 때 그 값이 컴파일 된다면 그 결과는 조건에 부합하는 것이라는 뜻이므로 신뢰도 올라감

2) 누구든 그 값을 봤을 때 예상 가능(좋은 이름을 써야 함 ex: double VAT = 10.0; 등) 

 

int a = 1 ; // Number → integer 정수

System.out.println(a); // 1

 

double b = 1.1 ; // real number 실수

System.out.println(b); // 1.1

 

String c = "Hello World";

System.out.println(c); // Hello World

 

*변수의 효용

String name = "ruru";

System.out.println("Hello, "+name+" ... "+name+" ... kitty ... bye");

⇒ 출력값: Hello, ruru ... ruru ... kitty ... bye

 

    변수

double VAT = 10.0;

데이터 타입

System.out.println(VAT); 

 

*캐스팅(casting): 데이터 타입을 다른 데이터 타입으로 변환(convert) 하는 것

double a = 1.1;

double b = 1; // 1과 1.0은 같아서 잃어버리는(손실) 값이 없으므로 오류는 안 남

double b2 = (double) 1; // 다만 궁극적으로 표현하자면 이렇게 된 것임!

System.out.println(b); // 1.0

 

// int c = 1.1; 를 쳤을 때는 똑똑이 이클립스가 두 가지 수정본을 보여 줌

double d = 1.1;  // 자동으로 데이터 타입을 double로 바꿔 주거나

int e = (int) 1.1; // 1.1을 강제로 정수(int)로 바꾸어 줌

System.out.println(e); // 1 

 

// 1 to String

모르겠으면 구글링을 해 봐요! 검색어: java int to string casting

String f = Integer.toString(1);

System.out.println(f.getClass());

     현재 참조하고 있는 클래스를 확인할 수 있는 메소드

⇒ 출력값:

1
class java.lang.String

 

*다른 사람의 작업 파일을 내 이클립스에 불러오는 법

깃허브 등에서 다운로드한 코딩 파일/폴더 전체 드래그 해서 내가 사용하고자 하는 이클립스 프로젝트에 끌어오면 끝

 

*프로그래밍(IoT ver.)

import org.opentutorials.iot.Elevator;

import org.opentutorials.iot.Security;

import org.opentutorials.iot.Lighting;

뒤의 주소에 있는 클래스를 끌어올 겁니다~ 라는 뜻

 

public class OkJavaGoInHouse {

   public static void main(String[] args) {

   

   String id = "JAVA APT 507"; // (1)~(4)가 중복이므로 변수 지정 이렇게 하면 더 쉬워집니다

 

     // Elevator call

     // (1) Elevator myElevator = new Elevator("JAVA APT 507");

     Elevator myElevator = new Elevator(id);

     myElevator.callForUp(1); // 나 올라갈 건데 엘베 좀 1층으로 보내 줘

 

     // Security off

    // (2) Security mySecurity = new Security("JAVA APT 507");

    Security mySecurity = new Security(id);

    mySecurity.off();

 

     // Light on

    // (3) Lighting hallLamp = new Lighting("JAVA APT 507 / Hall Lamp");

    Lighting hallLamp = new Lighting(id+" / Hall Lamp");

    hallLamp.on();

 

    // (4) Lighting floorLamp = new Lighting("JAVA APT 507 / floorLamp");

    Lighting floorLamp = new Lighting(id+" / floorLamp");

    floorLamp.on();

 

   }

}

저작자표시 (새창열림)
'📗 self-study/📗 생활코딩' 카테고리의 다른 글
  • JAVA1 ~완강
  • JAVA1 ~12.5. 직접 컴파일하고 실행하기 - 입력과 출력
  • JAVA1 ~4.2. 실행 - java의 동작 원리
  • WEB1- HTML & Internet ~엔딩 크래딧
천재강쥐
천재강쥐
  • 천재강쥐
    디버거도 버거다
    천재강쥐
  • 전체
    오늘
    어제
    • Category (467)
      • 진짜 너무 궁금한데 이걸 나만 몰라...? (0)
      • 💾 Portfolio (2)
      • 🐤 CodingTest (28)
        • Java (20)
        • ᕕ(ꐦ°᷄д°᷅)ᕗ❌ (5)
      • 🚀 from error to study (142)
        • AI (1)
        • Cloud (2)
        • DB (12)
        • Front-End (16)
        • Github (14)
        • Java (39)
        • Mac (7)
        • Normal (29)
        • Server (22)
      • 📘 certificate (44)
        • 📘 리눅스마스터1급 (1)
        • 📘⭕️ 정보처리기사 (40)
        • 📘⭕️ SQLD (3)
      • 📗 self-study (234)
        • 📗 inflearn (35)
        • 📗 생활코딩 (8)
        • 📗 KH정보교육원 당산지원 (190)
      • 🎨 Scoop the others (0)
        • 📖 Peeking into other people.. (0)
        • 🇫🇷 (0)
        • 📘⭕️ 한국사능력검정시험 심화 (11)
        • 오블완 (4)
  • 인기 글

  • hELLO· Designed By정상우.v4.10.1
천재강쥐
JAVA1 ~9.3. IoT 프로그램 만들기
상단으로

티스토리툴바