국비 3일차_1

강지수·2023년 12월 14일
0

국비교육

목록 보기
5/97

지난 시간 복습

char(문자) : '' 로 감싼다, 한 글자
string(문자열) : "" 로 감싼다, 여러 글자

float/double : 실수형 타입

java api

ex) Integer Class

계층도를 확인할 수 있음, java.lang.Integer 는 java.lang.Number 안에 속해있고,
java.lang.Number 는 java.lang.Object 에 속해있음.

수업중에는 toBinaryString method 를 사용함

int 값을 받아서 2진수값으로 변환한 후 string으로 return 한다.


부호 연산

SignOperatorExp class 생성

package com.tech.gt01;

public class SignOperatorExp {

	public static void main(String[] args) {
//		부호 연산
		int x=-100;
		int result1=+x; // +1 * -100
		int result2=-x; // -1 * -100
		
		System.out.println("result1 : "+result1);
		System.out.println("result2 : "+result2);

	}

}

결과

result1 : -100
result2 : 100

+1 과 -1을 곱하는것과 같은 결과

증감 연산

//		증감 연산
//		++ , --
		int x1=10;
		x1=++x1; // 전위연산
		x1=x1++; // 후위연산
		
		System.out.println("x1:"+x1);
		System.out.println("x1_1:"+(++x1));
//		출력 전 증감
		System.out.println("x1_2:"+(x1++));
//		출력 후 증감
		System.out.println("x1_3:"+x1);

결과

x1:11
x1_1:12
x1_2:12
x1_3:13


폰트 변경

코딩폰트 검색

D2 Coding 글꼴 다운로드

eclipse - Window - Preferences - General - Appearance - Colors and Fonts - Edit - Basic - Text Font - Edit - D2Coding 설정


조건문

com.tech.gt02 package 생성
IfExample class 생성

package com.tech.gt02;

public class IfExample {

	public static void main(String[] args) {
//		if문 분기
//		조건문
		int score=90;
		System.out.println(score>=90); // 조건식
//		결과값이 true/false 로 나옴
//		=> 조건식 : boolean
		
		if(score>=90) {
			System.out.println("굳~~~~~~"); // true일 때 표현되는 영역
		} else {
			System.out.println("No굳~~~~~~"); // false일 때 표현되는 영역
		}

	}

}

결과

true
굳~~~~~~

IfElseExample class 생성

등급 매기기

package com.tech.gt02;

public class IfElseExample {

	public static void main(String[] args) {
		 int score=50;
//		 등급매기기
//		 90점 이상 = A
//		 90점 미만 80점 이상 = B
//		 80점 미만 70점 이상 = C
//		 70점 미만 60점 이상 = D
//		 60점 미만 = F
		 if (score>=90) {
			 System.out.println("90점이상입니다.");
			 System.out.println("A");
		 } else if (score>=80) {
			 System.out.println("80점이상입니다.");
			 System.out.println("B");
		 } else if (score>=70) {
			 System.out.println("70점이상입니다.");
			 System.out.println("C");
		 } else if (score>=60) {
			 System.out.println("60점이상입니다.");
			 System.out.println("D");
		 } else {
			 System.out.println("60점미만입니다.");
			 System.out.println("F");
		 }

	}

}

결과

60점미만입니다.
F

IfElseExample2 class 생성

package com.tech.gt02;

public class IfElseExample2 {

	public static void main(String[] args) {
		int score=70;
		String grade=""; // 초기화
		
		if(score>=90) {
			grade="A";
		} else {
			grade="B";
		}
		System.out.println(grade);
	}
}

결과

B

반복적인 출력문을 줄여주는 작업


IfElseExample3 class 생성

package com.tech.gt02;

public class IfElseExample3 {

	public static void main(String[] args) {
		int score=70;
		String grade="";
		if(score>=90) grade="A";
		// 중괄호 안의 문장이 한 문장이면 {} 생략 가능
		else grade="B";
		// 중괄호 안의 문장이 한 문장이면 {} 생략 가능		
		System.out.println(grade);

	}

}

결과

B

중괄호 안의 문장이 한 문장이면 {} 생략 가능


미션
삼각형과 사각형을 구분하여
면적을 구하시오
width=100
height=50
삼각형인지 아닌지는 변수로 지정하세요
삼각형 면적 triSquare
사각형 면적 recSquare

내가 짠 코드

		int width=100;
		int height=50;
		int triSquare = (width*height)/2;
		int recSquare = width*height;
		
        String str = "사각형";
		
		if(str=="삼각형") {
			System.out.println(triSquare);
		} else {
			System.out.println(recSquare);

결과

5000

강사님의 코드 1

		int height=50;
		int width=100;
		
//		삼각형 면적
		float triSquare=0.0f; // 초기화
		triSquare=height*width/2f;
		System.out.println("삼각형면적:"+triSquare);
		
//		사각형 면적
		float recSquare=0.0f;
		recSquare=height*width;
		System.out.println("사각형면적:"+recSquare);

결과

삼각형면적:2500.0
사각형면적:5000.0

강사님의 코드 2

		int height = 50;
		int width = 100;

		float twoSquare = 0.0f; // 초기화

		String shape = "삼각형";

		if (shape == "삼각형") {
			twoSquare = height * width / 2f;
		} else {
			twoSquare = height * width;
		}
		System.out.println(shape+"면적:" + twoSquare);

결과

삼각형면적:2500.0

강사님의 코드 3

		int height = 50;
		int width = 100;

		float triSquare = 0.0f; // 초기화
		float recSquare = 0.0f;

		String shape = "삼각형";

		if (shape == "삼각형") {
//		삼각형 면적
			triSquare = height * width / 2f;
			System.out.println("삼각형면적:" + triSquare);
		} else {
//			사각형 면적
			recSquare = height * width;
			System.out.println("사각형면적:" + recSquare);

결과

삼각형면적:2500.0

미션을 즉흥적으로 수업중에 내서 진행하시다 보니 말이 애매한 부분이 많고, 진행하면서 필요한 부분을 추가하거나 생략해버리는 경향이 있음. 그때그때의 미션 문제의 정답이 뭐고, 어떻게 푸느냐보다 그냥 이런 기능을 이용할 수 있다 정도로 생각하면서 강의를 들어야 할 것 같음.

+ 개념 등과 같은 부분을 생각날때마다 조금씩 한번씩 쓱 설명하고 넘어가다보니 아예 처음 듣는 인원들은 이해하기 어려울 것 같음. 독학을 조금이나마 해놓았던 부분이 이해하는 데 큰 도움이 되고 있음.
교육은 강사님 스타일에 따라 정말 많이 갈린다는 걸 새삼 느끼는 중
이거는 아닌거같애~, 잘 모르겠는데, 이렇게 쓰면 되겠죠? 되나요? 안되네요.. 흠.. 이렇게 한번 써볼까요? 등등의 말버릇과 설명하는 버릇이 있으시고, 진행하다가 다른 방향으로 자주 빠지는 경향이 있으심. 본 내용 설명하다 이거 생각나면 이거 설명 쪼끔 하고, 저거 생각나면 저거 쪼끔 하다가 어차피 나중에 배우니까 다음에 설명을 깊게 할게요~ 하고 본 내용하고 이런 식..


Ctrl+Shift+F : 자동 정렬 단축키
Alt+위,아래 방향키 : 이미 작성된 Code Line 을 다른 Line으로 이동하는 단축키


변수

Variable class 생성

변수 선언 기준
매번 다른 데이터를 사용할 때
연산의 결과를 보관할 때
한번 선언한 데이터를 여러 곳에 사용할 때

변수 선언 규칙
대/소문자 구분
문자, _ , & : 변수 이름에 포함 가능, 이름 시작 가능
숫자 : 변수 이름에 포함 가능, but 이름 시작 불가능
_ 와 $를 제외한 특수문자 : 이름에 포함 불가능
Keyword 사용 불가
변수 이름에 의미를 담고 정의해야 함
변수 이름은 소문자로 시작함
여러 글자를 결합할 때 _ 사용 or 첫글자 대문자로
ex) user_name (Snake Type), userName (Camel Type)


StringConcatExp class 생성

		String str1="JDK"+6.0;
		String str2=str1+"특징";
		System.out.println(str2);
		
		String str3="jdk"+3+3+3.0;		
		System.out.println(str3);
		
		String str4=3+3+3.0+"jdk";
		System.out.println(str4);
		
		String str5=str1.concat(str4); // + 연산과 같음
		System.out.println(str5);

결과

JDK6.0특징
jdk333.0
9.0jdk
JDK6.09.0jdk

day003 project 생성
com.tech.gt001 package 생성
MathExample class 생성

Math.random method 사용

0.0 이상 1 미만의 수를 double로 return 해준다.

		double mathNum=Math.random();
		System.out.println(mathNum);

결과

0.9195252302097916

0이상 1미만의 수가 random으로 나온다.


//		정수로 표현
		mathNum=mathNum*10;
		System.out.println(mathNum);
		int intNum=(int) mathNum;
		System.out.println(intNum);

결과

8.833690549692378
8

double 을 int 로 강제형변환해주어 8 만 출력된다.


미션
Math.random 으로 값을 받아서 score (0 ~ 100 까지의 범위) 로 출력하세요

내가 짠 코드

		double score=Math.random();
		score=score*100;
		int intScore=(int) score;
		System.out.println(intScore);

결과

79

강사님의 코드

		double score=Math.random();
		score=score=100;
		System.out.println(score);
		int intNum2=(int)score;
		System.out.println("score:"+intNum2);

결과

59.302633048376116
score:59

미션
Math.random 으로 값을 받아서 score (0 ~ 100 까지의 범위) 로 출력하세요

내가 짠 코드

		double score3=Math.random();
		score3=(score3*50)+50;
		int intScore3=(int)score3;
		System.out.println("score:"+intScore3);

결과

score:54

강사님의 코드

		double score4=Math.random();
		score4=score4*50;
		System.out.println(score4);
		int intNum3=(int)score4+50;
		System.out.println("score:"+intNum3);

결과

17.158747989668882
score:67

profile
개발자 준비의 준비준비중..

0개의 댓글