학원/JAVA

9.30 13-1 MathClass

도원결의 2022. 9. 30. 18:19

MathClass

MathClass 특징:

요놈들은 다 static임!! 저번에 계산기 만들 때 생각하면 쉬움 인자들은 인스턴스변수로 받고 산술식은 static으로 만들면 코드양을 줄일 수 있다. 따라서 Math클래스 메소드 이용 시 인스턴스화 할 필요가 없고 클래스명.으로 호출하면 됨Math math = new Math();  인스턴스화 안됨Math.PI ;  요렇게 직접 불러와야함

 

[MathClass들의 주요메소드]

1.abs()  : 절대값 구하기

            float f = -3.14F;
            double d = -100.9;
             int num=10;
     Math.abs(f);     =>3.14     Math.abs(d);   =>100.9     Math.abs(num);   =>10내가만든 abs메소드(아니 그냥 메소드 쓰면되지 뭘 또 만들어..... 만들라니 만들어봅니다)private static int abs(int value){       return value >=0 ? value : -value ;      }

 

2.double ceil : 올림 메소드 Math.ceil(3.4);  Math.ceil(3.9);Math.ceil(-3.4);    =>-3

 

3.

double

floor : 내림메소드    음수일때 조심하세용

Math.floor(3.4);Math.floor(3.9);Math.floor(-3.4); ==>-4

 

4.

long/int

.round(long/int형 매개변수) : 반올림 메소드

Math.round(3.49);   =>3
Math.round(3.51);    =>4
Math.round(-1.4);    =>-1
Math.round(-1.8);    =>-2
Math.round(-0.4);    =>0
Math.round(-0.8);    =>-1
Math.round(-0.5);    =>0      =>이거 조심해욤
내가만든 .round 메소드private static long round(double value){    return value > 0 ? (long)(value+0.5) : (long)(value-0.4) ;}

 

5.double pow(a,b)  : a의b승

Math.pow(2, 3)

내가만든 pow 메소드

private static int pow (int num , int loop){

  int sum =1;

f

 

}