๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป/JAVA

[Java] 12์ผ์ฐจ : ์ž๋ฐ”์˜ ๊ธฐ๋ณธ API ํด๋ž˜์Šค 5

by ๋”ฐ๊ถˆ 2024. 3. 2.

 

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๋กœ ๋ณ€ํ™˜
	}

}