Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- 미니 프로젝트
- 딥러닝
- 개발 프로젝트
- 패캠
- 개인프로젝트
- FE
- 자바스크립트
- 국비지원
- 국비지원취업
- 패스트캠퍼스
- 깃
- 프로젝트
- 패스트캠퍼스부트캠프
- 부트캠프
- pokeapi
- 프로그래머스 JS 코테
- 프로그래머스코테
- 프론트엔드부트캠프
- Java
- 프론트엔드개발자
- 개발블로그
- 자바스크립트 코딩테스트
- 영상처리
- js코테
- 프론트엔드
- 패캠부트캠프
- 깃허브
- JS
- 프로그래머스
- 코딩테스트
Archives
- Today
- Total
가쟤의 해변일기 🐳
풀스택과정 2주차 - 조건문 | 반복문 | Encapsulation 본문
💡 2025. 04. 15 수업 이슈
1. 조건문 if~else / switch
2. Encapsulation
3. 반복문 for / while / do~while
오늘 문법 중 가장 기본적이고 중요한 제어문과 반복문의 진도를 나갔다.
다른 언어도 많이 했어서 편하게 들었다.
그래서 많이 정리 한 내용이 없다..ㅎ
조건문, 제어문
특정한 조건을 만족하는 동안 문장을 수행할 수 있도록 제어하는 역할을 한다.
조건 : Boolean 값을 내는 것
Encapsulation
클래스의 필드에 바로 접근하는 md.month = 12 이런 방식은 정제되지 않은 값이 들어가기 때문에 위험하다.
따라서 Class에서 필드는 private 으로 하고, getter, setter로만 주입하여 사용하는 것이 바람직하다.
여기서, setter에서 필드 초기화 하기 전에 validate하는 로직이 필요하다.
Encapsulation 코드 작성 패턴
- 필드 앞 —> private
- void setter(o, o, o) —> public
- void getter() —> public
- setter 메소드에서 필드 초기화 하기 전에 타당한 값만 필드에 할당되도록 제어문을 달아준다.
→ 필드는 소중하니까⭐
아래 제어문과 Encapsulation 코드 작성 패턴을 사용하여 월, 일을 출력하는 코드 실습이 있었다.
package com.edu.capsulation;
// 날짜와 관련된 정보를 저장하는 클래스
// 제어문을 완성 ...
// 2. 월에 따라서 날짜가 달라진다.
// 1, 3, 5, 7, 8, 10, 12월 : 1~31일
// 2월 : 1~28일
// 4, 6, 9 ,11월 : 1~30일
// 이 날짜에 해당하는 값만 받아들이고 나머지는 에러 메세지를 출력
// 2. 오늘은 X월 X일 입니다. <- 이렇게 출력
public class MyDate {
// 해당 클래스의 멤버들끼리만 접근
private int month;
private int day;
private Boolean status;
private Boolean dayStatus;
public void setMonth(int month) {
// 1~12사이의 값만 입력
if (month > 12 || month <= 0) {
this.status = false;
System.out.println("1에서 12 사이의 값을 입력해주세요.");
} else {
this.status = true;
this.month = month;
System.out.println(month + "월 입력되었습니다.");
}
}
public boolean getMonthStatus() {
return status;
}
public void setDay(int day) {
// 입력된 월에 따라 허용 가능한 값이 달라짐
int limitDay = 0;
switch (month) {
case 2:
limitDay = 28;
if (day > limitDay || day <= 0) {
this.dayStatus = false;
System.out.println(month + "월에는 " + limitDay + "일 까지 입력 가능합니다.");
} else {
this.day = day;
this.dayStatus = true;
System.out.println(day + "일 입력되었습니다.");
}
break;
case 4:
case 6:
case 9:
case 11:
limitDay = 30;
if (day > limitDay || day <= 0) {
this.dayStatus = false;
System.out.println(month + "월에는 " + limitDay + "일 까지 입력 가능합니다.");
} else {
this.day = day;
this.dayStatus = true;
System.out.println(day + "일 입력되었습니다.");
}
break;
default:
limitDay = 31;
if (day > limitDay || day <= 0) {
this.dayStatus = false;
System.out.println(month + "월에는 " + limitDay + "일 까지 입력 가능합니다.");
} else {
this.day = day;
this.dayStatus = true;
System.out.println(day + "일 입력되었습니다.");
}
}
}
public boolean getDayStatus() {
return dayStatus;
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
}
아직 반복문을 사용할 수 없어서 이런 식으로 구현했다.
package com.edu.capsulation.test;
import java.util.Scanner;
import com.edu.capsulation.MyDate;
public class MyTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
MyDate md = new MyDate();
System.out.print("Month를 입력하세요. >> ");
int month = sc.nextInt();
md.setMonth(month);
if (md.getMonthStatus() == false) {
System.out.println("프로그램을 종료합니다.");
System.exit(0);
}
System.out.print("Day를 입력하세요. >> ");
int day = sc.nextInt();
md.setDay(day);
if (md.getDayStatus() == false) {
System.out.println("프로그램을 종료합니다.");
System.exit(0);
}
System.out.println("오늘은 " + md.getMonth() + "월 " + md.getDay() + "일 입니다!");
sc.close();
}
}
값을 검증하는 것을 중요하게 생각하고 짜봤다.
반복문
- for
- while
- do ~ while
[for문과 while문의 차이]
→ 정해져 있는 값만큼 반복 : for
→ 얼만큼 반복할지 모를 때 : while
break + 조건 → 거기서 반복을 멈춰
continue + 조건 → 계속 반복을 이어가
728x90
'풀스택과정' 카테고리의 다른 글
풀스택 과정 2주차 - Class DataType Array | MVC | 메소드 오버로딩 (0) | 2025.04.21 |
---|---|
풀스택과정 2주차 - JavaDoc | Array | 가위바위보 리팩토링 팀작업 (2) | 2025.04.16 |
풀스택 과정 2주차 - Scanner | 가비지컬렉션 | Short Circuit (2) | 2025.04.14 |
풀스택 과정 3일차 (1) | 2025.04.11 |
풀스택 과정 2일차 (0) | 2025.04.10 |