전체 방문자
오늘
어제
21종
종이의 코딩 공부방
21종
  • 분류 전체보기 (171)
    • JAVA (64)
    • Springboot (46)
      • 블로그만들기 (45)
    • Database (60)
      • Oracle (60)
    • 프로젝트 3 (CELOVER) (0)
    • 개발서버 구축 (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

인기 글

최근 글

최근 댓글

hELLO · Designed By 정상우.
21종

종이의 코딩 공부방

JAVA

[자바/JAVA] 프로그래밍 - 상속 (inheritance) 실습 문제_2

2023. 6. 12. 19:28

[상속 실습문제2] 다음과 같은 조건을 만족하는 프로그램을 작성 하시오

도형의 x,y 좌표 값과 각 도형의 면적, 둘레를 계산하는 프로그램이다. 해당 구현 클래스 다이어그 램과 클래스의 구조를 참고하여 프로젝트를 완성하시오.

 


Point Class

package com.hw2.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;
	}
	
	public void draw() {
		 System.out.print("x : " + x + ", y : " + y);
	}

}

Circle Class

package com.hw2.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 void draw() {
		super.draw();
		System.out.printf(", radius : %d, 면적 : %.1f, 둘레 : %.1f",radius, Math.PI * radius * radius , Math.PI * radius * 2);
	}
}

Rectangle Class

package com.hw2.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 void draw() {
		super.draw();
		System.out.print(", 면적 : " + width * height + ", 둘레 : " + (width + height)*2);
	}

}

실행 Class

package com.hw2.run;

import com.hw2.model.vo.Circle;
import com.hw2.model.vo.Rectangle;

public class Run {
	public static void main(String[] args) {
		
		// 크기가 2인 Circle, Rectangle 각각 객체 배열 할당
		Circle[] c = new Circle[2];
		Rectangle[] r = new Rectangle[2];
		
		// 위의 사용 데이터를 참고하여 각각 초기화
		c[0] = new Circle(1, 2, 3);
		c[1] = new Circle(3, 3, 4);
		
		r[0] = new Rectangle(-1, -2, 5, 2);
		r[1] = new Rectangle(-2, 5, 2, 8);
		
		// 각 도형의 draw 메소드 실행
		for(int i = 0; i < c.length; i++) {
			c[i].draw();
			System.out.println();
		}
		for(int i = 0; i < r.length; i++) {
			r[i].draw();
			System.out.println();
		}
		
	}

}

컴파일

    'JAVA' 카테고리의 다른 글
    • [자바/JAVA] 프로그래밍 - 다형성 (Polymorphism) - 추상(abstract)
    • [자바/JAVA] 프로그래밍 - 다형성 (Polymorphism)
    • [자바/JAVA] 프로그래밍 - 상속 (inheritance) 실습 문제_1
    • [자바/JAVA] 프로그래밍 - 오버라이드 (Override)
    21종
    21종
    코딩 공부한 것 정리하려고 만든 블로그

    티스토리툴바