Operation_03.java
class Qu2_8{
public static void main(String[] args){
//1. byte형 변수 b1 선언, 80 으로 초기화
byte b1 = 80;
//2. byte형 변수 b2 선언, 100 으로 초기화
byte b2 = 100;
//3. long형 변수 lo1 선언, 642 으로 초기화
long lo1 = 642L;
//4. lo1과 b1의 합을 변수 result1에 저장
long result1 = lo1 + b1;
//5. result1의 값 출력
System.out.println(result1); // 642L + 80L = 722
//6. b1과 b2의 합을 변수 result2에 저장
//byte + byte => int + int => int
int result2 = b1 + b2;
//7. result2의 값 출력
System.out.println(result2); // 180
//8. byte형 변수 result3을 선언, b1과 b2의 합으로 초기화
//byte + byte => int + int => int
byte result3 = (byte)(b1 + b2);
//9. result3의 값 출력
System.out.println(result3); //
//10. 변수 lo2 선언, 60000과 80000의 곱을 저장.
//long lo2 = 60000 * 80000 //int로 인식해서 오버플로우 발생
//long lo2 = 60000L * 80000L // 하나만 L붙여줘도 나머지도 바뀜
long lo2 = 60000L * 80000;
//11. lo2에 저장된 값 출력
System.out.println(lo2); //4800000000
}
}
문제9는 여기 참고 : https://coderanch.com/t/411768/java/converting-int-byte
// 사용자가 값을 두개를 주면 사용자가 원하는 숫자의 몫과 나머지 구하기
int a = 94;
int b = 11;
int share = a/b;
int remain = a%b;
// a를 b로 나눈 몫은 share이고 나머지는 remain이다.
System.out.println(a+"을 "+b+"로 "+"나눈 몫은 "
+share+"이고 나머지는 "+remain+"이다.")
int shift = 10;
System.out.println(10<<33); //10 * 2^33 = 10 * 2^32 * 2 => 20
System.out.println(10>>3); //10 / 2^3 = 1
long shift = 200L;
System.out.println(200<<65); // 200 * 2^65 = 200 * 2^64 * 2 = 400
10 ---- (0)0001010
10<<2 (0)0101000
10>>2 (0)0000001
비워진 자리가 채워질때 앞의 부호(괄호안 숫자)에 따라 빈칸이 채워진다
여기서는 부호가 0이라 빈자리도 0으로 채워진다
이러면 8칸 움직일때 한칸한칸 차례로 0,1따라 달리 채워지면서 결국 원래모습이 된다
class Qu2_10{
public static void main(String[] args){
//1. 변수 f01 선언, 0.1f 로 초기화
float f01 = 0.1f;
//2. 변수 f02 선언, 10.0f 로 초기화
float f02 = 10.0f;
//3. 변수 do01 선언, 0.1 로 초기화
double do01 = 0.1;
//4. 변수 do02 선언, 10.0 로 초기화
double do02 = 10.0;
//5. 변수 str1 선언, "화이팅" 초기화
String str1 = "화이팅";
//6. "변수 f01과 do01의 값은 같은가?"의 결과를 출력
//f01 = 0.1f, do01 = 0.1
//f01 = do01 --프로그래밍--> f01 == do01
System.out.println(f01==do01);
//false.
//1) double 타입은 2^n으로 표현이 됨, 연산은 빠르나 근사치.
//2) float 타입은 정확한 값.
//3) 둘다 2^n으로 표현되는 값이면 true로 나온다.
//7. "변수 f02과 do02의 값은 같은가?"의 결과를 출력
//f02 = 10.0f, do02 = 10.0
//f02 = do02 --프로그래밍--> f02 == do02
System.out.println(f02==do02);
//true. 소숫점 이하가 없이 0에서 끝나기때문에
★★
//8. "str1의 값이 "화이팅"과 같은 값인가?"의 결과를 출력
//등가비교연산자는 기본형, 참조형 둘다 사용 가능
//str1 = "화이팅", "화이팅" 둘의 비교
//str1 = "화이팅" --프로그래밍--> str1 == "화이팅"
System.out.println(str1=="화이팅"); //true
//★ 비교연산자: 문자열이 완전히 같은 것인지 비교
System.out.println(str1.equals("화이팅"); //true
//★ equals(): 문자열의 내용이 같은지 비교. (p.182)
}
}
int a = 3;
int b = 5;
// 3 : 00000011
// 5 : 00000101
// 3&5 : 00000001
// 3|5 : 00000111
// 3^5 : 00000110
System.out.println(a&b); //1
System.out.println(a|b); //7
System.out.println(a^b); //6
int a2 = 7;
//Q. 5 이상 15이하일 때 true가 나오게 해라
//5 <= a2 <= 15 (수학적 연산)
//5 <= a2 그리고 a2 <= 15 (프로그래밍 연산)
//(5 <= a2) && (a2 <= 15)
System.out.println( (5 <= a2) && (a2 <= 15) ); // true
class Ex2_8{
public static void main(String[] args){
boolean b1 = true;
boolean b2 = false;
System.out.println( b1 && b2); // false.
int in01 = 10;
boolean re1 = in01 % 2 == 0;
//int01이 2의 배수인가? -> true
boolean re2 = in01 % 4 == 0;
//int01이 4의 배수인가? -> false
System.out.println(re1 || re2);
//|| or결합이기때문에 둘중 하나만 true여도 됨 --> -> true
}
}
class Qu2_11{
public static void main(String[] args){
//1. 변수 ch01 선언, 원하는 문자로 초기화
char ch01 = 'A';
//2. 변수 ch01의 값이 영어 대문자일 때 true인 조건식
// 'A' <= ch01 <= 'Z'
System.out.println('A' <= ch01 && ch01 <= 'Z'); //true
//3. 변수 ch01의 값이 영어 소문자일 때 true인 조건식
// 'a' <= ch01 <= 'z'
System.out.println('a' <= ch01 && ch01 <= 'z'); //false -> ch01 대문자
//4. 변수 ch01의 값이 영문자일 때 true인 조건식
// 'A' <= ch01 <= 'Z' 또는 'a' <= ch01 <= 'b'
System.out.println(( 'A' <= ch01 && ch01 <= 'Z' ) || ( 'a' <= ch01 && ch01 <= 'z' )); //true
//5. 변수 ch01의 값이 ASCII값 숫자 때 true인 조건식
// '0' <= ch01 <= '9'
System.out.println('0' <= ch01 && ch01 <= '9'); // false -> ch01 대문자
}
}
int a3 = -10;
int abs = a3 >= 0 ? a3 : -a3;
System.out.println(abs); // 절대값 구하는 프로그래밍
class Ex2_9{
public static void main(String[] args){
//변수에 저장된 값의 절대값을 구하는 코드이다.
int num = -10;
int result = num >= 0 ? num : -num;
System.out.println(result); // num의 초기화 값과 상관없이 절대값이 출력됨.
}
}
class Qu2_12{
public static void main(String[] args){
//1. 변수 x를 선언 및 'B'의 값으로 초기화
char x = 'B';
//2. 변수 result 선언, x의 값이 대문자일 때 "대문자"를, 소문자일 때 "소문자"를, 그 외에는 "영문자 아님" 을 저장해라.
//x가 대문자 -y-> "대문자"
// -n-> x가 소문자? -y-> "소문자"
// -n-> "영문자 아님"
//대문자: 'A' <= x && x <= 'Z'
//소문자: 'a' <= x && x <= 'z'
//정답
//1. 대문자 조건식: 'A' <= x && x <= 'Z';
//2. 소문자 조건식: 'a' <= x && x <= 'z';
String result = 'A' <= x && x <= 'Z' ? "대문자" : 'a' <= x && x <= 'z' ? "소문자" : "영문자 아님";
}
}
int b4 = 10;
//1. b4의 값에 3을 곱한 결과를 b4에 저장해 주세요.
b4 = b4 * 3; // 얘는 int니까 상관없는데 char면 아래와 같이 캐스팅 필수
b4 *= 3;
//int b4 *= 3; 선언과 동시에 못함
char c3 = 'A';
//2. c3의 값에 5을 더한 결과를 c3에 저장해 주세요.
c3 = (char)(c3 + 5);
c3 += 5; //위랑 같은 값
//c3 += 5+3; 이건 틀린 식