Coding/JAVA
[명품자바] 05장 상속 실습문제 5번/6번/7번
찡콩찡
2022. 4. 19. 20:12
5번. point를 상속받아 색을 가진 점을 나타내는 ColorPoint 클래스를 작성하라. 다음 main() 메소드를 포함하고 실행 결과와 같이 출력하게 되라.
package chapter05.colorpoint;
class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
// getters&setters
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;
}
protected void move(int x, int y) {
this.x = x;
this.y = y;
}
}
class ColorPoint extends Point {
private String color;
public ColorPoint(int x, int y, String color) {
super(x, y);
this.color = color;
}
public void setXY(int x, int y) {
super.move(x, y);
}
// getters&setters
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
// toString
@Override
public String toString() {
return getColor() + "색의 , (" + getX() + "," + getY() + ")의 점";
}
}
public class ColorPoint23 {
public static void main(String[] args) {
ColorPoint cp = new ColorPoint(5, 5, "YELLOW");
cp.setXY(10, 20);
cp.setColor("RED");
String str = cp.toString();
System.out.println(str + "입니다");
}
}
결과 값>>
6번. Point를 상속받아 색을 가진 점을 나타내는 ColorPoint 클래스를 작성하라. 다음 main() 메소드를 포함하고 실행 결과와 같이 출력되게 해라
package chapter05.colorpoint;
class ColorPoint2 extends Point {
private String color; //속성
public ColorPoint2(int x,int y,String color) {
super(x,y);
this.color = color;
}
public ColorPoint2(int x, int y) {
super(x, y);
}
public ColorPoint2() {
super(0,0);
this.color="black";
}
public void setXY(int x, int y) {
super.move(x, y);
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return color=" 컬러의 색은 : " + color + "(" + getX() + getY() + ")";
}
}
public class Ex5_6 {
public static void main(String[] args) {
ColorPoint2 zeroPoint = new ColorPoint2(); //(0,0) 위치의 black 색 점
System.out.println(zeroPoint.toString() + "입니다.");
ColorPoint2 cp = new ColorPoint2(10,10); //(10,10) 위치의 black 색 점
cp.setXY(5, 5);
cp.setColor("RED");
System.out.println(cp.toString() + "입니다");
}
}
결과 값>>
7. Point를 상속받아 3차원의 점을 나타내는 Point3D 클래스를 작성하라. 다음 main() 메소드를 포함하고 실행 결과와 같이 출력되게 하라
package chapter05.colorpoint;
class Point3D extends Point {
private int z;
public Point3D(int x, int y, int z) {
super(x, y);
this.z = z;
}
public int getZ() {
return z;
}
public void setZ(int z) { //z값받아서 저장
this.z = z;
}
public void moveUp() {
this.z += 1;
}
public void moveDown() { //동작만 하고 끝
this.z -= 1;
}
public void move(int x,int y, int z) {
super.move(x,y);
this.z=z;
}
@Override
public String toString() {
return "(" + getX() + "," + getY() + "," + getZ() + ")의 점";
}
}
public class Ex5_7 {
public static void main(String[] args) {
Point3D p = new Point3D(1,2,3); //1,2,3은 각각 x,y,z,축의 값
System.out.println(p.toString() + "입니다");
p.moveUp(); //z 축으로 위쪽 이동
System.out.println(p.toString() + "입니다");
p.moveDown(); //z 축으로 아래쪽 이동
p.move(10,10); //x,y축으로 이동
System.out.println(p.toString() + "입니다");
p.move(100, 200, 300); //x,y,z 축으로 이동
System.out.println(p.toString() + "입니다.");
}
}
결과 값 >>