2일차

|

주요내용 : 명시적 형변환, 강제적 형변환, 지수승, API 문서보는법, GUI구현, 데이터형, 연산자(산술, 관계, 논리, 복합대입, 증감, bit)

		// 명시적(강제적) 형변환2
		// 1. 묵시적 형변환(문자 + 숫자 = 문자, 정수형 + 실수형 = 실수형, float + double = double)
		// (자동형변환)
		float f = 3.14159267f;
		System.out.println(f); // 3.1415927

		int i = 3000;
		System.out.println(i); // 3000

		float sum = f + i;
		System.out.println(sum); // 3003.1416

		// 2. 강제적 형변환(원하는 대로 변경) : 상황에 따른 형변환이 필요. 형변환은 출력단계에서 할수도있고, 그전단계에서 할수도 있다.(그때
		// 그때 다르다)
		System.out.println((int) f);// 3
		System.out.println((float) i); // 3000.0

		float sum2 = f + i;
		System.out.println((int) sum2); // 3003

		// 지수승 : 가장 큰 값이나 가장 작은 값을 표현, 오차발생.
		double e1 = 333.1415e-3;
		double e2 = 15.123456e7;
		double e3 = 1234567.123e-7;
		System.out.println(e1); // 0.331415
		System.out.println(e2); // 151234560.0
		System.out.println(e3); // 0.1234567123

		// 10진수, 2진수, 8진수, 16진수의 관계
		int x = 100; // 10진수
		int y = 0100; // 8진수
		int z = 0x100; // 16진수
		System.out.println(x); // 100
		System.out.println(y); // 64
		System.out.println(z); // 256

		// 근대 컴퓨터의 효시 : 중국 역학(0,1) => 라이프니쯔 => 폰노이만
		// 10진수 : 사람이 주로 사용.
		// 2진수 : 0, 1 => 0000, 0001, 0011, 0100, 0101...
		// 8진수 : 0~7, 10~17, 20~27, 30~37, 40~47, 50~57, 60~67...
		// 16진수 : 0~9, A, B, C, D, E, F(15), 10~19, 1A~1F, 20~29, 2A~2F... 이 형태를 주로
		// 다룹니다.

		// 100은 1100100 이다.(2진수로 표현)
		// 100은 144 이다.(8진수로 표현)

		// 8진수는 = 2의 3승(3bit) -> 2진수로 표현된 것을 3개씩 묶어서 표현
		// 001 100 100 = 144

		// 16진수 = 2의 4승(4bit) -> 2진수로 표현된 것을 4개씩 묶어서 표현
		// 0110 0100 = 64

		// <API 문서보기>
		// 오라클 사이트에서 APIs로 들어가서 찾아보면 됨.
		// java.lang.*; 생략이 가능하다. 원래는 java.lang.system.gc 이런식으로 작성하는게 맞음.
		System.gc(); // 메모리청소
		System.out.println(Byte.MIN_VALUE + " ~ " + Byte.MAX_VALUE); // byte의 최소값 ~ 최대값(-128 ~ 127)
		System.out.println(Short.MIN_VALUE + " ~ " + Short.MAX_VALUE); // short의 최소값 ~ 최대값(-32768 ~ 32767)
		System.out.println(Integer.MIN_VALUE + " ~ " + Integer.MAX_VALUE); // integer의 최소값 ~ 최대값(-2147483648 ~
																			// 2147483647)
		// <GUI구현> -> GUISample1.java

		// public class Button extends Component implements Accessible
		// This class creates a labeled button.
		// The application can cause some action to happen when the button is pushed.
		// This image depicts three views of a "Quit" button as it appears under
		// the Solaris operating system:

		// 데이터형(타입)
		// 1. byte(1byte) :2^7 int형으로 변환
		byte ba = 10;
		byte bb = 20;
		byte bc = (byte) (ba + bb); // byte는 기본적으로 int 취급을 하므로, cast를 해야하거나 아랫줄처럼 작성.
		int bc2 = ba + bb;
		System.out.println("byte = " + bc); // 30

		// 2. short(2byte) : 2^15 int형으로 변환
		short sa = 10;
		short sb = 20;
		short sc = (short) (sa + sb);
		int sc2 = sa + sb;
		System.out.println("short = " + sc2);// 30

		// 3. int(4byte) : 2^31
		int ia = 10;
		int ib = 20;
		int ic = ia + ib;
		System.out.println("integer = " + ic); // 30

		// 4. long(4byte) : L자 붙이기
		long la = 30L;
		long lb = 50L;
		long lc = la + lb;
		System.out.println("long = " + lc); // 80

		// 5. float(4byte) : f자 붙이기
		float fa = 30.67f;
		float fb = 45.12f;
		float fc = fa + fb;
		System.out.println("float = " + fc); // 75.79

		// 6. double(8byte) : 2^63
		double da = 12.0;
		double db = 45.123;
		double dc = da + db;
		System.out.println("double = " + dc); // 57.123

		// 문제] 이름과 나이를 입력받아서(버퍼드 or 스캐너 사용) 출력하는 프로그램을 작성하세요.
		// 출력결과> 당신의 이름은 홍길동이고, 나이는 123세 입니다.

		Scanner sca = new Scanner(System.in);
		System.out.println("이름를 입력하세요.");
		String nm = sca.nextLine();
		System.out.println("나이를 입력하세요.");
		int age = sca.nextInt();

		System.out.println("당신의 이름은 " + nm + "이고, 나이는 " + age + "세 입니다.");

		// 연산자 : Operator
		// 1.산술연산자(+, -, *, /(몫), &(나머지))
		int x = 50, y = 30;
		int z = x + y;
		int z2 = x - y;
		int z3 = x * y;
		int z4 = x / y;
		int z5 = x % y;
		System.out.println(z); // 80
		System.out.println(z2); // 20
		System.out.println(z3); // 1500
		System.out.println(z4); // 1
		System.out.println(z5); // 20

		// 문제1] 국어, 영어, 수학 점수를 입력 받아서, 합계와 평균을 구하세요.
		// 4. args[] : 강사님 풀이법. run as configurations -> argument 값에 입력해서 실행하는 방법
		int kor, eng, mat, total;
		double avr;

		kor = Integer.parseInt(args[0]);
		eng = Integer.parseInt(args[1]);
		mat = Integer.parseInt(args[2]);

		total = kor + eng + mat;
		avr = total / 3.0;
		System.out.println("합계는 " + total + "이고, 평균은 " + avr + "입니다.");

	 	<문제1 내가 풀이한방법 - scanner 버전>
		Scanner sca = new Scanner(System.in);
		System.out.println("국어점수를 입력하세요.");
		int kr = sca.nextInt();
		System.out.println("영어점수를 입력하세요.");
		int en = sca.nextInt();
		System.out.println("수학점수를 입력하세요.");
		int math = sca.nextInt();
		int sum3 = kr + en + math;
		int avg3 = sum3 / 3;
		System.out.println("합계는 " + sum3 + "이고, 평균은 " + avg3 + "입니다.");

		// 2.관계연산자 : 이항연산, x <= y, x >= y, x == y, x!=y : true/false
		int x = 100, y = 200;
		boolean result = false;

		// and(&&)
		result = x < y && y >= x;
		System.out.println(result); // t t = t

		result = x < y && y < 300;
		System.out.println(result); // t t = t

		result = x > y && y++ > 300;
		System.out.println(result); // f f = f

		result = x < y && ++y > 200;
		System.out.println(result); // t t = t

		result = x == y && y != x;
		System.out.println(result); // f t = f

		// or(||)
		result = x < y || y >= x;
		System.out.println(result); // t t = t

		result = x < y || y < 300;
		System.out.println(result); // t t = t

		result = x > y || y++ > 300;
		System.out.println(result); // f f = f

		result = x < y || ++y > 200;
		System.out.println(result); // t t = t

		result = x == y || y != x;
		System.out.println(result); // f t = t

		// 3. 논리연산자
//-----------------------------------------------
// A B and or    not    exor nand nor
//-----------------------------------------------
// 0 0  0  0   1 -> 0    0    1    1
// 0 1  0  1   0 -> 1    1    1    0
// 1 0  0  1             1    1    0
// 1 1  1  1             0    0    0
//-----------------------------------------------

		// 4. 복합대입 연산자
		// 산술연산 + 대입 연산 = 복합 대입, 단축연산자
		// +=, -=, *=, .=, %=(나머지)
		int x = 250;
		int y = 380;
		int sum3 = x + y;
		x += 1000; // x = x + 1000
		System.out.println(x);// 1250
		x -= 350; // x = x - 350
		System.out.println(x);// 900
		x *= 5; // x = x * 5
		System.out.println(x);// 4500
		x /= 3; // x = x / 3
		System.out.println(x);// 1500
		x %= 2; // x = x % 2
		System.out.println(x);// 0

		// 5. 증감연산자
		// 1증가(++), 1감소(--)
		// 유형 : ++x, x++(대입 후 연산), --x(연산 후 대입), x--

		int x = 0, y = 10, z = 50, sum1, sum2;

		x++;
		System.out.println(x);// 1

		x--;
		System.out.println(x);// 0

		++x;
		System.out.println(x);// 1

		sum1 = x + ++y - z--;
		System.out.println(sum1);// 1+11-50(49는 기억) = -38

		sum2 = sum1++ - y;
		System.out.println(sum2);// -38(-37) - 11 = -49

		z--;
		System.out.println(z);// 48

		// 문제] a=0, b=10, c=20, d=30, e=40, sum1=0, sum2=0;
		int a = 0, b = 10, c = 20, d = 30, e = 40, sum1 = 0, sum2 = 0;

		b++;
		System.out.println(b);// 11

		sum1 = c-- + --e - a++; // 대입할 대상이 있으면 기억(기억하는 것은 다음 연산에 참여)
		System.out.println(sum1);// 20(19) + 39 - 0(1) = 59

		sum2 = sum1 - c + a;
		System.out.println(sum2);// 59 - 19 + 1 = 41

		sum1 = c++ - a-- + b - --d;
		System.out.println(sum1);// 19(20) - 1(0) + 11 - -29 = 0

		sum2 = --c + e++;
		System.out.println(sum2);// 19 + 39(40) = 58

		sum2 = c + e;
		System.out.println(sum2);// 19 + 40 = 59

		// 6. bit연산자(and, or, not, exor, nand, nor, <<, >>, >>>)
		// 통신공학, 전자공학, 전기공학, 반도체공학 등에서 임베디드 시스템으로 사용
		int x = 20;
		int y = 30;
		//         128 64 32 16 8 4 2 1    (8비트로 표현한 것.)
		// x 20 =>  0   0  0  1 0 1 0 0
		// y 30 =>  0   0  1  1 1 1 1 0
		//------------------------------
		// AND연산      0   0  0  1 0 1 0 0   -> 16 + 4 = 20 //참참 일때만 참.
		//  OR연산      0   0  0  1 1 1 1 0   -> 16 + 8 + 4 + 2 = 30 // 하나만 참 이면 참.
		// EXOR연산    0   0  0  0 1 0 1 0   -> 8 + 2 = 10 // 같0 다1.
		
		int z = x & y;
		System.out.println("&연산 : " + z);// 20
		
		int z2 = x | y;
		System.out.println("|연산 : " + z2); // 30
		
		int z3 = x ^ y;
		System.out.println("ExoR연산 : " + z3); // 10
		
		int z4 = x << 3; // x2만큼 늘어납니다.
		System.out.println("shift연산 : " + z4); // 160
        //                 128 64 32 16 8 4 2 1    (8비트로 표현한 것.)
		//         x 20 =>  0   0  0  1 0 1 0 0
		//shift연산              0  0   0  1  0 1 0 0 0 -> 32 + 8 = 40 
		//shift연산              0  0   1  0  1 0 0 0 0 -> 64 + 16 = 80 
		//shift연산              0  1   0  1  0 0 0 0 0 ->128 + 32 = 160 
		
		int z5 = y >> 3; // x2만큼 줄어듭니다.
		System.out.println("shift연산 : " + z5); // 3
        //                 128 64 32 16 8 4 2 1    (8비트로 표현한 것.)
		//         y 30 =>  0   0  0  1 1 1 1 0
		//shift연산                    0   0  0  0 1 1 1 1 0 -> 15 
		//shift연산                            0  0  0 0 1 1 1 1 0 -> 7 
		//shift연산                                  0  0 0 0 1 1 1 1 0 -> 3 

'Bitcamp > BITCAMP - Java' 카테고리의 다른 글

3일차 - 과제  (0) 2019.06.26
3일차  (0) 2019.06.26
2일차 - 과제  (0) 2019.06.26
2일차 - GUISample1  (0) 2019.06.25
1일차  (0) 2019.06.24
And