JAVA/자바 실습 / / 2021. 6. 9. 03:11

Java | Lab10

01 도형관리 : DObject 배열 정의

º 다음과 같은 모양의 배열을 main()에서 생성해 보시오.

<1-1> 추상 클래스(DObject)

public abstract class DObject {
	int width, height;
    double area;
    
    public DObject(int w, int h) {
    	width = w;
        height = h;
    }
    
    public void computeArea() {
    	area = width * height;
    }
    
    public abstract void show();
}

<1-2> Triangle 클래스

public class Triangle extends DObject {

	public Triangle(int w, int h) {
		super(w, h);
	}
	
	public void computeArea() {
		area = (width * height) / 2.0;
	}
	
	@Override
	public void show() {
		System.out.print("Triangle : ");
		System.out.println(width + " " + height + " " + area);

	}

}

<1-3> Rectangle 클래스

public class Rectangle extends DObject {

	public Rectangle(int w, int h) {
		super(w, h);
	}
	
	public void computeArae() {
		area = width * height;
	}
	
	@Override
	public void show() {
		System.out.print("Rectangle : ");
		System.out.println(width + " " + height + " " + area);
	}
}

<1-4> Rectangle2 클래스

public class Rectangle2 extends DObject {

	public Rectangle2(int w) {
		super(w, w);
	}
	
	public void computeArea() {
		area = width * width;
	}
	
	@Override
	public void show() {
		System.out.print("Rectangle2 : ");
		System.out.println(width + " " + height + " " + area);

	}
}

<1-5> ColorTriangle 클래스

public class ColorTriangle extends DObject {

	String color;
	
	public ColorTriangle(int w, int h, String c) {
		super(w, h);
		color = c;
	}
	
	public void computeArea() {
		area = (width * height) / 2.0;
	}
	
	@Override
	public void show() {
		System.out.print("ColorTriangle : ");
		System.out.println(width + " " + height + " " + area + " " + color);

	}
}

<1-6> ShapeEx 클래스

public class ShapeEx {
	public static void main(String[] args) {
		Triangle t1 = new Triangle(3, 4);
		t1.computeArea();
		t1.show();
		
		Rectangle r1 = new Rectangle(4, 5);
		r1.computerArea();
		r1.show();
		
		ColorTriangle ct1 = new ColorTriangle(6, 7, "red");
		ct1.computeArea();
		ct1.show();
		
		Rectangle2 r2 = new Rectangle2(7);
		r2.computeArea();
		r2.show();
		
		Rectangle r3 = new Rectangle(10, 12);
		r3.computerArea();
		r3.show();
		
		DObject ob[] = new DObject[5];
		ob[0] = t1;
		ob[1] = r1;
		ob[2] = ct1;
		ob[3] = r2;
		ob[4] = r3;

	}

}

 

02 도형관리 : DObject 배열 활용

<2-1> ShapeEx 클래스 추가 코드

public class ShapeEx {
	public static void main(String[] args) {
		Triangle t1 = new Triangle(3, 4);
		t1.computeArea();
		t1.show();
		
		Rectangle r1 = new Rectangle(4, 5);
		r1.computerArea();
		r1.show();
		
		ColorTriangle ct1 = new ColorTriangle(6, 7, "red");
		ct1.computeArea();
		ct1.show();
		
		Rectangle2 r2 = new Rectangle2(7);
		r2.computeArea();
		r2.show();
		
		Rectangle r3 = new Rectangle(10, 12);
		r3.computerArea();
		r3.show();
		
		DObject ob[] = new DObject[5];
		ob[0] = t1;
		ob[1] = r1;
		ob[2] = ct1;
		ob[3] = r2;
		ob[4] = r3;
		
		System.out.println("---------------------------");
		for(int i=0; i<ob.length; i++ ) {
			ob[i].computerArea();
			ob[i].show();
		}
	}
}

<3> ShapeEx 클래스 추가 코드 - Rectangle만 출력

public class ShapeEx {
	public static void main(String[] args) {
		Triangle t1 = new Triangle(3, 4);
		t1.computeArea();
		t1.show();
		
		Rectangle r1 = new Rectangle(4, 5);
		r1.computerArea();
		r1.show();
		
		ColorTriangle ct1 = new ColorTriangle(6, 7, "red");
		ct1.computeArea();
		ct1.show();
		
		Rectangle2 r2 = new Rectangle2(7);
		r2.computeArea();
		r2.show();
		
		Rectangle r3 = new Rectangle(10, 12);
		r3.computerArea();
		r3.show();
		
		DObject ob[] = new DObject[5];
		ob[0] = t1;
		ob[1] = r1;
		ob[2] = ct1;
		ob[3] = r2;
		ob[4] = r3;
		
		System.out.println("---------------------------");
		System.out.println("Rectangle만 출력");
		for(int i=0; i<ob.length; i++) {
			if(ob[i] instanceof Rectangle) ob[i].show();
		}
	}

}

<4> ShapeEx 클래스 추가 코드 - ColorTriangle 객체들만 출력

public class ShapeEx {
	public static void main(String[] args) {
		Triangle t1 = new Triangle(3, 4);
		t1.computeArea();
		t1.show();
		
		Rectangle r1 = new Rectangle(4, 5);
		r1.computerArea();
		r1.show();
		
		ColorTriangle ct1 = new ColorTriangle(6, 7, "red");
		ct1.computeArea();
		ct1.show();
		
		Rectangle2 r2 = new Rectangle2(7);
		r2.computeArea();
		r2.show();
		
		Rectangle r3 = new Rectangle(10, 12);
		r3.computerArea();
		r3.show();
		
		DObject ob[] = new DObject[5];
		ob[0] = t1;
		ob[1] = r1;
		ob[2] = ct1;
		ob[3] = r2;
		ob[4] = r3;
		
		System.out.println("---------------------------");
		System.out.println("ColorTriangle만 출력");
		for(int i=0; i<ob.length; i++) {
			if(ob[i] instanceof ColorTriangle) ob[i].show();
		}
	}

}

<5> ShapeEx 클래스 추가 코드 - Triangle 객체들만 출력

public class ShapeEx {
	public static void main(String[] args) {
		Triangle t1 = new Triangle(3, 4);
		t1.computeArea();
		t1.show();
		
		Rectangle r1 = new Rectangle(4, 5);
		r1.computerArea();
		r1.show();
		
		ColorTriangle ct1 = new ColorTriangle(6, 7, "red");
		ct1.computeArea();
		ct1.show();
		
		Rectangle2 r2 = new Rectangle2(7);
		r2.computeArea();
		r2.show();
		
		Rectangle r3 = new Rectangle(10, 12);
		r3.computerArea();
		r3.show();
		
		DObject ob[] = new DObject[5];
		ob[0] = t1;
		ob[1] = r1;
		ob[2] = ct1;
		ob[3] = r2;
		ob[4] = r3;
		
		System.out.println("---------------------------");
		System.out.println("Triangle만 출력");
		for(int i=0; i<ob.length; i++) {
			if(ob[i] instanceof Triangle && !(ob[i] instanceof ColorTriangle)) ob[i].show();
		}
	}

}

 

03 수열 관리 인터페이스

<6-1> Series interface

public interface Series {
	int getNext();  //return next number in series
	void reset();   //restart
	void setStart(int x);  //set starting value
}

<6-2> 구현 클래스(ByTwos)

public class ByTwos implements Series {
	int start;
	int val;
	
	public ByTwos() {
		start = 0;
		val = 0;
	}
	
	@Override
	public int getNext() {
		val += 2;
		return val;
	}

	@Override
	public void reset() {
		start = 0;
			val = 0;

	}

	@Override
	public void setStart(int x) {
		start = x;
			val = x;
	}
}

<6-3> 구현 클래스(ByThrees)

public class ByThrees implements Series {
	int start;
	int val;
	
	public ByThrees() {
		start = 0;
		val = 0;
	}
	
	@Override
	public int getNext() {
		val += 3;
		return val;
	}

	@Override
	public void reset() {
		start = 0;
			val = 0;

	}

	@Override
	public void setStart(int x) {
		start = x;
			val = x;

	}
}

<6-4> 실행 클래스(SeriesDemo1)

public class SeriesDemo1 {

	public static void main(String[] args) {
		ByTwos ob = new ByTwos();
		for(int i=0; i<5; i++)
			System.out.println("Next value is " + ob.getNext());
		System.out.println("\nResetting");
		ob.reset();
		for(int i=0; i<5; i++)
			System.out.println("Next value is " + ob.getNext());
		System.out.println("\nStarting at 100");
		ob.setStart(100);
		for(int i=0; i<5; i++)
			System.out.println("Next value is " + ob.getNext());
	}
}

<6-5> 실행 클래스(SeriesDemo2)

public class SeriesDemo2 {
	public static void main(String[] args) {
		ByTwos twoOb = new ByTwos();
		ByThrees threeOb = new ByThrees();
		Series ob;
		
		for(int i=0; i<5; i++) {
			ob = twoOb;
			System.out.println("Next ByTwos value is " + ob.getNext());
			ob = threeOb;
			System.out.println("Next ByThrees value is " + ob.getNext());
		}
	}
}

 

04 패키지 만들기

<7-1> Calculator 클래스

package lib;

public abstract class Calculator {
	public abstract int add(int a, int b);
	public abstract int sibtract(int a, int b);
	public abstract double average(int[] a);
}

<7-2> GoodCalc 클래스

package app;
import lib.Calculator;

public class GoodCalc extends Calculator {
	
	public int add(int a, int b) {
			return a + b;
	}
	public int subtract(int a, int b) {
		return a - b;
	}
	public double average(int[] a) {
		double sum = 0;
		for(int i=0; i<a.length; i++)
			sum += a[i];
		return sum/a.length;
	}
	public static void main(String[] args) {
		Calculator c = new GoodCalc();
		System.out.println(c.add(2,  3));
		System.out.println(c.subtract(2,  3));
		System.out.println(c.average(new int[] {2,3,4}));
	}

}

 

'JAVA > 자바 실습' 카테고리의 다른 글

Java | Lab11  (0) 2021.06.09
Java | Lab9  (0) 2021.06.08
Java | Lab8  (0) 2021.06.08
Java | Lab6  (0) 2021.04.16
Java | Lab5  (0) 2021.04.15
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유