오늘은 부트캠프 6일차 되는 날이다. 오늘부터 본격적인 프로그래밍 기초 주차가 시작되는 날이다. 일단 총 28강의 중 17강까지 들었고 객체지향까지 강의를 들어보았다. 처음에는 이해하기가 정말 쉬워서 쑥쑥 나아가다 객체지향쪽에서 벽을 만난 것처럼 쿵하고 부딪힌 느낌이었다. 뭔가 점점 어려워지는 것도 아니었고 다른 언어 공부하는 줄 알았다. 강의를 듣다보니 무언가 알거 같으면서도 모르겠다고 느낀 것이 마지막 퀴즈였던 객체지향 퀴즈에서 많이 당황하였다. 자바는 객체지향이 절반이상이라는 말이 틀린 것이 아니라는 생각이 든다. 그래도 어렵다고 포기하는 것보다 꾸준하고 성실하게 하면 알게 되는 날이 오지 않을까 싶다.
오늘 배운 것
1. 자바
- 객체 지향적 프로그래밍 언어이다. - 장점 : Write Once, Run Anywhere - 단점 : 예전에 정해진 문법들로 인해 더 유연하고 견고한 코드를 만들기 어렵다.
2. 변수와 상수
- 변수는 재할당이 되지만, final 상수는 재할당이 되지 않는다. - final은 접근제어자라고 한다. 자바에서는 카멜 케이스(ex>finalNumber) 규칙으로 사용되고 있다.
3. 기본자료형의 종류
- 숫자형(short / int / long / float / double) - 문자형(char) - 논리(boolean) - 바이트(byte)
4. 참조자료형
- 쉽게 말해 기본자료형 이외에 것들이 참조자료형이다. - Java의 인스턴스, 객체를 가르킬 수 있는 자료형이다. - Class로 정의된 타입을 쓸 때는 다 참조자료형이다.
5. 배열
- int[ ] 중 꺽쇠는 배열이라고 하고, int [ ]는 int를 여러 담을 수 있는 것 - 좀 더 코드를 짧고 간결하게 쓸 수 있다. - 배열 안에 있는 것을 인덱스라고 표현할 수 있다. - Array는 Array.to.String이란 걸로 감싸줘야 한다. 아니면 이상한 값이 출력될 수 있다.
6. 연산자
- 산술연산자(+ / - / * / / / %) - 관계연산자(> / < / >= / <= / == / !=) - 논리연산자(&& / || / !)
7. 조건문
- if 문 : 상황 판단할 때 쓰임 - switch 문 : 정확히 어떤 케이스에 맞냐라는게 목적이다. - break란 뒤에 있는 switch문을 확인하지 않고 블록을 탈출하겠다. - default란 아무런 케이스에 맞지 않을 때 default에 있는 결과가 출력된다. - 삼항연산자 : 논리 조건에 대해서 ?가 붙으면 그 뒤에 true일 때 수행할 것, false일 때 수행할 것, 이렇게 하면 그 결과가 사용한 변수에 할당된다.
8. 반복문
- for문 / while문 - for-each문 : 배열이나 collection 어떤 것의 나열들이 있는 변수가 있을 때 반복문을 쉽게 짤 수 있는 것이다. - do-while문 : 조건을 확인하지 않고 수행을 먼저 하는 것
9. 객체지향 언어
- 클래스 : 객체의 속성을 정의해 놓은 것 - 인스턴스 : 어떠한 클래스로부터 만들어진 객체 - 메소드 : 어떤 작업을 수행하는 코드를 하나를 묶어놓은 것 - 메소드를 사용하는 이유는 프로그램의 재사용성과 구조화를 위해서 사용 - 생성자 : new를 할 때만 불리는 것, class 이름과 똑같이 써줘야한다. - 생성자의 특징 ㄱ. return 값이 없다. - 상속 : 기존의 클래스를 재사용하는 방식 중의 하나, 여러 클래스를 상속 받을 수 없다. - overloading : 한 클래스 내에서 동일한 이름의 method를 여러 개 갖는 것 - overriding : 부모 class에 있는 method를 똑같이 자식 class에 정의하는 거, 부모에 있던 원래 함수의 내용은 없는 셈 치는 거 - 접근제어자 : 멤버 변수/함수 혹은 클래스에 사용되며 외부에서의 접근을 제한하는 역할을 한다. - private : 같은 클래스 내에서만 접근이 가능하다. - default : 같은 패키지 내에서만 접근이 가능하다. - protected : 같은 패키지 내에서, 그리고 다른 패키지의 자식클래스에서 접근이 가능하다. - public : 접근 제한이 전혀 없다. - 추상클래스 : 추상메소드를 선언할 수 있는 클래스를 의미한다. 상속받는 자식 클래스 없이 그 자체로 인스턴스를 생성 할 수 없다. - 추상클래스의 특징 ㄱ. extend라는 키워드를 사용 ㄴ. 추상메소드에서만 자식클래스에서 무조건 구현해야 된다. - 추상메소드 : 설계만 되어 있고 구현체가 없는 것(return type, 함수 이름, 파라미터는 제외) 중간에 있는 블록 구현체는 없고, 이것을 자식클래스에서 구현해야 한다. - 인터페이스 : 객체의 특정 행동의 특징을 정의하는 것(접근제어자, return type, method 이름만 정의), 함수의 내용은 추상메소드처럼 없다. - 인터페이스의 특징 ㄱ. 구현하는 객체의 동작에 명세 ㄴ. 다중상속 가능 ㄷ. implement라는 키워드를 이용해서 구현 ㄹ. method 시그니처만 선언이 가능
public class helloWorld{
public void main(String[] args) {
char str = 'S';
int num = 10;
System.out.println(str);
System.out.println(num);
int[] scores = {10, 20, 30};
System.out.println(scores[2]);
//다른 풀이
System.out.println(scores[scores.length - 1]);
}
}
public class helloWorld{
public void main(String[] args) {
int num1 = 10;
int num2 = 3;
boolean bool1 = true;
boolean bool2 = false;
num1 += num1;
num2 *= num1;
System.out.println(num1);
System.out.println(num2);
System.out.println(bool1 && bool2);
}
}
import java.util.Scanner;
public class helloWorld{
public void main(String[] args) {
Scanner sc = new Scanner(System.in);
int score = sc.nextInt();
if (score <= 100 && score > 90){
System.out.println("A등급입니다.");
}
else if(score <=90 && score > 80){
System.out.println("B등급입니다.");
}
else if(score <=80 && score > 70){
System.out.println("C등급입니다.");
}
else{
System.out.println("F등급입니다.");
}
}
}
public class helloWorld{
public void main(String[] args) {
for (int i = 5; i >= 0; i--) {
System.out.println("카운트 다운 : " + i);
}
}
}
public class Human {
String name;
int age;
int speed;
int x, y;
public Human(String name, int age, int speed, int x, int y) {
this.name = name;
this.age = age;
this.speed = speed;
this.x = x;
this.y = y;
}
public Human(String name, int age, int speed) {
this(name, age, speed, 0, 0);
}
public String getLocation() {
return "(" + x + ", " + y + ")";
}
protected void printWhoAmI() {
System.out.println("My name is " + name + ". " + age + " aged.");
}
}
Walkable.java
public interface Walkable {
void walk(int x, int y);
}
Runnable.java
public interface Runnable {
void run(int x, int y);
}
Swimmable.java
public interface Swimmable {
void swim(int x, int y);
}
GrandParent.java
public class GrandParent extends Human implements Walkable {
public GrandParent(String name, int age) {
super(name, age, 1);
}
@Override
public void walk(int x, int y) {
printWhoAmI();
System.out.println("walk speed: " + speed);
this.x = x;
this.y = y;
System.out.println("Walked to " + getLocation());
}
}
Parent.java
public class Parent extends Human implements Walkable, Runnable{
public Parent(String name, int age) {
super(name, age, 3);
}
@Override
public void run(int x, int y) {
printWhoAmI();
System.out.println("run speed: " + (speed + 2));
this.x = x;
this.y = y;
System.out.println("Ran to " + getLocation());
}
@Override
public void walk(int x, int y) {
printWhoAmI();
System.out.println("walk speed: " + speed);
this.x = x;
this.y = y;
System.out.println("Walked to " + getLocation());
}
}
Child.java
public class Child extends Human implements Walkable, Runnable, Swimmable{
public Child(String name, int age) {
super(name, age, 5);
}
@Override
public void swim(int x, int y) {
printWhoAmI();
System.out.println("swimming speed: " + (speed + 1));
this.x = x;
this.y = y;
System.out.println("Swum to " + getLocation());
}
@Override
public void run(int x, int y) {
printWhoAmI();
System.out.println("run speed: " + (speed + +2));
this.x = x;
this.y = y;
System.out.println("Ran to " + getLocation());
}
@Override
public void walk(int x, int y) {
printWhoAmI();
System.out.println("walk speed: " + speed);
this.x = x;
this.y = y;
System.out.println("Walked to " + getLocation());
}
}
Main.java
public class Main {
public static void main(String[] args) {
Human grandParent = new GrandParent("할아버지", 70);
Human parent = new Parent("엄마", 50);
Human child = new Child("나", 20);
Human[] humans = { grandParent, parent, child };
for (Human human : humans) {
System.out.println(human.name + ", 나이: " + human.age + ", 속도: " + human.speed + ", 장소: " + human
.getLocation());
}
System.out.println("<활동 시작>");
for (Human human : humans) {
if (human instanceof Walkable) {
((Walkable) human).walk(1, 1);
System.out.println(" - - - - - - ");
}
if (human instanceof Runnable) {
((Runnable) human).run(2, 2);
System.out.println(" - - - - - - ");
}
if (human instanceof Swimmable) {
((Swimmable) human).swim(3, -1);
System.out.println(" - - - - - - ");
}
}
}
}