p340~344p에 있는 예제 소개하려고합니다.
-----------------------------------------------------------------------------------
1번째 예제
●결과값
public class Tire {
//필드
public int maxRotation; //최대 회전수, 타이어의 수명, 최대 회전수에 도달하면 타이어 펑크
public int accumulatedRotation;//누적 회전수, 타이어가 1번 회전할 때마다 1씩 증가
//누적회전수 == 최대회전수 타이어펑크
public String location; // 타티어 위치, 앞왼쪽, 앞오른쪽, 뒤왼쪽, 뒤오른쪽
// 앞왼쪽 : frontLeftTire
// 앞 오른쪽 : frontRightTire
// 뒤 왼쪽 : backLeftTire
// 뒤 오른쪽 : backRightTire
/*
roll() 메소드는 타이어를 1회전시키는 메소드
1번 실행할 때마다 누적 회전수가 1식 증가 : accumlatedRotation++
누적회전수 < 최대 회전수 : 남은 회전수를 출력
== : 펑크 출력
if(누적회전수 < 최대 회전수 ){
return true; 정상이면 true
}else { 누적회전수 == 최대 회전수 일때,
return false; 펑크 : false
}
roll() 메소드의 리턴 타입 : boolean
정상이면 true
펑크 : false
*/
//생성자 - 타이어 위치와 최대 회전수 매개값
public Tire(String location, int maxRotation) {
this.location = location;
this.maxRotation = maxRotation;
}
//메소드
boolean result = false;
public boolean roll() { //roll() 메소드의 리턴 타입 : boolean
++accumulatedRotation; // ++accumulatedRotation, 1번 실행할 때마다 누적 회전수가 1식 증가
if(accumulatedRotation<maxRotation) { //누적회전수 < 최대 회전수
System.out.println(location + " Tire 수명 : " + (maxRotation-accumulatedRotation)+"회");
//return true; //true false를 안에다가 쓰는것은 읽기가 어렵다. 그러므로 밖에서 return값을 받을수있게 true,false값을 변수로 주는것이 좋다.
result = true;
}else { //누적회전수 == 최대 회전수 일때, 펑크 출력
// ~~위치의 타이어가 펑크 남을 표시
System.out.println("*** " + location + " Tire 펑크 ***");
//return false;
result = false;
}
return result;
}
}
public class KumhoTire extends Tire {
//필드
//생성자
public KumhoTire(String location, int maxRotation) {
super(location, maxRotation); //super 부모 Tire객체를 호출, 매개값을 가져감.
}
//메소드
@Override //재정의 - 부모 것을 고쳐쓴다.
public boolean roll() {//roll() 메소드의 리턴 타입 : boolean
++accumulatedRotation; // ++accumulatedRotation , 1번 실행할 때마다 누적 회전
if(accumulatedRotation<maxRotation) { //누적회전수 < 최대 회전수
//어느 위치에 타이어 수명이 ~~회 남음을 표시
System.out.println(location + "KumhoTire 수명: " + (maxRotation-accumulatedRotation + "회"));
return true;
}else { //누적 ==최대 회전수 일때, 펑크출력
//~~위치의 타이어가 펑크 남을 표시
System.out.println("*** " + location + " KumhoTire 펑크 ***");
return false;
}
}
}
public class HanKookTire extends Tire {
//필드
//생성자 - 생성자이름과 클래스이름은 같아야 한다.
public HanKookTire(String location, int maxRotation) {
super(location, maxRotation); //부모 Tire 객체를 호출
}
//메소드
@Override
public boolean roll() {
++accumulatedRotation;
if(accumulatedRotation<maxRotation) {
System.out.println(location + " HankookTire 수명: " + (maxRotation-accumulatedRotation) + "회");
return true;
}else {
System.out.println("*** " + location + " HanKookTire 펑크 ***");
return false;
}
}
}
public class Car {
/*
frontLeftTire : 최대 회전수가 6이라면, 6이 되면 펑크
// 앞왼쪽 : frontLeftTire
// 앞오른쪽 : frontRightTire
// 뒤왼쪽 : backLeftTire
// 뒤오른쪽 : backRightTire
run() : 4개의 타이어를 한 번씩 1회전시킴
roll() : 각각의 Tire 객체
*/
//필드 : Tire의 생성자 public Tire(String,
Tire frontLeftTire = new Tire("앞왼쪽",6); //위치, 최대 회전수
Tire frontRightTire = new Tire("앞오른쪽",2); //위치, 최대 회전수
Tire backLeftTire = new Tire("뒤왼쪽",3); //위치, 최대 회전수
Tire backRightTire = new Tire("뒤오른쪽",4); //위치, 최대 회전수
//생성자 - 생략되면 기본 생성자 컴파일시 생성
//메소드
//run() : 4개의 타이어를 한 번씩 1회전시킴
int run() { //(생략가능하기떄문에, 아무것도없다면) default(접근제한자)라는 뜻 ,같은 패키지의 클래스들에서 사용가능
System.out.println("[자동차가 달립니다.]");
if(frontLeftTire.roll()==false) {stop(); return 1; } //stop메소드 호출, 펑크이기 때문에 //해당위치의 터이어번호
if(frontRightTire.roll()==false) {stop(); return 2; }//stop메소드 호출, 펑크이기 때문에 //해당위치의 터이어번호
if(backLeftTire.roll()==false) {stop(); return 3; }//stop메소드 호출, 펑크이기 때문에 //해당위치의 터이어번호
if(backRightTire.roll()==false) {stop(); return 4; }//stop메소드 호출, 펑크이기 때문에 //해당위치의 터이어번호
return 0; // 위 조건이 모두 만족하지 않을 때 리턴값
}//end of run
void stop() {//접근제한자 default - 생략하면 default, 같은 패키지의 클래스들에서 사용가능
System.out.println("[자동차가 멈춥니다.]");
}
public class CarExample {
public static void main(String[] args) {
Car car = new Car();
for(int i = 1; i <= 5; i++) {
int problemLocation = car.run(); //for문 한번 돌때마다 switch문에서 1~4번 (앞/뒤)왼쪽,오른쪽 위치 확인...
switch(problemLocation) {
case 1:
System.out.println("앞왼쪽 HanKookTire로 교체");
car.frontLeftTire = new HanKookTire("앞왼쪽",15); //, 바퀴위치값(problemLocation)을 체크, 1값을 받을시 타이어교체
break;
case 2:
System.out.println("앞오른쪽 KumhoTire로 교체");
car.frontRightTire = new KumhoTire("앞오른쪽",13);//, 바퀴위치값(problemLocation)을 체크, 2값을 받을시 타이어교체
break;
case 3:
System.out.println("뒤왼쪽 HanKookTire로 교체");
car.backLeftTire = new HanKookTire("뒤왼쪽",14); //, 바퀴위치값(problemLocation)을 체크, 3값을 받을시 타이어교체
break;
case 4:
System.out.println("뒤오른쪽 KumhoTire로 교체");
car.backRightTire = new KumhoTire("뒤오른쪽",17); //, 바퀴위치값(problemLocation)을 체크, 4값을 받을시 타이어교체
break;
}
System.out.println("--------------------------"); // 1회전시 출력되는 내용의 구분선
}
}
}
'1. JAVA > 4). 자바_실전_이클립스' 카테고리의 다른 글
자바실전_Day_23_01(숫자찍기(별찍기)) (0) | 2024.01.31 |
---|---|
자바실전_Day_19_01(추상메소드, 상속 - 제품출력하기) (0) | 2024.01.25 |
자바실전_Day_17_01(클래스 - 학생정보출력 - 리팩토링) (2) | 2024.01.23 |
자바실전_Day_15_02(요일맞추기게임) (0) | 2024.01.19 |
자바실전_Day_15_01(똑같은 클래스를 사용하는 방법) (0) | 2024.01.19 |