[Java κΈ°μ΄] 6. 볡μ ν΄λμ€λ₯Ό μ¬μ©ν κ°λ°
πμ μ©ν μ 보
ν΄λΉ λ³μ 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