2012년 12월 29일 토요일

[Ch08Ex05] Math Class & Static field

Result :


-3.4의 절대값은 3.4
원주율(Math.PI) :  3.141592653589793
반지름이 5인 원의 면적 78.53981633974483

Sample Code :


public class Ch08Ex05 {

public static void main(String[] args) {
double d = -3.4;
System.out.println( d + "의 절대값은 " + Math.abs(d));

System.out.println("원주율(Math.PI) :  " + Math.PI);

int r = 5;
double area;
area=r*r*Math.PI;
System.out.println("반지름이 " + r + "인 원의 면적 " + area);


}

}




Applied Code :
the same result without Static method "Math"



public class AppliedCh08Ex05 {

static double abs(double x){
double y;

if(x < 0)
y = -x;
else
y = x;
return y;
}

static double pi(double r){

double z;
z = r * r * 3.14;
  return z;
}

public static void main(String[] args) {
double d = -3.4;
double ans = abs(d);
System.out.println( d + "의 절대값은 " + ans);

int r = 5;
double area=pi(r);

System.out.println("반지름이 " + r + "인 원의 면적 " + area);
}

}

Reference book : 이클립스로 배우는 Java 6 Programming/ 임종권, 전병찬/ 가메출판사

댓글 없음:

댓글 쓰기