인터페이스

 

추상 메서드만을 가진 순수한 추상 클래스

static final 데이터 멤버를 가질 수 있음.

추상메서드의 극단적 형태라고 생각하면 이해하기 쉽다!

 


Shape 프로그램

 

사각형, 원, 직각삼각형 등의 도형들을 입력받아 저장하고

면적과 둘레 길이를 계산하는 기능

 

실행 예

$ add R 1 2

$ add C 5

$ show // 정보출력

 1. Rectangle : width is 1, height is 2

 2. Circle : radius is 5

$showdetail // 면적 높이 계산 결과 출력

 1. Rectangle : width is 1, height is 2

   -The area is 2.0

   - The perimeter is 6.0

 2. Circle : radius is 5

   - The area is 78.539816339744683

   - The perimeter is 31.41592653589793

 

$ add R 2 6

$ sort //면적에 대해서 오름차순으로 정렬

$ exit //종료

 


package chapter5;

public abstract class Shape {
	public  String shapeName;
	
	public Shape(String name) {
		shapeName =name;
	}
	
	public abstract double computearea();
	public abstract double computePerimeter();

}

Shape클래스 각 도형의 부모 추상클래스

 

package chapter5;

public class Rectangle extends Shape{
	public int width;
	public int height;
	
	public Rectangle(int w, int h) {
		super("Rectangle");
		width = w;
		height = h;
	}
	
	public double computearea() {
		return (double)width*height;
	}
	
	public double computePerimeter() {
		return 2.0*(width+height);
	}
	
	public String toString() {
		return "Rectangle: width is " + width + ", height is " + height;
	}

	
	
}

사각형 클래스

 

package chapter5;

public class Circle extends Shape{
	public int radius;
	
	public Circle(int r) {
		super("Circle");
		radius = r;
	}
	
	public double computearea() {
		return Math.PI * radius * radius;
	}
	
	public double computePerimeter() {
		return 2.0*Math.PI*radius;
	}
	
	public String toString() {
		return "Circle: radius is" + radius;
	}
	
}

원 클래스

 

package chapter5;

import java.util.Scanner;

public class ShapeApplication {
	
	private int capacity = 10;
	private Shape[] Shapes = new Shape[capacity];
	private int n=0;
	private Scanner kb = new Scanner(System.in);
	
	public void processCommand() {
		while(true) {
			System.out.print("$ ");
			String command = kb.next();
			if(command.equals("add")) {
				handleAdd();
			}
			else if(command.equals("show")||command.equals("showdetail")){
				handleShow(command.equals("showdetail"));
			}
			else if(command.equals("sort")) {
				handleSort();
			}
			else if(command.equals("exit")) {
				break;
			}
		}
		
	}
	private void handleSort() {
		// TODO Auto-generated method stub
		
	}
	private void handleShow(boolean detailed) {
		for(int i =0; i<n; i++) {
			System.out.println((i+1)+". "+Shapes[i].toString());
			if(detailed) {
				System.out.println("      The area is "+ Shapes[i].computearea());
				System.out.println("      The perimeter is "+ Shapes[i].computePerimeter());
			}
		}
	}
	private void handleAdd() {
		String type = kb.next();
		switch(type) {
		
		case "R": 
			addShape(new Rectangle(kb.nextInt(),kb.nextInt()));
			break;
			
		case "C":
			addShape(new Circle(kb.nextInt()));
			break;
		case "T":
			//생략
		}
		
	}
	private void addShape(Shape shape) {
		if(n >= capacity)
			reallocate();
		Shapes[n++] = shape;
		
	}
	private void reallocate() {
		capacity *=2;
		Shape[] tmp = new Shape[capacity];
		System.arraycopy(Shapes, 0, tmp, 0, Shapes.length);
		Shapes = tmp;
	}
	public static void main(String[] args) {
		ShapeApplication app = new ShapeApplication();
		app.processCommand();
	}

}

메인 함수를 포함한 클래스 

 

 

아직 미완성 되었지만 지금까지 잘된 것을 확인할 수 있다.

+ Recent posts