국비지원교육 (22.01-22.07)/강의노트
22-02-23(수) 020일차 [Java] java.util.Math
옌炎
2022. 4. 20. 02:07
728x90
수업내용
1교시 (09:30-10:20)
- 기억노트
2022-02-15
--------------------------
자바에서 자원을 메모리에 올리는 방법
1. static 키워드
2. new 키워드(연산자)
Aclass a = new aClass();
3. 상속
extends 키워드 클래스 단일 상속
implements 키워드 인터페이스 다중 상속
2022-02-16
--------------------------
생성자 Constructor
------------------
1. 클래스 이름에 () 소괄호를 붙이고 {} 중괄호를 가지고 있는 함수이다.
2. 생성자는 클래스를 메모리에 올릴 때(new 인스턴스, 상속 inheritance)
========================================
해당 객체에 있는 멤버 변수를 초기화한다.
========================================
3. 생성자는 일반 함수의 오버로딩처럼 생성자 오버로딩을 할 수 있다.
4. 생성자 안에서 사용하는 this, super 키워드, this(), super() 함수를 사용할 수 있다.
this 키워드, this() 함수는 자기자신 클래스에서 사용한다.
super 키워드, super() 함수는 상속 관계에서 부모 클래스에 있는 자원 및 생성자를 호출할 때 사용한다.
================================================
5. 생성자를 호출할 때는 매개변수가 같은 것을 호출해야 한다.
================================================
6. default Constructor 소스 코드에 매개 변수가 업슨 생성자를 구현하지 않으면
자바 버추얼 머신이 만들어주는 기본 생성자
2022-02-23
--------------------------
자바에서 자원을 메모리에 올리는 방법
1. static 키워드
2. new 키워드(연산자)
Aclass a = new aClass();
3. 상속
extends 키워드 클래스 단일 상속
implements 키워드 인터페이스 다중 상속
4. 추상 클래스
getInstance()
5. Class.forName("클래스이름").newInstance()
2교시 (10:30-11:20)
- Exam_Math
package a.b.c.ch5;
import java.text.DecimalFormat;
public class Exam_Math {
double de = Math.E;
public static void main(String[] args) {
// TODO Auto-generated method stub
// java.lang.Math
// public static final double E
double e = Math.E;
System.out.println("e >>> : " + e);
System.out.println("e >>> : " + Math.round(e));
System.out.println("e >>> : " + Math.round(e*100));
System.out.println("e >>> : " + Math.round(e*100)/100.0);
System.out.println("e >>> : " + Math.round(e*1000));
System.out.println("e >>> : " + Math.round(e*1000)/1000.0);
System.out.println("e >>> : " + Math.round(e*100000));
System.out.println("e >>> : " + Math.round(e*100000)/100000.0);
System.out.println("e >>> : " + String.format("%.2f", e));
System.out.println("e >>> : " + String.format("%.3f", e));
System.out.println("e >>> : " + String.format("%.5f", e));
DecimalFormat df = new DecimalFormat("#.###");
System.out.println("df >>> : " + df.format(1.234567));
System.out.println("df >>> : " + df.format(e));
System.out.println("e >>> : " + Math.floor(e));
System.out.println("Math.E >>> : " + Math.E);
System.out.println("Math.E >>> : " + Math.round(Math.E));
System.out.println("Math.E >>> : " + Math.floor(Math.E));
// java.lang.Math
// public static final double PI
double pi = Math.PI;
System.out.println("pi >>> : " + pi);
}
}
3교시 (11:30-12:20)
- Exam_Math (계속)
package a.b.c.ch5;
import java.text.DecimalFormat;
public class Exam_Math {
double de = Math.E;
public static void main(String[] args) {
// TODO Auto-generated method stub
// java.lang.Math
// public static final double E
double e = Math.E;
System.out.println("e >>> : " + e);
System.out.println("e >>> : " + Math.round(e));
System.out.println("e >>> : " + Math.round(e*100));
System.out.println("e >>> : " + Math.round(e*100)/100.0);
System.out.println("e >>> : " + Math.round(e*1000));
System.out.println("e >>> : " + Math.round(e*1000)/1000.0);
System.out.println("e >>> : " + Math.round(e*100000));
System.out.println("e >>> : " + Math.round(e*100000)/100000.0);
System.out.println("e >>> : " + String.format("%.2f", e));
System.out.println("e >>> : " + String.format("%.3f", e));
System.out.println("e >>> : " + String.format("%.5f", e));
DecimalFormat df = new DecimalFormat("#.###");
System.out.println("df >>> : " + df.format(1.234567));
System.out.println("df >>> : " + df.format(e));
System.out.println("e >>> : " + Math.floor(e));
System.out.println("Math.E >>> : " + Math.E);
System.out.println("Math.E >>> : " + Math.round(Math.E));
System.out.println("Math.E >>> : " + Math.floor(Math.E));
// java.lang.Math
// public static final double PI
double pi = Math.PI;
System.out.println("pi >>> : " + pi);
// round : 반올림 : 전달받은 실수를 소수점 첫 번째 자리에서 반올림한 결과 반환
double round = Math.round(10.1);
System.out.println("round >>> : " + round);
// ceil : 올림 : 전달받은 실수보다 큰 정수 중 가장 작은 정수 반환
double ceil = Math.ceil(10.1);
System.out.println("ceil >>> " + ceil);
// floor : 내림 : 전달받은 실수보다 작은 정수 중에 가장 큰 정수 반환
double floor = Math.floor(10.9);
System.out.println("floor >>> : " + floor);
// pow : 제곱 연산 : (5, 2) -> 25
double pow = Math.pow(5, 2);
System.out.println("pow >>> : " + pow);
// abs : 절대값 반환 : absolute number
System.out.println("Math.abs(-10) >>> : " + Math.abs(-10));
System.out.println("Math.abs(0) >>> : " + Math.abs(0));
System.out.println("Math.abs(10) >>> : " + Math.abs(10));
// max : 큰 값
int max = Math.max(2, 3);
System.out.println("max >>> : " + max);
// min : 작은 값
int min = Math.min(2, 3);
System.out.println("min >>> : " + min);
}
}
- Exam_Math_1
package a.b.c.ch5;
public class Exam_Math_1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// public static double random()
double r = Math.random();
System.out.println("r >>> : " + r);
double r1 = Math.random() * 10;
System.out.println("r1 >>> : " + r1);
int r2 = (int)(Math.random() * 10);
System.out.println("r2 >>> : " + r2 );
int r3 = (int)(Math.random() * 100);
System.out.println("r3 >>> : " + r3);
}
}
- Exam_Math_2 (내가 푼 것)
package a.b.c.ch5;
import java.util.Scanner;
public class Exam_Math_2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// public static double random()
int answer = (int)(Math.random() * 100) + 1;
// System.out.println("Answer >>> : " + answer);
int input = 0;
int count = 0;
Scanner sc = new Scanner(System.in);
for (int i=0; i < 100; i++) {
System.out.println("1과 100 사이의 값을 입력하시오 >>> : ");
count++;
input = sc.nextInt();
if (answer == input) {
System.out.println("정답!!! >>> : " + count + " 번");
break;
} else if (answer > input) {
System.out.println("up");
} else if (answer < input) {
System.out.println("down");
}
if(count == 10) {
break;
}
}
}
}
4교시 (12:30-13:20)
- Exam_Math_2
package a.b.c.ch5;
import java.util.Scanner;
public class Exam_Math_2 {
public static void main(String[] args) {
// public static double random()
int answer = (int)(Math.random() * 100) + 1;
System.out.println("answer >>> : " + answer);
int input = 0;
int count = 0;
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
for (int i=0; i < 10; i++) {
System.out.println("1 과 100 사이의 값을 입력하시오 >>> : ");
count++;
System.out.println("count >>> : " + count);
input = sc.nextInt();
System.out.println("input >>> : " + input);
if (answer == input) {
System.out.println("answer >>> : " + answer);
System.out.println("input >>> : " + input);
System.out.println("정답 !!! >>> : " + count + " 번");
break;
}else if (answer > input) {
System.out.println("answer >>> : " + answer);
System.out.println("input >>> : " + input);
System.out.println("더 작은 수를 입력하시오 !!! >>> : \n");
}else if (answer < input) {
System.out.println("answer >>> : " + answer);
System.out.println("input >>> : " + input);
System.out.println("더 큰 수를 입력하시오 !!! >>> : \n");
}
if (count == 10) {
System.out.println("count >>> : " + count);
break;
}
}
sc.close();
}
}
- Exam_Math_3 (내가 푼 것)
package a.b.c.ch5;
import java.util.Scanner;
public class Exam_Math_3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int answer = (int)(Math.random() * 100) + 1;
System.out.println("Answer >>> : " + answer);
int input = 0;
boolean bool = true;
Scanner sc = new Scanner(System.in);
// while
while (bool) {
System.out.println("1과 100 사이의 값을 입력하시오 >>> : ");
input = sc.nextInt();
if (answer == input) {
System.out.println("정답 !!! >>> : ");
bool = false;
} else if (answer > input) {
System.out.println("Up");
} else if (answer < input) {
System.out.println("Down");
}
}
}
}
- Exam_Math_3
package a.b.c.ch5;
import java.util.Scanner;
public class Exam_Math_3 {
public static void main(String[] args) {
// public static double random()
int answer = (int)(Math.random() * 100) + 1;
System.out.println("answer >>> : " + answer);
int input = 0;
int count = 0;
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
while (true){
count++;
System.out.println("1 과 100상의 값을 입력하세요 !!");
input = sc.nextInt();
if (answer == input){
System.out.println("정답 입니다. !! " + count++ + " 번");
break;
}else if (answer < input){
System.out.println("더 작은 수를 입력하세요 !!\n");
}else if (answer > input){
System.out.println("더 큰 수를 입력하세요 !!\n");
}
if (count == 10) {
break;
}
}
sc.close();
}
}
- Exam_Math_4
package a.b.c.ch5;
import java.util.Scanner;
public class Exam_Math_4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int answer = (int)(Math.random() * 100) + 1;
System.out.println("Answer >>> : " + answer);
int input = 0;
int count = 0;
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
// do while
do {
count++;
System.out.println("1과 100 사이의 값을 입력하시오 >>> : ");
input = sc.nextInt();
if (answer > input) {
System.out.println("더 큰 수를 입력하세요 !! \\n");
} else if (answer < input) {
System.out.println("더 작은 수를 입력하세요 !!\\n");
} else {
System.out.println("GOOD !!");
System.out.println("시도횟수는 " + count + " 번 입니다.");
break;
}
if (count == 10) {
break;
}
}
while (true);
sc.close();
}
}
5교시 (14:30-15:20)
- Exam_Math_5
package a.b.c.ch5;
public class Exam_Math_5 {
public static int max(int a, int b, int c) {
System.out.println("Exam_Math_5.max() 함수 진입 >>> : ");
System.out.println("Exam_Math_5.max() a >>> : " + a);
System.out.println("Exam_Math_5.max() b >>> : " + b);
System.out.println("Exam_Math_5.max() c >>> : " + c);
int max = a;
System.out.println("Exam_Math_5.max() max >>> : " + max);
if (b >= max) {
System.out.println("Exam_Math_5.max() if (b >= max) b >>> : " + b);
System.out.println("Exam_Math_5.max() if (b >= max) max >>> : " + max);
max = b;
System.out.println("Exam_Math_5.max() if (b >= max) max = b max");
}
if (c >= max) {
System.out.println("Exam_Math_5.max()");
System.out.println("Exam_Math_5.max()");
max = c;
System.out.println("Exam_Math_5.max()");
}
System.out.println("Exam_Math_5.max()");
return max;
}
public static int max(int imax[]) {
System.out.println("Exam_Math_5.max(int imax[]) 함수 진입 >>> : ");
System.out.println("Exam_Math_5.max(int imax[]) imax >>> : " + imax);
for (int i=0; i < imax.length; i++) {
System.out.println("Exam_Math_5.max(int imax[]) imax[" + i + "] >>> : "+ imax[i]);
}
int max = imax[0];
System.out.println("Exam_Math_5.max(int imax[]) max >>> : "+ max);
for (int i=0; i < imax.length; i++) {
System.out.println("Exam_Math_5.max(int imax[]) >>> : " + imax[i]);
if (imax[i] > max) {
System.out.println("Exam_Math_5.max(int imax[]) >>> : " + imax[i] + " > " + max);
max = imax[i];
System.out.println("Exam_Math_5.max(int imax[]) >>> : " + max);
}
}
System.out.println("Exam_Math_5.max(int imax[]) >>> : " + max);
return max;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Exam_Math_5.main() 함수 진입 >>> : ");
int max = Exam_Math_5.max(11, 2, 3);
System.out.println("Exam_Math_5.max(11, 2, 3) 함수 호출한 결과 값 max >>> : " + max);
System.out.println("max >>> : " + max);
int imax[] = {5, 3 , 6, 7, 9, 8};
System.out.println("imax >>> : " + imax);
int maxArray = Exam_Math_5.max(imax);
System.out.println("Exam_Math_5.max(imax) 함수 호출한 결과 값 max >>> : " + maxArray);
System.out.println("maxArray >>> : " + maxArray);
}
}
6교시 (15:30-16:20)
- Exam_Math_6
package a.b.c.ch5;
public class Exam_Math_6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 로또 번호를 생성해서 담으려고 배열을 만든다.
// 번호가 6개 이니 방 6개로 만든다. 정수형
int lo[] = new int[6];
System.out.println("lo >>> : " + lo);
for (int i=0; i < lo.length; i++) {
System.out.println("lo[" + i + "] >>> : " + lo[i]);
}
// 6개 방에 로또 번호를 넣기
for (int i=0; i < lo.length; i++) {
// Math.random() 함수 이용해서 로또 번호 생성
// 1 ~ 45번까지
// 6개 방에 넣기
int ii = (int)(Math.random()*45) + 1;
System.out.println("\n(int)(Math.random()*45) + 1 :: ii >>> : " + ii);
lo[i] = ii;
System.out.println("lo[" + i + "] >>> : " + lo[i]);
// 인덱스 0 1 2 3 4 5
// 값 35 18 17 10 30 27
// 먼저 생성된 로또 번호 lo[i]
// 새로들어오 번호 같으면 먼저 만들어진 번호 삭제
for (int j=0;j < i; j++) {
System.out.println("\nfor >>> : jjjjj :: " + lo[j]);
System.out.println("for >>> : iiiii :: " + lo[i]);
if (lo[j] == lo[i]) {
System.out.println("if >>> : " + lo[j] + " : " + lo[i]);
i--;
break;
}
}
}
for (int i=0; i < lo.length; i++) {
System.out.print(lo[i] + " ");
}
}
}
- Exam_Math_6_1
package a.b.c.ch5;
public class Exam_Math_6_1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int n=0; n < 5; n++) {
int lo[] = new int[6];
for (int i=0; i < lo.length; i++) {
lo[i] = (int)(Math.random()*45) + 1;
for (int j=0; j < i; j++) {
if (lo[j] == lo[i]) {
System.out.println("if >>> : " + lo[j] + " : " + lo[i]);
i--;
break;
}
}
}
for (int i=0; i < lo.length; i++) {
System.out.print(lo[i] + " ");
}
System.out.println();
}
}
}
7교시 (16:30-17:20)
- Exam_Math_6
package a.b.c.ch5;
public class Exam_Math_6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 로또 번호를 생성해서 담으려고 배열을 만든다.
// 번호가 6개 이니 방 6개로 만든다. 정수형
int lo[] = new int[6];
System.out.println("lo >>> : " + lo);
for (int i=0; i < lo.length; i++) {
System.out.println("lo[" + i + "] >>> : " + lo[i]);
}
// 6개 방에 로또 번호를 넣기
for (int i=0; i < lo.length; i++) {
// Math.random() 함수 이용해서 로또 번호 생성
// 1 ~ 45번까지
// 6개 방에 넣기
int ii = (int)(Math.random()*45) + 1;
System.out.println("\n================================================");
System.out.println("(int)(Math.random()*45) + 1 :: ii >>> : " + ii);
System.out.println("================================================");
lo[i] = ii;
System.out.println("lo[" + i + "] >>> : " + lo[i]);
// 인덱스 0 1 2 3 4 5
// 값 35 18 17 10 30 27
// 먼저 생성된 로또 번호 lo[i]
// 새로들어오 번호 같으면 먼저 만들어진 번호 삭제
for (int j=0;j < i; j++) {
System.out.println("\nfor >>> : jjjjj :: " + lo[j]);
System.out.println("for >>> : iiiii :: " + lo[i]);
if (lo[j] == lo[i]) {
System.out.println("if >>> : " + lo[j] + " : " + lo[i]);
i--;
break;
}
}
}
System.out.println("\n================================================");
for (int i=0; i < lo.length; i++) {
System.out.print(lo[i] + " ");
}
}
}
8교시 (17:30-18:30)
- Exam_Math_8
package a.b.c.ch5;
import java.util.UUID;
public class Exam_Math_8 {
/*
자바 중복되지 않는 고유 키
UUID(Universally unique identifier)
숫자와 영어(소문자)만을 조합하여 임시 비밀번호 생성
32개 문자 + '-' = 36개
*/
public static String tempPW(int len) {
String u = UUID.randomUUID().toString();
System.out.println("u >>> : " + u);
u = u.replace("-","").substring(0, len);
System.out.println("u >>> : " + u);
return u;
}
public static String randomPW(int len) {
char c[] = {
'1','2','3','4','5','6','7','8','9','0',
'A','B','C','D','E','F','G','H','I','J',
'K','L','M','N','O','P','Q','R','S','T',
'U','V','W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j',
'k','l','m','n','o','p','q','r','s','t',
'u','v','w','x','y','z',
'!','@','#','&'
//'!','@','#','$','%','^','&','*','(',')'
};
String p = "";
for (int i=0; i < len; i++) {
int a = (int)(Math.random()*(c.length));
p += c[a];
}
return p;
}
public static String certificNum(int len) {
char c[] = {'1','2','3','4','5','6','7','8','9'};
String p = "";
for (int i=0; i < len; i++) {
int a = (int)(Math.random()*(c.length));
p += c[a];
}
return p;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1 = Exam_Math_8.tempPW(8);
String s2 = Exam_Math_8.randomPW(8);
String s3 = Exam_Math_8.certificNum(6);
System.out.println("UUID >>> : " + s1);
System.out.println("randomPW >>> : " + s2);
System.out.println("certificNum" + s3);
}
}
Notes
728x90