본문 바로가기

verdantjuly/Today I Learned

TIL 20240226

728x90

이번 주 목표

JAVA 강의 완강

오늘 목표

JAVA 1-3주차

오늘 한 것

JAVA 1주차

JAVA 2주차

오늘 스케줄

AM 9:00 JAVA 공부

PM 10:00 TIL 작성

PM 11:00 취침

1. 자바 기본 개념

// 클래스
// public : (접근)제어자, public(공공의, 공통의)

import java.lang.reflect.Array;
import java.util.Arrays;

public class Main {

    // [ JDK ]
    // (1) compiler :  .java -> .class
    // (2) JRE
    // (3) JDB : 디버깅 (버그를 없애는 과정, n번째 코드를 면밀히 살펴본다.)

    // () : 소괄호
    // {} : 중괄호
    // [] : 대괄호

    // main method
    // 자바 프로젝트(앱)은 , 제일 먼저 클래스의 메인 메소드를 실행시킨다.
    // = JVM의 약속
    // static : 이 프로그램이 시작 될 때 무조건 실행되는 녀석임을 표현

    // output
    // void : 메서도 출력값의 데이터 타입
    // void : 아무것도 없다 > 출력은 없다.

    // input
    // String[] args : 매개변수 자리

    public static void main(String[] args) {

        // 객체  : 특징(속성, 변수), 행동(메소드)
        // print -> 줄 바꿈 하지 X
        // println -> 줄 바꿈 o
        // ln : line

        // -> 주석 description
        System.out.println(7);
        System.out.println(3);
        System.out.println(3.14);
        System.out.println("JAVA");

        // 상수 -> 변할 수 없는 수
        final boolean flag = true;

        // char (1개의 문자만 저장) : 작은 따옴표로 묶기
        // String(문자열) : 큰 따옴표로 묶기
        char charA = 'A';
        String alphabet = "Alphabet";

        // 작은 타입 > 큰 타입 형 변환 시 자동 형변환
        // 큰 타입 > 작은 타입 형 변환 시 값의 손실, 개발자가 선택하여 형변환, 강제 형변환 = 캐스팅

        // instance of
        // 피 연산자 조건에 명시된 클래스의 객체인지 비교하여
        // 맞으면 true
        // 틀리면 false


        // 연산자 우선 순위 : 산술 > 비교 > 논리 > 대입
        // 단, 괄호로 감싸 주면 괄호 안의 연산이 최우선 순위로 계산

        // switch 문 : case
        // while 문 : break continue

        // 깊은 복사 : 전부 복사해서 새로운 배열을 만든다.
        int [] a = {1, 2, 3, 4};
        int [] b = a.clone();
        // clone 메서드는 2차원 이상 배열에서는 얕은 복사로 동작한다.

        int [] c = Arrays.copyOf(a, a.length);
        // 깊은 복사 메서드

        int [] d = a;
        // 얕은 복사
        // 주소값만 복사

    }
}

 

2. String.format 에서 %

%s string

%d 정수

%f 실수
 %%를 하면 문자열 %가 출력됨.

 

3. Scanner next() vs nextLine()

next()는 띄어쓰기가 되지 않음

nextLine()은 띄어쓰기가 됨

 

4. 문자열 비교는 equals()

JAVA에서 ===는 사용 안 됨

==도 숫자만 사용 됨.

 

5. 1주차 숙제 : 레시피 출력기

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("제목을 입력해 주세요.");
        String title = sc.nextLine();;
        System.out.println("별점을 실수로 입력해 주세요.");
        String point = sc.nextLine();
        float floatpoint = Float.valueOf(point);
        Float percent = (floatpoint / 5) * 100;
        ArrayList<String> recipe = new ArrayList<String>();

        for (int i=0; i>=0; i++){
            System.out.println("레시피를 입력해 주세요. 레시피 입력이 끝나면 end를 입력해 주세요.");
             String input = sc.nextLine();
             if (input.equals("end")){
                 sc.close();
                 break;
             } else{
                 recipe.add((i + 1) + ". " + input);
             }
        }
        System.out.println(String.format("[ %s ]",title));
        System.out.println(String.format("별점 :  %d (%.1f%%)",(int)floatpoint, percent));
        recipe.forEach(System.out::println);
    }
}

 

6. 2주차 숙제 : 자료구조별 레시피 출력기

import java.util.*;

public class Main {


    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {


        System.out.println("자료 구조를 소문자로 입력해 주세요.");
        String structure = sc.nextLine();

        System.out.println("제목을 입력해 주세요.");
        String title = sc.nextLine();
        Converter converter = new Converter();

        switch(structure){
            case "map" :
                converter.map( structure, title);
                break;
            case "set" :
                converter.set( structure, title);
                break;
            case "arraylist" :
                converter.arraylist( structure, title);
                break;
        }



    }
}

  class Converter extends Main {
    protected void map  (String structure, String title ){
       Map<Integer, String> recipe = new HashMap<Integer, String>();

        for (int i=0; i>=0; i++){

            System.out.println("레시피를 입력해 주세요. 레시피 입력이 끝나면 끝을 입력해 주세요.");
            String input = sc.nextLine();
            if (input.equals("끝")){
                sc.close();
                break;
            } else{
                recipe.put(i+1 , (i+1) + ". " + input);
            }
        }
        System.out.println(String.format("[ %s 으로 저장된 %s ]",structure, title));
        recipe.forEach((key, value) -> System.out.println(value));
    }

      protected void set (String structure, String title){
          Set<String> recipe = new TreeSet<String>();

          for (int i=0; i>=0; i++){

              System.out.println("레시피를 입력해 주세요. 레시피 입력이 끝나면 끝을 입력해 주세요.");
              String input = sc.nextLine();
              if (input.equals("끝")){
                  sc.close();
                  break;
              } else{
                  recipe.add((i+1) + ". " + input);
              }
          }
          System.out.println(String.format("[ %s 으로 저장된 %s ]",structure, title));
          recipe.forEach(System.out::println);
      }

      protected void arraylist (String structure, String title){
          ArrayList<String> recipe = new ArrayList<String>();

          for (int i=0; i>=0; i++){

              System.out.println("레시피를 입력해 주세요. 레시피 입력이 끝나면 끝을 입력해 주세요.");
              String input = sc.nextLine();
              if (input.equals("끝")){
                  sc.close();
                  break;
              } else{
                  recipe.add((i+1) + ". " + input);
              }
          }
          System.out.println(String.format("[ %s 으로 저장된 %s ]",structure, title));
          recipe.forEach(System.out::println);
      }

}

정리 

1. 자바 기본 개념

// 클래스
// public : (접근)제어자, public(공공의, 공통의)

import java.lang.reflect.Array;
import java.util.Arrays;

public class Main {

    // [ JDK ]
    // (1) compiler :  .java -> .class
    // (2) JRE
    // (3) JDB : 디버깅 (버그를 없애는 과정, n번째 코드를 면밀히 살펴본다.)

    // () : 소괄호
    // {} : 중괄호
    // [] : 대괄호

    // main method
    // 자바 프로젝트(앱)은 , 제일 먼저 클래스의 메인 메소드를 실행시킨다.
    // = JVM의 약속
    // static : 이 프로그램이 시작 될 때 무조건 실행되는 녀석임을 표현

    // output
    // void : 메서도 출력값의 데이터 타입
    // void : 아무것도 없다 > 출력은 없다.

    // input
    // String[] args : 매개변수 자리

    public static void main(String[] args) {

        // 객체  : 특징(속성, 변수), 행동(메소드)
        // print -> 줄 바꿈 하지 X
        // println -> 줄 바꿈 o
        // ln : line

        // -> 주석 description
        System.out.println(7);
        System.out.println(3);
        System.out.println(3.14);
        System.out.println("JAVA");

        // 상수 -> 변할 수 없는 수
        final boolean flag = true;

        // char (1개의 문자만 저장) : 작은 따옴표로 묶기
        // String(문자열) : 큰 따옴표로 묶기
        char charA = 'A';
        String alphabet = "Alphabet";

        // 작은 타입 > 큰 타입 형 변환 시 자동 형변환
        // 큰 타입 > 작은 타입 형 변환 시 값의 손실, 개발자가 선택하여 형변환, 강제 형변환 = 캐스팅

        // instance of
        // 피 연산자 조건에 명시된 클래스의 객체인지 비교하여
        // 맞으면 true
        // 틀리면 false


        // 연산자 우선 순위 : 산술 > 비교 > 논리 > 대입
        // 단, 괄호로 감싸 주면 괄호 안의 연산이 최우선 순위로 계산

        // switch 문 : case
        // while 문 : break continue

        // 깊은 복사 : 전부 복사해서 새로운 배열을 만든다.
        int [] a = {1, 2, 3, 4};
        int [] b = a.clone();
        // clone 메서드는 2차원 이상 배열에서는 얕은 복사로 동작한다.

        int [] c = Arrays.copyOf(a, a.length);
        // 깊은 복사 메서드

        int [] d = a;
        // 얕은 복사
        // 주소값만 복사

    }
}

2. String.format 에서 %

%s string

%d 정수

%f 실수
 %%를 하면 문자열 %가 출력됨.

 

3. Scanner next() vs nextLine()

next()는 띄어쓰기가 되지 않음

nextLine()은 띄어쓰기가 됨

 

4. 문자열 비교는 equals()

JAVA에서 ===는 사용 안 됨

==도 숫자만 사용 됨.

 

KPT

Keep

열심히 하는 것

Problem

목표를 높게 잡는 것

Try

더 열심히 하기로

소감

자바를 드디어 다시 시작했다. 뿌듯.

 

 

 

 

'verdantjuly > Today I Learned' 카테고리의 다른 글

TIL 20240229  (0) 2024.02.29
TIL 20230227  (0) 2024.02.27
TIL 20240222  (0) 2024.02.22
TIL 20240221  (0) 2024.02.22
TIL 20240201  (0) 2024.02.01