๐์ ์ฉํ ์ ๋ณด
ํด๋น ๋ณ์ Rename (eclipse)
- ๋ณ์ ์ฐํด๋ฆญ - Refacto - Rename
Alt
+Shift
+R
import ์ ๋ฆฌ
ctrl
+shift
+o
ctrl
+ shift
+ f
์ ํจ๊ป ์์ฃผ ๋๋ฌ์ฃผ์.double์ ์์์ ์ค์ฐจ ๋ณด์ (๋ถ๋ ์์์ ๋ณด์ )
- ๋ถ๋ ์์์ ์ ๊ฐ์๋ถ 15์๋ฆฌ๋ฅผ ๋์ด์๋ฉด ์ค๋ฒ ํ๋ก์ฐ๊ฐ ๋ฐ์ํ๊ฒ ๋์ด ๊ฐ์ ์ค์ฐจ๊ฐ ๋ฐ์ํ๊ฒ ๋๋ค (๋ถ๋์์์ ์์ ํํ ๋ฒ์)
- 10์ง์๋ก๋ ์ ํ ์์์ด์ง๋ง, 2์ง์๋ก ๋ฌดํ ์์์ธ ๊ฒฝ์ฐ ์ค์ฐจ ๋ฐ์
epsilon ๊ฐ
์ ์ด์ฉํ์ฌ ์ค์ฐจ ๋ณด์ ๊ฐ๋ฅ์ Number.EPSILON์ 2.220446049250313e-16์ผ๊น?
double ํ ๋ถ๋ ์์์ ๊ฐ์๋ถ์์ 0์ ์ ์ธํ๊ณ ๊ฐ์ฅ ์์ ์ซ์๋ ๋ฌด์์ผ๊น?
๋ฐ๋ก 2-52, 0.0000000000000002220446049250313(2.220446049250313e-16)์ด๋ค.
(๊ฐ์๋ถ๋ ์์์ ์ดํ๋ฅผ ํํํ๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ 252๊ฐ ์๋๋ค.)
๊ทธ ๋ง์ 0 < x < 0.0000000000000002220446049250313, ์ฆ x๋ ํํํ์ง ๋ชปํ๋ ์ซ์๊ฐ ๋๋ค.๊ทธ๋์ Number.EPSILON์ด 2-52, ์ฆ 2.220446049250313e-16๊ฐ ๋ ๊ฒ์ด๋ค.
(ES6) Number.EPSILON์ ์ 2.220446049250313e-16์ธ๊ฐ?๋ค์ด๊ฐ๊ธฐ์ ์์ Number.EPSILON์ด ๋ญ์ง ๋ชจ๋ฅด๋ ์ฌ๋์ ํด๋น ๋งํฌ๋ฅผ ์ฐธ์กฐํ๊ณ ์ค์. ๋ถ๋์์์ ์ด ๋ญ์ง ๋ชจ๋ฅด๋ ์ฌ๋์ ํด๋น ๋งํฌ๋ฅผ ์ฐธ์กฐํ๊ณ ์ค์. ์๋ฐ์คํฌ๋ฆฝํธ์์ ์ซ์์ ์๋ฃํ์?C๋ Java๋ฅผ ์ ํด๋ณธ ์ฌ๋์ด๋ผ๋ฉด ์ซ์์ ์๋ฃํ์ ํฌ๊ฒ ๋ ๊ฐ์ง๋ก ๋๋ ์ ์์ ๊ฒ์ด๋ค.https://perfectacle.github.io/2017/08/04/ES6-EPSILON/
๐ก5์ฅ ํผ๋๋ฐฑ Tip
๐ 6์ฅ ๋ณต์ ํด๋์ค๋ฅผ ์ฌ์ฉํ ๊ฐ๋ฐ
์์ค ํ์ผ์ ๋ถ๋ฆฌ
๋๊ท๋ชจ ๊ฐ๋ฐ์์๋ ํผ์ ๊ฐ๋ฐ์ด ์ด๋ ต๊ณ , ๋ถ๋ด ํ์ ํ์ฌ ์์ค๋ฅผ ๋ชจ๋ํ ํด์ผ ํ๋ค.
(1๊ฐ์ ์์ค ํ์ผ๋ก๋ ๊ฐ๋ฐ์ ํ๊ณ)
๊ณ์ฐ๊ธฐ ํ๋ก๊ทธ๋จ
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 2;
int total = add(a, b);
int delta = minus(a, b);
System.out.println("๋ํ๋ฉด " + total + ", ๋นผ๋ฉด " + delta);
}
private static int add(int a, int b) {
return a + b;
}
private static int minus(int a, int b) {
return a - b;
}
}
๊ณ์ฐ ํด๋์ค ๋ถ๋ฆฌ
public class CalcLogic {
public static int add(int a, int b) {
return a + b;
}
public static int minus(int a, int b) {
return a - b;
}
}
import com.example.logics.CalcLogic;
public class Main {
public static void main(String[] args) {
int a = 10;
int b = 20;
int total = CalcLogic.add(a, b);
int delta = CalcLogic.minus(a, b);
System.out.println("๋ํ๋ฉด " + total + ", ๋นผ๋ฉด " + delta);
}
}
ํจํค์ง (package)
Java์์๋ ๊ฐ ํด๋์ค๋ฅผ ํจํค์ง(package) ๋ผ๋ ๊ทธ๋ฃน์ ์์์์ผ, ๋ถ๋ฅ, ๊ด๋ฆฌ๊ฐ ๋๋๋ก ํ ์ ์๋ค.
ํจํค์ง ๋ถ๋ฆฌํ๊ธฐ



๋ค๋ฅธ ํจํค์ง์ ์๋ ํด๋์ค ํธ์ถ
import๊ฐ ์๋ ๊ฒฝ์ฐ
com.example.logics.CalLogic
์ด์ฒ๋ผ ๋ชจ๋ ํจํค์ง ์ฃผ์์ ํด๋์ค๋ช
์ ๋ช
์ํด์ ํธ์ถ
์ด๋ฆ ๊ณต๊ฐ (name space)
์์ ์ด ์์ฑํ ํด๋์ค์ ๋ํด, ๊ฐ๋ฐ์๊ฐ ์์ ๋ก์ด ์ด๋ฆ์ ์ง์ ํ ์ ์๋ค.
ํจํค์ง๋ช ์์ฒด์ ์ถฉ๋์ ํผํ๊ธฐ ์ํด ๋ณด์ ํ ๋๋ฉ์ธ์ ์๋ค๋ฅผ ๋ฐ๊พธ์ด ํจํค์ง๋ช ์ผ๋ก ์ฌ์ฉํ๋ค.
ํจํค์ง๋ช
๋ค๋ฅธ ํจํค์ง์ ์๋ ํด๋์ค ์์น import


Java API
Java API ์ฌ์ฉ ์์
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] heights = {170, 240, 300, 15, 320};
Arrays.sort(heights); //java sort ์ ๋ ฌ API ์ฌ์ฉ
for (int height : heights) {
System.out.println(height);
}
}
}

java API์ ํฌํจ๋ ๋ํ์ ์ธ ํจํค์ง

API reference
์ถ๋ ฅ ํฌ๋ฉง

๐์ฐ์ต ๋ฌธ์
6-1

import java.lang.Thread;
public class Exam6_1 {
public static void main(String[] args) throws Exception {
System.out.println("3์ด๊ฐ ๊ธฐ๋ค๋ฆผ!");
Thread.sleep(3000);
System.out.println("๋");
}
}
6-2

public class Exam6_2 {
public static void main(String[] args) {
for (int i = 1; i < 10; i++) {
for (int j = 2; j < 10; j++) {
if (j == 9) {
System.out.printf("%d * %d = %2d", j, i, j * i);
} else {
System.out.printf("%d * %d = %2d \t", j, i, j * i);
}
}
System.out.println();
}
}
}

6-3

public class Exam6_3 {
public static void main(String[] args) {
for (int i = 0; i < 60; i++) {
for (int j = 1; j <= 12; j++) {
if (j == 12) {
System.out.printf("%d:%02d", j, i);
} else {
System.out.printf("%d:%02d ", j, i);
}
}
System.out.println();
}
}
}


6-4


import java.util.Random;
import java.util.Scanner;
public class Exam6_4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random random = new Random();
System.out.println("๋์ง ํ์๋ฅผ ์
๋ ฅํด์ฃผ์ธ์");
long tries = sc.nextLong();
int hits = 0;
for (int i = 0; i < tries; i++) {
// -1.0 ~ +1.0 ๋์ ์์ฑ
double x = random.nextDouble() * 2 - 1;
double y = random.nextDouble() * 2 - 1;
if (Math.sqrt(x * x + y * y) <= 1) {
hits++;
}
}
double piEstimate = 4 * hits / (double) tries;
System.out.println(piEstimate);
}
}

Uploaded by N2T