본문 바로가기
개발일지/일간회고 (TIL)

조졌다! JAVA 문제 풀이 / TIL (22-11-16)

by 윤승임 2022. 11. 16.

조졌다,

알고리즘에만 신경쓰다보니 자바는 뒷전이었는데..

이제 앞으로 실시간 강의다. 나는 조졌다.

지금부터라도 잘 들어야지 조졌다 조졌다 하면 뭐하나.

화이팅~

 

heapq

자바 반복문 퀴즈

파이썬 하다가 자바하니까 엄한 분위기에 정신을 못차리겠다..!

1부터 100 더하기

public class Main {
    public static void main(String[] args) {
        // write your code here
        int sum = 0;
        for (int i = 0; i < 100; i++){
            sum += i + 1;
        }
        System.out.println(sum);
    }
}

5초부터 카운트 다운

#내가 한거
public class Main {
    public static void main(String[] args) {
        // write your code here

        for(int i = 0; i < 6; i++){
            int five = 5;
            five = five - i;
            System.out.println(five);
        }
    }
}

#다른 방법
public class Main {
    public static void main(String[] args) {
        // write your code here

        for(int i = 5; i >= 0; i--){
            System.out.println("카운트다운: " + i);
        }
    }
}

1부터 30까지 홀수의 합과 짝수의 합을 더하고 각각 출력하기

public class Main {
    public static void main(String[] args) {
        // write your code here
        int 짝수 = 0;
        int 홀수 = 0;
        for(int i = 1; i < 31; i++){
            if(i % 2 == 0){
                짝수 += i;
            }else {
                홀수 += i;
            }
        }
        System.out.println("홀수: " + 홀수);
        System.out.println("짝수: " + 짝수);
    }
}

#결과 
홀수: 225
짝수: 240

객체지향언어

클래스

표현하고자 하는 대상의 공통 속성을 한 군데에 정의해 놓은 것.

객체의 속성을 정의해 놓은 것.

클래스 내부의 정보를 멤버 변수라고 함.

멤버 변수

메소드 밖에서 선언된 변수

지역 변수

메소드 안에 선언된 변수

 

인스턴스

클래스로부터 만들어진 객체를 그 클래스의 인스턴스라고 한다.

인스턴스의 멤버변수에 접근할 때는 [생성된 인스턴스.멤버 변수] 의 형식을 사용한다.

class Phone {
    String model;	#멤버 변수
    String color;	#멤버 변수
    int price;		#멤버 변수
}

public class Main {
    public static void main(String[] args) {
        Phone galaxy = new Phone(); 	#인스턴스
        galaxy.model = "Galaxy10";
        galaxy.color = "Black";
        galaxy.price = 100;
        
        Phone iphone =new Phone();	#인스턴스
        iphone.model = "iPhoneX";
        iphone.color = "Black";
        iphone.price = 200;
        

        System.out.println("철수는 이번에 " + galaxy.model + galaxy.color + " 색상을 " + galaxy.price + "만원에 샀다.");
        System.out.println("영희는 이번에 " + iphone.model + iphone.color + " 색상을 " + iphone.price + "만원에 샀다.");
    }
}

메소드(method)

어떤 작업을 수행하는 코드를 하나로 묶어놓은 것.

재사용이 가능하다.

중복된 부분을 없앨 수 있다.

프로그램의 구조화.

int[] heights = new int[5]; // 키가 들어가 있는 배열

initHeight(heights); // 1. 키에 대한 초기화
sortHeight(heights); // 2. 키를 오름차순으로 정렬
printHeight(heights); // 3. 정렬된 키를 출력

readability의 기본 품질을 위해서 Java로 메소드를 만들 때 지켜야 하는 기본 약속

  1. 동사로 시작해야한다.
  2. camel case로 작성해야한다. (첫 단어는 소문자로, 이후 단어의 구분에 따라서 첫 글자만 대문자인 단어가 이어집니다. 중간에 띄어쓰기나 특수문자는 들어가지 않습니다.)

 

생성자

class Phone {
    String model;
    String color;
    int price;

    Phone (String model, String color, int price){
        this.model = model;
        this.color = color;
        this.price = price;
    }
}

public class Main {
    public static void main(String[] args) {
        Phone galaxy = new Phone( "galaxy10", " black", 100);

        Phone iphone =new Phone("iphoneX", " black", 200);

        System.out.println("철수는 이번에 " + galaxy.model + galaxy.color + " 색상을 " + galaxy.price + "만원에 샀다.");
        System.out.println("영희는 이번에 " + iphone.model + iphone.color + " 색상을 " + iphone.price + "만원에 샀다.");
    }
}

상속

class Animal{
    String name;

    public void cry(){
        System.out.println(name + "is crying.");
    }
}
class Dog extends Animal{
    Dog(String name){
        this.name = name;
    }
    public void swim() {
        System.out.println(name + "is swimming.");
    }
}

public class Main {
    public static void main(String[] args) {
        // write your code here
        Dog dog = new Dog("코코");
        dog.cry();
        dog.swim();

        Animal dog2 = new Dog("미미");
        dog2.cry();
        // dog2.swim(); 실제 변수를 선언한 타입은 Animal이기 때문에 Animal에 있는 기능밖에 못쓴다.
    }

오버로딩

오버라이딩