본문 바로가기

JAVA/복습

Math클래스

Math 클래스 - 수학계산을 위한 클래스

코싸인, 싸인, 탄젠트, 절대값, 랜덤값을 구할 수 있는 클래스

  • Math클래스는 생성자가 private으로 되어 있기 때문에 new 연산자를 이용하여 객체를 생성할 수 없다.
  • 객체를 생성할 수는 없지만 모든 메소드와 속성이 static으로 정의되어 있기 때문에 객체를 생성하지 않고도 사용할 수 있다.

최대값 구하기
- Static한 메소드 선언 - 클래스명.메소드명
- max() : 두 값중에 더 큰 값을 구할 수 있는 메소드

 

package java03.day03;

public class MathExam {

    public static void main(String[] args) {

        int value1 = Math.max(5, 30);
        System.out.println(value1);

    }
}


- 최소값 구하기

int value = Math.min(5,30);
System.out.println(value);



- 절대값 구하기 - abs()

System.out.println(Math.abs(-10));



- 랜덤한 값 구하기 - random()는 double타입이다. 0이상 1.0 미만의 실수 값을 구한다.

System.out.println(Math.random());



- 제곱근을 구하기 - sqrt()

System.out.println(Math.sqrt(25));

   

System.out.println("2의 10승 = " + Math.pow(2, 10));
System.out.println("16의 1/2승 = : " + Math.pow(16, 0.5));
System.out.println("log200 = " + Math.log10(200));

'JAVA > 복습' 카테고리의 다른 글

Method  (0) 2021.10.21
java.util 패키지  (0) 2021.10.21
StringBuffer  (0) 2021.10.21
Generic  (0) 2021.10.20
java.lang 패키지/ 오토박싱  (0) 2021.10.20