본문 바로가기
Language/Java 예제

[Java예제] 도형면적 구하는 예제

by 나비와꽃기린 2015. 1. 30.
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

abstract class Shape{
 
 protected double area;
 private String name;
 
 Shape(){}
 
 Shape(String name){
  this.name=name;
 }
 
 public double getArea(){
  return area;
 }
 
 public void setArea(double area){
  this.area=area;
 }
 
 public abstract void calculationArea();
 
 public void print(){
  System.out.println(name+"의 면적은 "+area);
 }
 
}
 
class Circle extends Shape{
 
 double radius;
 
 Circle(){}
 
 Circle(String name,double radius){
  super(name);
  this.radius=radius;
 }
 
 public void setRadius(double radius){
  this.radius=radius;
 }
 
 public void calculationArea(){
  area=Math.PI*radius*radius;
 }
 
}
 
class Rectangular extends Shape{
 
 double width;
 double height;
 
 Rectangular(){}
 
 Rectangular(String name,double width,double height){
  super(name);
  this.width=width;
  this.height=height;
 }
 
 public void calculationArea(){
  area=height*width;
 }
 
}
 
 
 
public class TestShape{
 public static void main(String[] args) 
 {
  Shape[] shape=new Shape[2];
 
  shape[0]=new Circle("원",10);
  shape[1]=new Rectangular("직사각형",10,20);
 
  for(int i=0;i