ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 2023년 05월11일 수업 내용 정리
    국비 교육 내용 정리 2023. 5. 11. 09:50

    <JAVA>

    (1) for문

    package ch03.ex03;

     

    public class C01For {

     

    public static void main(String[] args) {

     

    // for (초기화식; 조건식/종료식; 증감식){실행}

    // 특정 조건에 맞춰서 정해진 횟수를 반복한다.

    for(int i=0; i<10; i++) {

    System.out.println(i);

    }

    System.out.println("END");

    }

     

    }

     

     

     

    (2) 배열 + for문 결합 코드

    package ch04;

     

    public class C06Array {

    public static void main(String[] args) {

     

    int[] arr = new int[10]; //화물열차 생각하셈

     

    for (int i=0; i<10; i++) { // i<10을 통해서 10개라는 걸 알 수 있음

    System.out.println(arr[i]); // arr[0] arr[1] arr[2]

    }

    System.out.println();

     

    int num = 1;

    for (int i=0; i<10; i++) {

    arr[i] = num++; // arr[0]=1,arr[1]=2.arr[2]=3,...

    System.out.println(arr[i]);

    }

    System.out.println();

     

    // Enhanced for문(향상된 for문)_forEach문_자루구조 알고리즘

    int[] elements = {1,2,3,4,5};

     

    for (int el : elements) {

    System.out.println(el);

    }

    }

    }

     

    (3) 함수의 종류 

    package ch05;

     

    public class C01Function {

     

    // 함수의 4가지 유형

    // 1. 반환값 X 매개변수 X

    // 2. 반환값 O 매개변수 X

    // 3. 반환값 X 매개변수 O

    // 4. 반환값 O 매개변수 O

    //[반환타입] [함수명](매개변수) { 실행블록 }

     

    //input (x) -> f(x) -> output(y)

     

    void f1() {} // x(input)도 없고, y(output)도 없음

    int f2() {return 0;}

    void f3(int a) {}

    char f4(int a) { return '0';}

     

     

    void main(String[] args) {

     

     

    }

     

    }

     

    (3) 함수의 활용

    package ch05;

     

    public class C02Method {

     

    // 멤버변수_필드(속성): 변수상자에 데이터 저장

    int a;

    int b;

     

    // 멤버함수_메소드(기능)1

    static int add(int x,int y) {

    return x + y;

    }

     

     

    public static void main(String[] args) { //args 부분은 매개변수라 아무렇게나 이름 지어줘도됨, 그냥 의미있는 문자 넣기//

     

    System.out.println(add(10,10)); // int x, int y 2개니깐 숫자 넣어줘야함! //

    }

     

    }

    (4)멤버함수_메소드(기능)2

    package ch05;

    public class C03Method {
     // 멤버변수_필드(속성): 변수상자에 데이터 저장//
        String bookTitle;
    int bookPrice;
    String author;

    //멤버함수_메소드(기능)//
    static void print(String msg) {
         System.out.println(msg);
        }

    static int add(int x, int y) {
                return x + y;
    }
    static double avg(int x, int y) {
    int sum = add(x, y); //4)
    double result = sum / 2.0;//소수점으로 만들어 주려고함 
    return result; // 입력한거의 나누기 2.0이니깐 평균구하는식임!
    }

     
    static void process() {
        double result = avg(100, 20); //3)
    print("계산결과: " + result); //5)
    }

    public static void main(String[] args) { //1)

    process(); //2)



    }

    + 오류 잡을떄 chatgpt 활용 .. ai 시대가 다가온다는게 무서운거 같다.

     

    코드를 보면 메소드 내부에서 사용되는 add 메소드는 static으로 선언되어 있기 때문에 인스턴스 메소드에서 직접 호출할 수 없습니다. 따라서 avg 메소드를 static으로 변경하거나, add 메소드를 인스턴스 메소드로 변경해야 합니다.

    또한, print 메소드에서는 msg 매개변수를 사용하지 않고 있기 때문에 불필요한 매개변수입니다. msg 매개변수를 삭제하고 메소드 내에서 출력할 문자열을 직접 작성하면 됩니다.

    수정된 코드는 다음과 같습니다.

     

    package ch05;

    public class C03Method {
        // 멤버변수_필드(속성): 변수상자에 데이터 저장//
        String bookTitle;
        int bookPrice;
        String author;

        // 멤버함수_메소드(기능)//
        static void print(String msg) {
            System.out.println(msg);
        }

        static int add(int x, int y) {
            return x + y;
        }

        static double avg(int x, int y) {
            int sum = add(x, y);
            double result = sum / 2.0;
            return result;
        }

        static void process() {
            double result = avg(100, 20);
            print("계산결과: " + result);
        }

        public static void main(String[] args) {
            process();
        }
    }

     

     

    (5) 객체

    -Book.java(book 이라는 클래스를 만들어줌 (설계도라 보면됨))

     

    package ch01.ex01;

     

    public class Book { //객체를 생성하는 (찍어내는) 설계도_틀 //도메인 클래스

    // 멤버 변수_필드(속성)

    String title; // 제목

    String author; // 저자

    String publisher; // 출판사

    int page; // 페이지수

    int price; // 판매가

    String dateOfPubilcation; // 발간일

    }

     

     

     

    - BookTest.java

     

    package ch01.ex01;

     

    public class BookTest { //실행용 클래스

    public static void main(String[] args) {

     

    Book b1 = new Book(); //객체 생성_책 1권 찍어냄 (천번째책) -> 위에 book.java라는 데이터를 b1으로 가져옴

     

    b1.title = "해리 포터";

    b1.author = "J.K.롤링";

    b1.page = 500;

     

    System.out.println(b1.title);

    System.out.println(b1.author);

    Book b2 = new Book();// 객체 생성_책 1권 찍어냄(두번째 책)

    b2.title ="Rich Dad";

    System.out.println(b2.title);

     

    }

     

    }

Designed by Tistory.