지난 포스팅에서 상속에 대해 다뤄봤습니다.
새로운 내용을 공부하기 전, 복습 차원에서
상속의 예제를 풀어봅시다.
자바 상속 예제
난이도 ★★★☆
예제 1
Q : 다음 클래스 다이어그램과 구조, 요구사항을 보고 프로그램을 작성하여라
● 아래와 같은 클래스 다이어그램과 클래스 구조이 있다.
실행결과
풀이 :
model.vo 패키지 - Point 클래스
package com.kh.practice.point.model.vo;
public class Point {
private int x;
private int y;
public Point() {
}
public Point(int x, int y) {
this.x = x;
this.y = 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;
}
@Override
public String toString() {
return "Point [x=" + x + ", y=" + y + "]";
}
}
model.vo 패키지 - Circle 클래스
package com.kh.practice.point.model.vo;
public class Circle extends Point {
private int radius;
public Circle() {
}
public Circle(int x, int y, int radius) {
super(x,y);
this.radius = radius;
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
@Override
public String toString() {
return getX() + ", " + getY() + ", " + getRadius();
}
}
model.vo 패키지 - Rectangle 클래스
package com.kh.practice.point.model.vo;
public class Rectangle extends Point{
private int width;
private int height;
public Rectangle() {
}
public Rectangle(int x, int y, int width, int height) {
super(x, y);
this.width = width;
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
@Override
public String toString() {
return this.getX() + ", " + this.getY() + ", " + this.getHeight() + ", " + this.getWidth();
}
}
controller 패키지 - CircleController 클래스
package com.kh.practice.point.controller;
import com.kh.practice.point.model.vo.Circle;
public class CircleController {
private Circle c = new Circle();
public String calcArea(int x, int y, int radius) {
c.setX(x); c.setY(y); c.setRadius(radius);
return "면적 : " + c.toString() + " / " + (Math.PI * (double)c.getRadius() * (double)c.getRadius());
}
public String calcCircum(int x, int y, int radius) {
c.setX(x); c.setY(y); c.setRadius(radius);
return "둘레 : " + c.toString() + " / " + (Math.PI * 2 * (double)c.getRadius());
}
}
controller 패키지 - RectangleController 클래스
package com.kh.practice.point.controller;
import com.kh.practice.point.model.vo.Rectangle;
public class RectangleController {
private Rectangle r = new Rectangle();
public String calcArea(int x, int y, int height, int width) {
r.setX(x); r.setY(y); r.setHeight(height); r.setWidth(width);
return "면적 : " + r.toString() + " / " +
(r.getHeight() * r.getWidth());
}
public String calcPerimeter(int x, int y, int height, int width) {
r.setX(x); r.setY(y); r.setHeight(height); r.setWidth(width);
return "둘레 : " + r.toString() + " / " +
2 * (r.getHeight() + r.getWidth());
}
}
view 패키지 - PointMenu 클래스
package com.kh.practice.point.view;
import java.util.Scanner;
import com.kh.practice.point.controller.CircleController;
import com.kh.practice.point.controller.RectangleController;
import com.kh.practice.point.controller.TriangleController;
public class PointMenu {
private Scanner sc = new Scanner(System.in);
private CircleController cc = new CircleController();
private RectangleController rc = new RectangleController();
private TriangleController tc = new TriangleController();
public void mainMenu() {
System.out.println("===== 메뉴 =====");
System.out.println("1. 원");
System.out.println("2. 사각형");
System.out.println("3. 삼각형");
System.out.println("9. 끝내기");
System.out.print("메뉴 번호 : ");
int menu = sc.nextInt(); sc.nextLine();
if(menu == 1) {
this.circleMenu();
}else if(menu == 2) {
this.rectangleMenu();
}else if(menu == 3) {
this.triangleMenu();
}else {
System.out.println("종료합니다.");
System.exit(0);
}
}
public void circleMenu() {
System.out.println("===== 원 메뉴 =====");
System.out.println("1. 원 둘레");
System.out.println("2. 원 넓이");
System.out.println("9. 메인으로");
System.out.print("메뉴 번호 : ");
int menu = sc.nextInt(); sc.nextLine();
if(menu == 1) {
this.calcCircum();
}else if(menu == 2) {
this.calcCircleArea();
}else if(menu == 9){
this.mainMenu();
}else {
this.circleMenu();
}
}
public void rectangleMenu() {
System.out.println("===== 사각형 메뉴 =====");
System.out.println("1. 사각형 둘레");
System.out.println("2. 사각형 넓이");
System.out.println("9. 메인으로");
System.out.print("메뉴 번호 : ");
int menu = sc.nextInt(); sc.nextLine();
if(menu == 1) {
this.calcPerimeter();
}else if(menu == 2) {
this.calcRectArea();
}else if(menu == 9){
this.mainMenu();
}else {
this.rectangleMenu();
}
}
public void triangleMenu() {
System.out.println("===== 삼각형 메뉴 =====");
System.out.println("1. 삼각형 둘레");
System.out.println("2. 삼각형 넓이");
System.out.println("9. 메인으로");
System.out.print("메뉴 번호 : ");
int menu = sc.nextInt(); sc.nextLine();
if(menu == 1) {
this.calcTriperimeter();
}else if(menu == 2) {
this.calcTriArea();
}else if(menu == 9){
this.mainMenu();
}else {
this.triangleMenu();
}
}
public void calcCircum() {
System.out.print("x 좌표 : ");
int x = sc.nextInt(); sc.nextLine();
System.out.print("y 좌표 : ");
int y = sc.nextInt(); sc.nextLine();
System.out.print("반지름 : ");
int radius = sc.nextInt(); sc.nextLine();
System.out.println(cc.calcCircum(x, y, radius));
this.mainMenu();
}
public void calcCircleArea() {
System.out.print("x 좌표 : ");
int x = sc.nextInt(); sc.nextLine();
System.out.print("y 좌표 : ");
int y = sc.nextInt(); sc.nextLine();
System.out.print("반지름 : ");
int radius = sc.nextInt(); sc.nextLine();
System.out.println(cc.calcArea(x, y, radius));
this.mainMenu();
}
public void calcPerimeter() {
System.out.print("x 좌표 : ");
int x = sc.nextInt(); sc.nextLine();
System.out.print("y 좌표 : ");
int y = sc.nextInt(); sc.nextLine();
System.out.print("높이 : ");
int height = sc.nextInt(); sc.nextLine();
System.out.print("너비 : ");
int width = sc.nextInt(); sc.nextLine();
System.out.println(rc.calcPerimeter(x, y, height, width));
this.mainMenu();
}
public void calcRectArea() {
System.out.print("x 좌표 : ");
int x = sc.nextInt(); sc.nextLine();
System.out.print("y 좌표 : ");
int y = sc.nextInt(); sc.nextLine();
System.out.print("높이 : ");
int height = sc.nextInt(); sc.nextLine();
System.out.print("너비 : ");
int width = sc.nextInt(); sc.nextLine();
System.out.println(rc.calcArea(x, y, height, width));
this.mainMenu();
}
public void calcTriperimeter() {
System.out.print("x 좌표 : ");
int x = sc.nextInt(); sc.nextLine();
System.out.print("y 좌표 : ");
int y = sc.nextInt(); sc.nextLine();
System.out.print("변 1의 길이 : ");
int n1 = sc.nextInt(); sc.nextLine();
System.out.print("변 2의 길이 : ");
int n2 = sc.nextInt(); sc.nextLine();
System.out.print("변 3의 길이 : ");
int n3 = sc.nextInt(); sc.nextLine();
if(tc.calcPerimeter(x, y, n1, n2, n3) != null) {
System.out.println(tc.calcPerimeter(x, y, n1, n2, n3));
}else {
System.out.println("해당 3개의 변으로는 삼각형을 만들 수 없습니다.");
this.calcTriperimeter();
}
this.mainMenu();
}
public void calcTriArea() {
System.out.print("x 좌표 : ");
int x = sc.nextInt(); sc.nextLine();
System.out.print("y 좌표 : ");
int y = sc.nextInt(); sc.nextLine();
System.out.print("변 1의 길이 : ");
int n1 = sc.nextInt(); sc.nextLine();
System.out.print("변 2의 길이 : ");
int n2 = sc.nextInt(); sc.nextLine();
System.out.print("변 3의 길이 : ");
int n3 = sc.nextInt(); sc.nextLine();
if(tc.calcArea(x, y, n1, n2, n3) !=null) {
System.out.println(tc.calcArea(x, y, n1, n2, n3));
}else {
System.out.println("해당 3개의 변으로는 삼각형을 만들 수 없습니다.");
this.calcTriArea();
}
this.mainMenu();
}
}
run 패키지 - Run 클래스
package com.kh.practice.point.run;
import com.kh.practice.point.view.PointMenu;
public class Run {
public static void main(String[] args) {
PointMenu pm = new PointMenu();
pm.mainMenu();
}
}
난이도 ★★★★☆
예제 2
Q : 위 프로그램을 활용하여 다음 요구사항을 만족하는 프로그램을 작성하라.
● 위 프로그램과 동일한 구조를 가지지만,
원, 사각형뿐만 아니라 삼각형 클래스도 생성하여라.
※ 세 변의 길이를 알 때, 삼각형의 넓이를 구하는 공식
풀이 :
model.vo 패키지 - Triangle 클래스
package com.kh.practice.point.model.vo;
public class Triangle extends Point{
private int n1, n2, n3;
public Triangle() {
}
public Triangle(int x, int y, int n1, int n2, int n3) {
super(x, y);
this.n1 = n1;
this.n2 = n2;
this.n3 = n3;
}
public int getN1() {
return n1;
}
public void setN1(int n1) {
this.n1 = n1;
}
public int getN2() {
return n2;
}
public void setN2(int n2) {
this.n2 = n2;
}
public int getN3() {
return n3;
}
public void setN3(int n3) {
this.n3 = n3;
}
@Override
public String toString() {
return this.getX() + ", " + this.getY() + ", " +
this.getN1() + ", " + this.getN2() + ", " + this.getN3();
}
}
controller 패키지 - TriangleController 클래스
package com.kh.practice.point.controller;
import com.kh.practice.point.model.vo.Triangle;
public class TriangleController {
private Triangle t = new Triangle();
public String calcArea(int x, int y, int n1, int n2, int n3) {
t.setX(x); t.setY(y); t.setN1(n1); t.setN2(n2); t.setN3(n3);
if(n1 + n2 <= n3 | n1 + n3 <= n2 | n2 + n3 <= n1) {
return null;
}else {
double s = (t.getN1() + t.getN2() + t.getN3()) / 2.0;
return "면적 : " + t.toString() + " / " +
Math.sqrt(s*(s-(double)t.getN1())*(s-(double)t.getN2())*(s-(double)t.getN3()));
}
}
public String calcPerimeter(int x, int y, int n1, int n2, int n3) {
t.setX(x); t.setY(y); t.setN1(n1); t.setN2(n2); t.setN3(n3);
if(n1 + n2 <= n3 | n1 + n3 <= n2 | n2 + n3 <= n1) {
return null;
}else {
return "둘레 : " + t.toString() + " / " +
(t.getN1() + t.getN2() + t.getN3());
}
}
}
난이도 ★★★★★★☆
예제 3
Q : 위 프로그램을 활용하여 다음 요구사항을 만족하는 프로그램을 작성하라.
● 위 프로그램과 동일한 구조를 가지지만,
사각형 메뉴에서 '사각형 쪼개기' 라는 메뉴를 생성하고
가로로 쪼갤것인지, 세로로 쪼갤 것인지, 가로 및 세로로 쪼갤 것인지 사용자 입력을 받아
사각형을 쪼개었을 때 나오는 사각형들을 객체배열을 생성해 담고,
그 사각형들의 가로 세로 길이를 출력하여라.
풀이 :
controller 패키지 - RectangleController 클래스 일부
public String calcSplit(int x, int y, int height, int width, int split) {
r.setX(x); r.setY(y); r.setHeight(height); r.setWidth(width);
String ret = "";
Rectangle[] rArr = new Rectangle[split];
if(split < 0) {
return "split은 양수로 입력하셔야 합니다.";
}else{
System.out.print("가로로만 나누려면 1, 세로로만 나누려면 2,"
+ "가로 와 세로 둘 다 나누려면 3 : ");
int num = sc.nextInt(); sc.nextLine();
if(num==1) {
int splitHeight = r.getHeight() / split;
for(int i=0; i<rArr.length; i++) {
rArr[i] = new Rectangle(x, y, width, height);
}
for (Rectangle r: rArr) {
r.setHeight(splitHeight);
ret += r.toString() + " / ";
}
return ret;
}else if(num==2) {
int splitWidth = r.getWidth() / split;
for(int i=0; i<rArr.length; i++) {
rArr[i] = new Rectangle(x, y, width, height);
}
for (Rectangle r: rArr) {
r.setHeight(splitWidth);
ret += r.toString() + " / ";
}
return ret;
}else if(num==3) {
int sqr=0;
int i=1;
while(true) {
sqr = i*i;
if(sqr == split) {
int splitWidth = r.getWidth() / split;
int splitHeight = r.getHeight() / split;
for(int j=0; j<rArr.length; j++) {
rArr[j] = new Rectangle(x, y, width, height);
}
for (Rectangle r: rArr) {
r.setWidth(splitWidth);
r.setHeight(splitHeight);
ret += r.toString() + " / ";
}
return ret;
}else if(sqr > split) {
return "해당 split으로는 사각형을 나눌 수 없습니다.";
}
i++;
}
}
}
return ret;
}
view 패키지 - PointMenu 클래스 일부
public void calcRectSplit() {
int split;
System.out.print("x 좌표 : ");
int x = sc.nextInt(); sc.nextLine();
System.out.print("y 좌표 : ");
int y = sc.nextInt(); sc.nextLine();
System.out.print("높이 : ");
int height = sc.nextInt(); sc.nextLine();
System.out.print("너비 : ");
int width = sc.nextInt(); sc.nextLine();
System.out.print("분리 갯수 : ");
split = this.sc.nextInt(); sc.nextLine();
System.out.println(rc.calcSplit(x, y, height, width, split));
this.mainMenu();
}
이상 java의 상속 실습 문제를 마치겠습니다.
다음 포스팅에선 java 다형성에 대해 다뤄보고자 합니다.
https://healthdevelop.tistory.com/entry/java16