2012년 12월 30일 일요일

[ Ch11Ex03] 도형 면적 구하는 메소드

Result :


This is 3.141592653589793
도형의 면적은 78.53981633974483
도형의 면적은 100.0
도형의 면적은 50.0



Sample code

File : Ch11Ex03


import kame.ch11.ex02.Circle;
import kame.ch11.ex02.Rectangle;
import kame.ch11.ex02.Shape;
import kame.ch11.ex02.Triangle;

public class Ch11Ex03 {
  public static void main(String[] args) {


 Shape []s_arr=new Shape[3];
 s_arr[0] = new Circle();
 s_arr[1] = new Rectangle();
 s_arr[2] = new Triangle();

 for ( int i = 0; i < s_arr.length; i++)
 System.out.println("도형의 면적은 " + s_arr[i].area());
  }
}


File : Shape


package kame.ch11.ex02;

public class Shape {

  public double res=0;

  public double area( ){
    return res;
  }
}


File :  Rectangle


package kame.ch11.ex02;

public class Rectangle extends Shape {
public int w=10;
public int h=10;
public double area( ){
 res=w*h;
 return res;
 }
}



File : Triangle


package kame.ch11.ex02;

public class Triangle extends Shape {
public int w=10;
public int h=10;
 public double area( ){
 res=w*h*0.5;
 return res;
 }
}





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

댓글 없음:

댓글 쓰기