Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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
- 어노테이션
- produces
- AOP
- 비즈니스레이어
- @
- 의존주입
- application.properties
- 서비스레이어
- .xml
- frontController
- SpringBoot
- 유효성검사
- @RequestMapping
- after-throwing
- springjdbc
- @RequestParam
- springmvc
- MVC
- Java
- gradle
- 생성자주입
- @Valid
- spring
- PointCut
- c:if
- Model
- 바인딩변수
- jointpoint
- 스프링
- @ResponseBody
Archives
- Today
- Total
메모장
[실습문제] 상속 (응용문제) 본문
728x90
반응형
문제
===== 메뉴 =====
1. 점 생성
2. 점 목록 출력
3. 점 1개 출력
4. 점 이동
5. 색 변경
6. 점 합치기 (+)
7. 프로그램 종료
===============
메뉴를 출력 하고
Point[] data=new Point[3]; // 최대 3개 // 배열 만들기
new Point(); // 점(0,0) 기본값
new Point(3,4); // 점(3,4)
new ColorPoint(); // 검정(0,0) // 색깔 기본값
new ColorPoint(1,2); // 검정(1,2)
new ColorPoint("빨강",3,4); // 빨강(3,4)
점(1,2)
검정(2,3)
분홍(-1,-3)
point.move(); -> +1 +1 // 점 이동 할때 기본값
point.move(10); -> +10 +10
point.move(1,2); -> +1 +2
point.changeColor("빨강")
1,2 + 2,3 => 3,5
package class04;
import java.util.Scanner;
class Point {
int x; // x값
int y; // y값
Point() { // 기본 생성자
this(0, 0);
}
Point(int x, int y) {
this.x = x;
this.y = y;
}
void move() { // 한칸 움직인다고 가정
move(1);
}
void move(int x) {
move(x, x);
}
void move(int x, int y) {
this.x += x;
this.y += y;
}
@Override
public boolean equals(Object obj) {
Point point = (Point) obj;
if (this.x == point.x) {
if (this.y == point.y) {
return true; // x값,y값이 둘다 맞을때 true
}
}
return false;
}
@Override
public String toString() { // 출력 메서드 오버라이딩
return "점(" + this.x + "," + this.y + ")";
}
}
class ColorPoint extends Point { // Point 상속받기
String color;
ColorPoint() {
this(0, 0);
}
ColorPoint(int x, int y) {
this("검정", x, y);
}
ColorPoint(String color, int x, int y) {
super(x, y);
this.color = color;
}
void changeColor(String color) {
this.color = color;
System.out.println("색 변경 완료!");
}
@Override
public boolean equals(Object obj) {
ColorPoint colorPoint = (ColorPoint) obj;
if ((this.x == colorPoint.x) && (this.y == colorPoint.y) && (this.color.equals(colorPoint.color))) {
return true; // 색깔,x값,y값 반환
}
return false;
}
@Override
public String toString() {
return this.color + "(" + this.x + "," + this.y + ")";
}
}
public class Test02 {
// INPUT: 점 2개를 받아서
// OUTPUT: 출력하고 끝! void
// 6번 점 합치기를 실행할때 사용할 메서드 선언
public static void addPoint(Point point1,Point point2) {// 점합치기는 메서드로 쓰기
Point resPoint=new Point();
resPoint.x=point1.x+point2.x; // x값은 x값끼리 더하고
resPoint.y=point1.y+point2.y; // y값은 y값끼리 더하기
System.out.println(point1); // 점1과
System.out.println("더하기");
System.out.println(point2); // 점2를
System.out.println("결과) "+resPoint); // 둘을 더한 결과
}
public static void main(String[] args) {
Point[] data=new Point[3]; // 3칸 배열 만들기
Scanner sc=new Scanner(System.in);
int index=0;
while(true) {
System.out.println("1. 점 생성"); // 메서드로 쓸 수 없다 xxx
System.out.println("2. 점 목록 출력"); // 메서드로 쓸 수 없다 xxx
System.out.println("3. 점 1개 출력");
System.out.println("4. 점 이동");
System.out.println("5. 색 변경");
System.out.println("6. 점 합치기");
System.out.println("7. 프로그램 종료");
System.out.print("입력) ");
int action=sc.nextInt();
if(action==1) {
System.out.println("1. 점 생성");
System.out.println("2. 색깔 점 생성");
System.out.print("입력) ");
action=sc.nextInt();
if(action==1) {
data[index++]=new Point();
}
else { // 2번을 눌렀을때
System.out.print("색 입력) ");
String color=sc.next();
data[index++]=new ColorPoint(color,10,20); //색도 입력된 값
}
}
else if(action==2) {
// 배열에 저장된 데이터 개수(index)만큼
// 점을 출력해줘~~ index 가 들어가야함 /length가 아님
for(Point point:data) {
if(point==null) {
break;
}
System.out.println(point);
///// == System.out.println(point.toString()); // 같은 말이다.
}
}
else if(action==3) {
System.out.print("index 번호 입력) ");
action=sc.nextInt();
System.out.println(data[action]);
}
else if(action==4) {
System.out.print("index 번호 입력) ");
action=sc.nextInt();
data[action].move(); // 1칸 이동한다.
System.out.println(data[action]);
}
else if(action==5) {
System.out.print("index 번호 입력) ");// 어떤거를 색을 변경할거야?
//걔가 색을 변경할수 있는 애인지를 확인 해야한다.
action=sc.nextInt();
// 객체 instanceof 클래스
// 객체가 클래스거야?
// 점 instanceof Point T
// 색깔점 instanceof Point T
// 어떤 애의 타입, 자료형, 출신 클래스를 물어보는 연산자: instanceof
// T 참일때 실행 할수 있다.
if(data[action] instanceof ColorPoint) { // data[action]이 색깔점이야?
ColorPoint cp=(ColorPoint)data[action];
System.out.print("색 입력) ");
String color=sc.next();
cp.changeColor(color);
}
else {
System.out.println("색 변경 불가능!");
}
System.out.println(data[action]);
}
else if(action==6) {
// 점A와 점B를 더해줘~~ O
// 점A야, 점B를 더해! xxxxx
// ☆ 기능을 개발할때에는 항상
// "주어(주체)"가 누구인지?
// "대상(인자)"가 누구인지?
System.out.print("index 번호 입력1) ");
action=sc.nextInt();
System.out.print("index 번호 입력2) ");
int action2=sc.nextInt();
addPoint(data[action],data[action2]);
}
else if(action==7) {
System.out.print("프로그램을 종료합니다. ");
break;
}
유효성 검사는 빠져있다 ...
728x90
반응형
'JAVA > [실습문제]' 카테고리의 다른 글
| [실습문제] 자판기프로그램 MVC패턴 (0) | 2024.04.08 |
|---|---|
| [실습문제] 포켓몬(추상클래스, private) (0) | 2024.04.08 |
| [실습문제] 상속 (예제 만들기) (0) | 2024.04.08 |
| [실습 문제] 상속 (포켓몬) (0) | 2024.04.08 |
| [자판기 프로그램] (0) | 2024.04.08 |