Notice
Recent Posts
Recent Comments
Link
«   2025/03   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
Tags
more
Archives
Today
Total
관리 메뉴

archive

[9주차 실습] 4월 28일 Problem Set 본문

Java/23_1 소프트웨어프로젝트

[9주차 실습] 4월 28일 Problem Set

안정민 2023. 4. 28. 02:35

The Point class and the main method is as follows. Implement the ColorPoint class and the Point3D class so that the following console window can be shown.

package April27;

public class Point {
	private int x, y;
	public Point(int x, int y) {
		this.x=x;
		this.y=y;
	}
	public int getX() {return x;}
	public int getY() {return y;}
	protected void move(int x, int y) {
		this.x=x;
		this.y=y;
	}
	
}
package April27;

public class ColorPoint extends Point{
	private String color;
	public ColorPoint(int x, int y, String color) {
		super(x, y);
		this.color=color;
	}
	
	public ColorPoint() {
		super(0,0);
		this.color="black";
	}
	
	public ColorPoint(int x, int y) {
		super(x, y);
		this.color="black";
	}
	public void setXY(int x, int y){
		move(x, y);
	}
	public void setColor(String color) {
		this.color=color;
	}
	
	public String toString() {
		String str = "The "+color+"point at ("+getX()+","+getY()+")";
	    return str;
	}
	
}
package April27;

public class Point3D extends Point{
	private int z;
	
	public Point3D(int x, int y, int z) {
		super(x, y);
		this.z=z;
	}
	
	public String toString() {
		String str="The point at ("+getX()+", "+getY()+", "+z+")";
		return str;
	}
	
	public void moveUp() {
		z++;
	}
	public void moveDown() {
		z--;
	}
	
	public void move(int x, int y, int z) {
		move(x, y); // 얘는 메소드 오버라이딩이 아님, 매개변수가 다르기 때문에 아님, 그래서 super 키워드를 쓰면 안 되고 바로 함수 호출해야함
		this.z=z;
	}
}
package April27;

public class PointExample {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		ColorPoint cp = new ColorPoint(5, 5, "yellow");
		cp.setXY(10, 20);
		cp.setColor("red");
		String str = cp.toString();
		System.out.println(str);
		
		ColorPoint zeroPoint=new ColorPoint();
		System.out.println(zeroPoint.toString());
		
		ColorPoint blackPoint=new ColorPoint(5,5);
		System.out.println(blackPoint.toString());
		
		Point3D p=new Point3D(1,2,3);
		System.out.println(p.toString());
		
		p.moveUp();
		System.out.println(p.toString());
		
		p.moveDown();
		System.out.println(p.toString());
		
		p.move(10, 10);
		System.out.println(p.toString());
		
		p.move(100,200, 300);
		System.out.println(p.toString());
	}

}

 실행결과