{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# HW4 2D 오브젝트 그룹 (15점)\n", "\n", "?분반 HW4-1??그룹:\n", "\n", "* 이름: ???\n", "* 이름: ???\n", "* 이름: ???" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "이 과제는 3 단계로 이루어진다.\n", "각 단계를 완수할 때마다 부분점수가 주어지지만, 주의할 점은 실행가능한 코드가 아니면 0점이라는 것이다.\n", "\n", "예를 들어 1단계까지만 했다면 1단계가 완성되어 실행가능한 형태의 코드로 제출해야 한다.\n", "\n", "1단계를 수행한 후 2단계를 하려다 만 코드라면 점수를 얻지 못하고 0점이다.\n", "\n", "1단와와 2단계를 완료한 직후에 파일을 따로 저장해 놓고 그 다음 단계 작업을 진행하는 것을 추천한다.\n", "\n", "수정해도 된다고 밝힌 클래스의 정의만 수정해야 한다.\n", "\n", "## Level 1. 일차원적 2D 오브젝트 그룹 클래스 정의 (4점)\n", "여러개의 ShapeObj2D로 구성된 GroupObj2D의 정의를 완성하라.\n", "즉, toString 및 Displayable과 ToSVG에서 오버라이드하라고 요구하는 메소드를 구현하라는 뜻이다.\n", "\n", "테스트로 3개 이상의 ShapeObj2D를 함께 묶어놓은 GroupObj2D 오브젝트를 정의하여 display 해보라.\n", "\n", "## Level 2. HW3의 변환 기능 추가 (5점)\n", "HW3에서 작성한 3가지 변환함수 메소드를 Trans2D 인터페이스로 추상화하라.\n", "\n", "그리고 ShapeObj2D와 GroupObj2D 클래스가 공통적인 인터페이스 Trans2D를 구현하도록 두 클래스의 정의를 수정하라.\n", "\n", "이차원 변환에 테스트는 Level 1에서 정의했던 GroupObj2D 오브젝트에 대해 수행하면 된다.\n", "\n", "## Level 2. 중첩 가능한 2D 오브젝트 그룹 클래스로 개선 (6점)\n", "\n", "GroupObj2D 클래스의 멤버 변수 배열인 objects에 ShapeObj2D 인스턴스 뿐만 아니라 GroupObj2D 인스턴스도 포함될 수 있도록\n", "GroupObj2D 클래스의 정의를 개선하라. 필요하다면 ShapeObj2D 클래스의 (심하게 뜯어고치는 건 안되고) 정의를 약간은 수정해도 된다.\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "import org.apache.commons.lang3.tuple.*;\n", "\n", "interface Displayable {\n", " public void display();\n", "}\n", "\n", "interface ToSVG {\n", " public String toSVG();\n", " \n", " // points of the bounding rectangle\n", " public Integer minX();\n", " public Integer minY();\n", " public Integer maxX();\n", " public Integer maxY();\n", " // default methods using above four abstract methods\n", " default public Pair minPoint() {\n", " return Pair.of( this.minX(), this.minY() );\n", " }\n", " default public Pair maxPoint() {\n", " return Pair.of( this.maxX(), this.maxY() );\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "import io.github.spencerpark.ijava.runtime.*;\n", "import org.apache.commons.lang3.tuple.*;\n", "\n", "abstract class Shape {\n", " // 인스턴스 변수\n", " int width; // 양과 음의 정수값 모두 가능\n", " int height; // 양과 음의 정수값 모두 가능\n", " String fill; // 도형의 안쪽을 채우는 색깔\n", " double opacity; // 도형을 그렸을 때 투명도\n", " \n", " Shape(int width, int height, String fill, double opacity) {\n", " this.width = width;\n", " this.height = height;\n", " this.fill = fill;\n", " this.opacity = opacity;\n", " }\n", "\n", " // 추상 메소드 \n", " abstract double area(); // 넓이 계산\n", " abstract String toSVGshape(Pair point); // SVG 기본 도형 태그 생성\n", " \n", " @Override\n", " public String toString() {\n", " return super.toString()\n", " + String.format(\"(width=%d, height=%d, fill=%s, opacity=%f)\",\n", " width, height, fill, opacity );\n", " }\n", "\n", " void display() { // 이미지 형태로 보여주기 위한 메소드\n", " Pair point = Pair.of( (width<0)? Math.abs(width) :0,\n", " (height<0)? Math.abs(height):0 );\n", " String svgStr = String.format(\n", " \"%s\",\n", " Math.abs(width), Math.abs(height), this.toSVGshape(point) );\n", " Display.display(svgStr,\"text/html\");\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "class RightTri extends Shape {\n", " RightTri(int width, int height, String fill, double opacity) {\n", " super(width, height, fill, opacity);\n", " }\n", " \n", " @Override\n", " double area() { return Math.abs(width * height) / 2; } // 삼각형 넓이공식에 맞게\n", " \n", " @Override\n", " String toSVGshape(Pair point) {\n", " int x0 = point.getLeft();\n", " int y0 = point.getRight();\n", " return\n", " String.format(\"\",\n", " x0,y0, fill, opacity)\n", " +\n", " String.format(\"\",\n", " x0,y0, x0+width,y0, x0,y0+height, fill, opacity );\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "class Rectangle extends Shape {\n", " Rectangle(int width, int height, String fill, double opacity) {\n", " super(width, height, fill, opacity);\n", " }\n", " \n", " @Override\n", " double area() { return Math.abs(width * height); } // 직사각형 넓이공식에 맞게\n", " \n", " @Override\n", " String toSVGshape(Pair point) {\n", " int x0 = point.getLeft();\n", " int y0 = point.getRight();\n", " return\n", " String.format(\"\",\n", " x0,y0, fill, opacity)\n", " +\n", " String.format(\"\",\n", " (width>0)? x0: x0+width, (height>0)? y0: y0+height,\n", " Math.abs(width), Math.abs(height), fill, opacity );\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "class ShapeObj2D implements Displayable, ToSVG {\n", " Pair point;\n", " Shape shape;\n", " \n", " ShapeObj2D(Pair point, Shape shape) {\n", " this.point = point;\n", " this.shape = shape;\n", " }\n", " \n", " @Override\n", " public String toString() {\n", " return\n", " super.toString()\n", " +\n", " String.format(\"( point=%s, shape=%s )\",\n", " point.toString(), shape.toString() );\n", " }\n", " \n", " // Displayable에서 오버라이드하라고 요구하는 메소드를 구현\n", " @Override // 지금은 무조건 원점부터 1사분면만 그리고 있다고 생각하고 작성하고 있음\n", " public void display() { // 이미지 형태로 보여주기 위한 메소드\n", " String svgStr = String.format(\n", " \"%s\",\n", " maxX(), maxY(), this.toSVG() );\n", " Display.display(svgStr,\"text/html\");\n", " }\n", " \n", " // ToSVG에서 오버라이드하라고 요구하는 메소드들을 구현\n", " @Override\n", " public String toSVG() { return shape.toSVGshape(point); } \n", " // points of the bounding rectangle\n", " @Override\n", " public Integer minX() { return Math.min(point.getLeft(), point.getLeft()+shape.width); }\n", " @Override\n", " public Integer minY() { return Math.min(point.getRight(), point.getRight()+shape.height); }\n", " @Override\n", " public Integer maxX() { return Math.max(point.getLeft(), point.getLeft()+shape.width); }\n", " @Override\n", " public Integer maxY() { return Math.max(point.getRight(), point.getRight()+shape.height); }\n", "\n", " /*\n", " // Trans2D에서 오버라이드하라고 요구하는 메소드들을 구현\n", " */\n", "}" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "ename": "CompilationException", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m\u001b[0m\u001b[1m\u001b[30m\u001b[41mclass GroupObj2D implements Displayable, ToSVG {\u001b[0m", "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m\u001b[41m ShapeObj2D [] objects;\u001b[0m", "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m\u001b[41m \u001b[0m", "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m\u001b[41m GroupObj2D(ShapeObj2D[] objects) {\u001b[0m", "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m\u001b[41m this.objects = objects;\u001b[0m", "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m\u001b[41m }\u001b[0m", "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m\u001b[41m \u001b[0m", "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m\u001b[41m /*\u001b[0m", "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m\u001b[41m @Override\u001b[0m", "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m\u001b[41m public String toString() { ... }\u001b[0m", "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m\u001b[41m */\u001b[0m", "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m\u001b[41m \u001b[0m", "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m\u001b[41m /*\u001b[0m", "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m\u001b[41m // Displayable에서 오버라이드하라고 요구하는 메소드를 구현\u001b[0m", "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m\u001b[41m */\u001b[0m", "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m\u001b[41m\u001b[0m", "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m\u001b[41m /*\u001b[0m", "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m\u001b[41m // ToSVG에서 오버라이드하라고 요구하는 메소드들을 구현\u001b[0m", "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m\u001b[41m */\u001b[0m", "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m\u001b[41m \u001b[0m", "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m\u001b[41m /*\u001b[0m", "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m\u001b[41m // Trans2D에서 오버라이드하라고 요구하는 메소드들을 구현\u001b[0m", "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m\u001b[41m */\u001b[0m", "\u001b[1m\u001b[30m| \u001b[1m\u001b[30m\u001b[41m}\u001b[0m", "\u001b[1m\u001b[31mGroupObj2D is not abstract and does not override abstract method display() in Displayable\u001b[0m", "" ] } ], "source": [ "class GroupObj2D implements Displayable, ToSVG {\n", " ShapeObj2D [] objects;\n", " \n", " GroupObj2D(ShapeObj2D[] objects) {\n", " this.objects = objects;\n", " }\n", " \n", " /*\n", " @Override\n", " public String toString() { ... }\n", " */\n", " \n", " /*\n", " // Displayable에서 오버라이드하라고 요구하는 메소드를 구현\n", " */\n", "\n", " /*\n", " // ToSVG에서 오버라이드하라고 요구하는 메소드들을 구현\n", " */\n", " \n", " /*\n", " // Trans2D에서 오버라이드하라고 요구하는 메소드들을 구현\n", " */\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Java", "language": "java", "name": "java" }, "language_info": { "name": "java" } }, "nbformat": 4, "nbformat_minor": 4 }