Math
ํด๋์ค๋ ์ํ(Mathematics)๊ณผ ๊ด๋ จ๋ ๋ฉ์๋๋ฅผ ์ ๊ณตํ๋ ํด๋์ค๋ก ์์ฑ์๊ฐ ์์ง๋ง private ์ผ๋ก ์ ์ธ๋์ด ์ธ๋ถ์์ ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ ์๋ค. ๋ชจ๋ ๋ฉ์๋๊ฐ staic์ผ๋ก ์ ์ธ๋์ด ์์ผ๋ฏ๋ก ๊ฐ์ฒด๋ฅผ ์ ์ธํ์ง ์๊ณ ๋ฐ๋ก ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
package ch07.unit06;
public class Ex01_Math {
public static void main(String[] args) {
double a;
System.out.println(Math.PI);
a = Math.sin(30 * Math.PI / 180); // PI์์ฃผ์จ
System.out.println(a);
System.out.println(Math.sqrt(2)); // ์ ๊ณฑ๊ทผ
System.out.println(Math.floor(12.77)); // ์ ์ญ 12.0
System.out.println(Math.floor(-12.77)); // ์ ์ญ -13.0
a = Math.pow(2, 10); // 2์ 10์น
System.out.println(a);
a = Math.random(); // 0~ <1 ๋์
System.out.println(a);
}
}
Random
์ฌ๋ฌ ํํ์ ๋์๋ฅผ ์ ๊ณตํ๋ ํด๋์ค. Math ํด๋์ค์ ์ ์ ๋ฉ์๋์ธ random()๋ ๋์๋ฅผ ์ ๊ณตํ๋ ๋ฉ์๋ ์ด์ง๋ง 0..~1.0์ฌ์ด์ ์ค์๋ง ๋ฐ์ํ๋ฏ๋ก ํ์ํ ์ฌ๋ฌ ํํ์ ๋์๋ฅผ ๋ง๋ค๋ ค๋ฉด ์ฐ์ฐ์ ํ์๋ก ํ๋ค. ๊ทธ๋์ ์๋ฐ๋ ์ฌ๋ฌ ํํ์ ๋์๋ฅผ ๋ฐ์์์ผ์ฃผ๋ Random ํด๋์ค๋ฅผ ์ ๊ณตํ๋ค.
package ch07.unit06;
import java.util.Random;
public class Ex02 {
public static void main(String[] args) {
// Random : ์ฌ๋ฌ ํํ์ ๋์ ๋ฐ์
Random rnd = new Random ();
int n;
for(int i =1;i<=100;i++) {
n=rnd.nextInt(100); // 0~99 ์ฌ์ด ๋์
System.out.printf("%5d",n);
if(i%10==0) {
System.out.println();
}
}
}
}
package ch07.unit06;
import java.util.Random;
public class EX03 { // 5์๋ฆฌ ๋์ ๋ฐ์ํ๊ธฐ
public static void main(String[] args) {
System.out.println(toNumber5());
System.out.println(toNumber3());
}
public static String toNumber5() {
String s = null;
Random rnd = new Random();
s = String.format("%05d",rnd.nextInt(100000));
return s;
}
// 1~9 ์๋ฅผ ์ด์ฉํ์ฌ ์๋ก ๋ค๋ฅธ 3์๋ฆฌ ๋์ (123~987)
public static String toNumber3(){
StringBuilder sb = new StringBuilder( );
Random rnd = new Random();
String n;
for(int i=0; i<3; i++) {
n= Integer.toString(rnd.nextInt(9)+1);
if(sb.indexOf(n)>=0) {
i--;
continue;
}
sb.append(n);
}
return sb.toString();
}
}
โถ ์ค๋ฌด๊ณ ๊ฐ (๋์ ๋ง์ถ๊ธฐ)
package ch07.unit06;
import java.util.Random;
import java.util.Scanner;
public class Ex04_01 {
public static void main(String[] args) {
// ์ค๋ฌด๊ณ ๊ฐ : 1~100 ์ฌ์ด์ ๋์๋ฅผ ๋ฐ์ ๋ง์ถ๊ธฐ
Scanner sc = new Scanner(System.in);
Random rnd = new Random();
int input;
int count = 0;
int com = rnd.nextInt(100) + 1;
System.out.println("์ค๋ฌด๊ณ ๊ฐ ํ๋ก๊ทธ๋๋ฐ...");
do {
System.out.println("input ?");
input = sc.nextInt();
if (com > input) {
System.out.printf("%d ๋ณด๋ค๋ ํฐ์์
๋๋ค.\n", input);
} else if (com < input) {
System.out.printf("%d ๋ณด๋ค ์์์์
๋๋ค.\n", input);
}
count++;
} while (input != com && count < 20);
System.out.println("๋์:" + com);
{
}
if (input == com) {
System.out.println("count : " + count);
} else {
System.out.println("์คํจํ์ต๋๋ค.");
}
sc.close();
}
}
Formatter
ํ์ ๋ฌธ์์ด์ ์ ๊ณตํ๊ณ ์ด ๋ฌธ์์ด์์ ์ง์ ํ ๋๋ก ๋ฐ์ดํฐ๊ฐ ํ์ํ ๋๋ค. format() ๋ฉ์๋์ ํ์ ๋ฌธ์์ด์ธ format ๋ฌธ์์ด์ ์ถ๋ ฅํ๊ณ ์ถ์ ๋ฌธ์์ด ํํ๋ก ์ง์ ํ๊ณ ๊ทธ ์ค๊ฐ์ %๋ก ์์ํ๋ ํ์ํ ์ธ์๋ฅผ ์ง์ ํ๋ค. ์ด ํ์ํ ์ธ์๋ ๋ช ๊ฐ๋ฅผ ๋ฃ์ด๋ ์๊ด์์ง๋ง ๋ ๋ฒ์งธ ์ธ์์ธ ๋น์ ํ ์ธ์์์ ํ์ํ ์ธ์์ ์๋งํผ์ ๋ฐ์ดํฐ๊ฐ ํ์ํ๋ค.
NumberFormat
์ถ์ ํด๋์ค๋ก ์ซ์์ ํฌ๋งท์ ์ง์ ํ ๋ ์ฌ์ฉํ๋ ํด๋์ค, ํํํ ์ ์๋ ํฌ๋งท์ ์ข ๋ฅ์๋ ์ ์ํ, ํตํ ์์น, ํผ์ผํธ๊ฐ ์๋ค. ๋ฌธ์์ด์ ํ๋ผ๋ฏธํฐ๋ก ์ฌ์ฉํ๋ parse() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ํฌ๋งท์ด ์ ์ฉ๋์ด ์๋ ๋ฌธ์์ด์ Number ๊ฐ์ฒด๋ก ๋ฐํํ ์ ์๋ค.
package ch07.unit07;
import java.text.NumberFormat;
import java.util.Locale;
public class Ex01_NumberFormat {
public static void main(String[] args) {
String s;
// ์๋ ์ถ๋ ฅ์ ๋ชจ๋ ๋ฌธ์์ด๋ก! ์ซ์ ์๋!
// ์ซ์ -> ๋ฌธ์์ด
int a = 12345;
s = Integer.toString(a);
System.out.println(s); // 12345
s = String.format("%,d", a);
System.out.println(s); // 12,345
s = String.format("%.2f", 125.7788);
System.out.println(s); // 125.78
s = String.format(Locale.ITALY, "%.2f", 125.7788); // ์ดํ๋ฆฌ๋ ,๊ฐ ์์์
System.out.printf(s); // 125,78
System.out.println();
// ์ซ์์ ๋ํ ํฌ๋ฉง
NumberFormat nf = NumberFormat.getInstance(); // ๋ฃ๋ค๊ฐ ๋์ค๋ฉด ์๋ , ๋ถ์
s = nf.format(a);
System.out.println(s);
s = nf.format(12345.2569);
System.out.println(s); // ์์์ ๊ธฐ๋ณธ 3์๋ฆฌ.
NumberFormat nf2 = NumberFormat.getInstance(); // ๋ฃ๋ค๊ฐ ๋์ค๋ฉด ์๋ , ๋ถ์
nf2.setMaximumFractionDigits(4); // ์์์ ์ต๋ 4์๋ฆฌ
nf2.setMinimumFractionDigits(2); // ์์์ ์ต์ 2์๋ฆฌ
s = nf2.format(12345.25687);
System.out.println(s); // 12,345.2569
s = nf2.format(12345);
System.out.println(s); // 12,345.00
// ํตํ
NumberFormat nf3 = NumberFormat.getCurrencyInstance();
s = nf3.format(12345);
System.out.println(s); // โฉ12,345
// ๋ฏธ๊ตญ ํตํ. ๋ฏธ๊ตญ์ ์์์ ์ฌ์ฉ.
NumberFormat nf4 = NumberFormat.getCurrencyInstance(Locale.US);
s = nf4.format(12345);
System.out.println(s); // $12,345.00
// ํผ์ผํธ
NumberFormat nf5 = NumberFormat.getPercentInstance();
s = nf5.format(0.23);
System.out.println(s); // 23%
}
}
package ch07.unit07;
import java.text.NumberFormat;
public class Ex02 {
public static void main(String[] args) {
String s1 = "1234"; // ์ ์
String s2 = "1,234"; // ์ ์
String s3 = "4567.12"; // ์ค์
// int n = Integer.parseInt(s2);
// System.out.println(n); // ,๊ฐ ์์ด์ ์๋จ.
int n = Integer.parseInt(s2.replaceAll(",", "")); // ,์ ๊ฑฐ ํ
System.out.println(n); // 1234
System.out.println();
// ๋ฌธ์์ด -> ์ซ์
NumberFormat nf = NumberFormat.getInstance();
try {
// Number : integer, Long, Double ๋ฑ์ ์์ ํด๋์ค
// integer, Long, Double ํด๋์ค๋ ์๋ก ์์ ๊ด๊ณ๊ฐ ์๋ ํ์ ๊ด๊ณ์ด๋ฏ๋ก ํ๋ณํ ๋ถ๊ฐ.
// Upcasting : Long ์ Number๋ก. ์ธ์ ๋ ๊ฐ๋ฅ
// Downcasting : Number -> Long.
// parse()๋ก ํ๋ณํ ๋ ๊ฒฐ๊ณผ๋ ์ ์๋ Long, ์ค์๋ Double
Number n1 = nf.parse(s1); // Long //
Number n2 = nf.parse(s2); // Lone
Number n3 = nf.parse(s3); // Double
System.out.println(n1); // 1234
System.out.println(n2); // 1234
System.out.println(n3); // 4567.12
System.out.println();
// System.out.println((n1 + 1000)); // ์ปด์ค๋ฅ. n1์ ๋ฌธ์์ด๋ ์๋๊ณ ์ซ์๋ ์๋.
// Integer ii = (Integer)n1; // ๋ฐ์ค๋ฅ. longํ์ integer๋ก ์บ์คํ
ํ ์ ์๋ค. // ClassCastException
Long obj = (Long)n1;
System.out.println(obj + 1000); // 2234
// ์ค์๋ฅผ ์ ์๋ง ์ฒ๋ฆฌ ํ๋๋ก. ์์์ ๋ฒ๋ฆฌ๊ธฐ
nf.setParseIntegerOnly(true);
Number n4 = nf.parse(s3);
System.out.println(n4); // 4567
} catch (Exception e) {
e.printStackTrace();
}
}
}
DateFormat
๊ตญ๊ฐ๋ณ ์ค์ ๊ณผ ์๊ด์์ด ๋ ์ง ๋๋ ์๊ฐ์ ํ์ํ(๋ ์ง →ํ ์คํธ)ํ๊ฑฐ๋ ๊ตฌ๋ฌธ ๋ถ์(ํ ์คํธ→๋ ์ง)์ ์ํ abstract ํด๋์ค์ด๋ค. ๋ ์ง๋ Date ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๊ฑฐ๋ 1970๋ 1์ 1์ผ 00:00:00 GMT ์ดํ ๋ฐ๋ฆฌ ์ด๋ก ํ์ํ๋ค. ์ถ์ ํด๋์ค์ด๋ฏ๋ก ํ์ฌ ๋ก์ผ์ผ์ ๋ ์ง๋ฅผ ํ์ํํ๋ ค๋ฉด, ์ ์ (static) ํฉํ ๋ฆฌ ๋ฉ์๋ ์ค ํ๋๋ฅผ ์ฌ์ฉํ๋ค.
package ch07.unit07;
import java.text.DecimalFormat;
public class Ex03_DecimalFormat {
public static void main(String[] args) {
// DecimalFormat : ํจํด ํ์์ผ๋ก ์ซ์๋ฅผ ๋ฌธ์์ด๋ก ๋ณํ
String s;
DecimalFormat df1 = new DecimalFormat("#,##0"); // #:์ซ์๊ฐ ์์ผ๋ฉด ๋์ค๊ณ ์์ผ๋ฉด ๋์ค์ง ๋ง๋ผ
s = df1.format(2345.566987);
System.out.println(s); // 2,346 // ๋ฐ์ฌ๋ฆผ
System.out.println();
DecimalFormat df2 = new DecimalFormat("#,##0.##");
s = df2.format(2345.566987);
System.out.println(s); // 2,345.57 // ๋ฐ์ฌ๋ฆผ
s = df2.format(2345);
System.out.println(s); // 2,345 // ๋ฐ์ฌ๋ฆผ
System.out.println();
DecimalFormat df3 = new DecimalFormat("#,##0.0#");
s = df3.format(2345.566987);
System.out.println(s); // 2,345.57 // ๋ฐ์ฌ๋ฆผ
s = df3.format(2345);
System.out.println(s); // 2,345.57 // ๋ฐ์ฌ๋ฆผ
System.out.println(); // 2,345.0
DecimalFormat df4 = new DecimalFormat("#,###");
s = df4.format(2345.566987);
System.out.println(s); // 2,346
s = df4.format(0);
System.out.println(s); // 0
}
}
ChoiceFormat
๋ฐฐ์ด๋์ ํจํดํํ๋ก '#' ๋๋ '<'์ ๋ ๊ฐ์ง ๊ตฌ๋ถ์๋ฅผ ์ด์ฉํ์ฌ 'imit#value'์ ํ์์ผ๋ก ์ฌ์ฉํ๋ฉฐ '#'์ ๊ฒฝ๊ณ ๊ฐ์ ๋ฒ์์ ํฌํจ ํ์ง๋ง '<'๋ ํฌํจ ํ์ง ์๋๋ค.
package ch07.unit07;
import java.text.ChoiceFormat;
public class Ex04_ChoiceFormat {
public static void main(String[] args) {
// ChoiceFormat : ํน์ ๋ฒ์์ ๋ฌธ์์ด ๋ฐํ
double[] limits = {0,60,70,80,90}; // ์ ์์์์ ํฐ์์์ผ๋ก.
String [] grads = {"F","D","C","B","A"};
ChoiceFormat cf = new ChoiceFormat(limits, grads);
System.out.println("95 : " + cf.format(95));
System.out.println("80 : " + cf.format(80));
System.out.println("0 : " + cf.format(0));
System.out.println("-30 : " + cf.format(-30));
limits = new double [] {0,60,65,70, 75, 80, 85, 90, 95}; // ์ ์๋ ๊ฐ์ ๋ง์์ผ ํจ.
grads = new String[] {"0.0","1.0","1.5","2.0","2.5", "3.0", "3.5", "4.0", "4.5"};
ChoiceFormat cf2 = new ChoiceFormat(limits, grads);
System.out.println("95 : " + cf2.format(95));
System.out.println("80 : " + cf2.format(80));
System.out.println("0 : " + cf2.format(0));
System.out.println("-30 : " + cf2.format(-30));
//์ฃผ์ ๋์ค๋ ๊ฒฐ๊ณผ๋ ๋ฌธ์์ด์ด๋ฏ๋ก double๋ก ๋ณํ
double a = Double.parseDouble(cf2.format(95));
System.out.println(a);
String p = "60#D|70#C|80<B|90#A"; // # < ๋ง ๊ฐ๋ฅ //
ChoiceFormat cf3 = new ChoiceFormat(p);
System.out.println("95 : " + cf3.format(95)); // A
System.out.println("90 : " + cf3.format(90)); // A
System.out.println("85 : " + cf3.format(85)); // B // ๊ฐ์ฅ ๊ฐ๊น์ด ์
System.out.println("80 : " + cf3.format(80)); // C // 80#Bํ๋ฉด B๋ก ๋์ด.
System.out.println("65 : " + cf3.format(65)); // D
System.out.println("0 : " + cf3.format(0)); // D
//์ฃผ์ ๋์ค๋ ๊ฒฐ๊ณผ๋ ๋ฌธ์์ด์ด๋ฏ๋ก double๋ก ๋ณํ
}
}
'๐ป > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] 13์ผ์ฐจ : ์์ (0) | 2024.03.04 |
---|---|
[Java] 12์ผ์ฐจ : ์๋ฐ์ ๊ธฐ๋ณธ API ํด๋์ค 6 (0) | 2024.03.02 |
[Java] 11์ผ์ฐจ : ์๋ฐ์ ๊ธฐ๋ณธ API ํด๋์ค 4 - (๋ ์ง) (0) | 2024.02.29 |
[Java] 11์ผ์ฐจ : ์๋ฐ์ ๊ธฐ๋ณธ API ํด๋์ค - String 2 (0) | 2024.02.29 |
[Java] 10์ผ์ฐจ : ์๋ฐ์ ๊ธฐ๋ณธ API ํด๋์ค - String (1) | 2024.02.27 |