2차원 도형 = 타원, 사각형, 삼각형
3차원 도형 = 구, 정육면체, 원기둥
최상위 부모 Shape를 상속받아 각각 2차원, 3차원 도형을 뜻하는
TwoDimShape, ThreeDimShape 클래스를 생성
TwoDimShape를 상속 받는 '타원, 사각형, 삼각형' Class
ThreeDimShape를 상속 받는 '구, 정육면체, 원기둥' Class
intanceof 함수로 어떤 객체를 상속 받는지 확인 후
2차원 배열일 경우 넓이를 구하고 (getArea() 함수)
3차원 배열일 경우 부피를 구함 (getVolume() 함수)
import java.util.ArrayList;
import java.util.Scanner;
class Shape{
protected int x;
protected int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Shape(int x, int y) {
super();
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "Shape [x=" + x + ", y=" + y + "]";
}
}
abstract class TwoDimShape extends Shape{
protected double area; //면적
public abstract double getArea(); //2차원 도형 면적 계산
public TwoDimShape(int x, int y) {
super(x, y);
}
}
abstract class ThreeDimShape extends Shape{
protected double volume; //부피
public abstract double getVolume(); // 3차원 도형 부피 계산
public ThreeDimShape(int x, int y) {
super(x, y);
// TODO Auto-generated constructor stub
}
}
class Ellipse extends TwoDimShape{
private double l_radius;
private double s_radius;
@Override
public double getArea() { // 2차원 타원의 면적 구하기
// TODO Auto-generated method stub
area = Math.PI * l_radius * s_radius;
return area;
}
public Ellipse(int x, int y, double l_radius, double s_radius) {
super(x, y);
this.l_radius = l_radius;
this.s_radius = s_radius;
}
@Override
public String toString() {
return "Ellipse [l_radius=" + l_radius + ", s_radius=" + s_radius + ", area=" + area + ", x=" + x + ", y=" + y
+ "]";
}
}
class Rectangle extends TwoDimShape{
private double width;
private double height;
public Rectangle(int x, int y, double width, double height) {
super(x, y);
this.width = width;
this.height = height;
}
@Override
public double getArea() { //2차원 사각형 면적 구하기
area = width * height;
return area;
}
@Override
public String toString() {
return "Rectangle [width=" + width + ", height=" + height + ", area=" + area + ", x=" + x + ", y=" + y + "]";
}
}
class Triangle extends TwoDimShape{
private int width;
private int height;
public Triangle(int x, int y, int width, int height) {
super(x, y);
this.width = width;
this.height = height;
}
@Override
public double getArea() {
area = (width * height)/2;
// TODO Auto-generated method stub
return area;
}
@Override
public String toString() {
return "Triangle [width=" + width + ", height=" + height + ", area=" + area + ", x=" + x + ", y=" + y + "]";
}
}
class Shpere extends ThreeDimShape{// 구
private double radius;
public Shpere(int x, int y, double radius) {
super(x, y);
this.radius = radius;
}
@Override
public double getVolume() {
volume = 4.0/3.0 * Math.PI * Math.pow(radius, 3);
return volume;
}
@Override
public String toString() {
return "Shpere [radius=" + radius + ", volume=" + volume + ", x=" + x + ", y=" + y + "]";
}
}
class Cube extends ThreeDimShape{ //직육면체
private double width;
private double length;
private double height;
public Cube(int x, int y, double width, double length, double height) {
super(x, y);
this.width = width;
this.length = length;
this.height = height;
}
@Override
public double getVolume() {
volume= width*length*height;
return volume;
}
@Override
public String toString() {
return "Cube [width=" + width + ", length=" + length + ", height=" + height + ", volume=" + volume + ", x=" + x
+ ", y=" + y + "]";
}
}
class Cylinder extends ThreeDimShape{
private double radius;
private double height;
public Cylinder(int x, int y, double radius, double height) {
super(x, y);
this.radius = radius;
this.height = height;
}
@Override
public double getVolume() {
volume = Math.PI * Math.pow(radius, 2) * height;
// TODO Auto-generated method stub
return volume;
}
}
public class ShapeTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 모든 종류의 도형을 담을 수 있는 배열을 만듭니다.
// 배열의 크기는 3으로 합니다.
// 3가지의 도형을 배열에 생성하고 담고
// 반복문을 이용하여 배열의 요소를 하나씩 꺼내오고
// 그 요소가 2차원 도형이면 면적을 계산하고
// 3차원 배열이면 부피를 계산
Shape arr[] = new Shape[3];
arr[0] = new Ellipse(10,10,50,20); //2차원 타원
arr[1] = new Shpere(10,100,50); //3차원 구
arr[2] = new Rectangle(10,200,20,40); //2차원 사격형
for (int i=0; i<arr.length; i++) {
if (arr[i] instanceof TwoDimShape) {
((TwoDimShape)arr[i]).getArea();
}else if(arr[i] instanceof ThreeDimShape) {
((ThreeDimShape)arr[i]).getVolume();
}
System.out.println(arr[i]);
}
// for (int i=0; i<arr.length; i++) {
// if(arr[i] instanceof TwoDimShape) {
// ((TwoDimShape)arr[i]).getArea();
// //System.out.println((TwoDimShape)arr[1]);
// }
// }
}
}
반응형
'기타' 카테고리의 다른 글
문제풀이 ) 건강검진 대상자 구하기 (0) | 2022.04.07 |
---|---|
부트 캠프, 국비 학원 (0) | 2022.02.10 |
간단한 웹서버 만들기(Web Server For Chrome) (0) | 2022.01.16 |
깃허브 호스팅 방법 (0) | 2022.01.16 |
깃허브 저장소(repository) 만들기 (0) | 2022.01.16 |
댓글