CarTest

C/Java : 2007. 11. 6. 14:26

//CarTest.java


public class CarTest {
 static final int COUPE = 1;
 static final int CONVERTIBLE = 2;
 static final int T_TOP = 3;
 
 static final int V4 = 1;
 static final int V6 = 2;
 static final int V8 = 3;
 static final int V10 = 4;
 
 static int engineType;
 static int bodyType;
 static int topSpeed;
 static int gas;
 static int oil;
 static boolean isRunning;
 static int currentSpeed;
 
 public static void turnOn() {
  isRunning = true;
 }
 
 public static void turnOff() {
  isRunning = false;
 }
 
 public static void accelerate() {
  switch(engineType) {
  case V4:
   speedUp(2);
   break;
  case V6:
   speedUp(3);
   break;
  case V8:
   speedUp(4);
   break;
  case V10:
   speedUp(5);
   break;
  }
 }
 
 public static void speedUp(int amount) {
  if(isRunning == false) {     //자동차가 멈춰있으면 아무것도 하지 않고 리턴
   return;
  }
 
  if((currentSpeed + amount) >= topSpeed) {
   currentSpeed = topSpeed;
  }
 
  else {
   currentSpeed += amount;
  }
 }
 
 public static void decelerate() {
  if(isRunning == false) {      //자동차가 멈춰있으면 아무것도 하지 않고 리턴
   return;
  }
  if((currentSpeed - 5) <= 0) {
   currentSpeed = 0;
  }
  else {
   currentSpeed -= 5;
  }
 }
 
 public static void main(String[] args) {
  engineType = V10;
  bodyType = CONVERTIBLE;
  topSpeed = 185;
  isRunning = false;
  currentSpeed = 0;     //자동차의 속성
 
  turnOn();    //자동차를 출발시킨다.
  for(int i=0; i<10; i++) {
   accelerate();
   System.out.println("Current Speed a: " + currentSpeed);
  }
  for(int i=0; i<5; i++) {
   decelerate();
   System.out.println("Current Speed d: " + currentSpeed);
  }
  turnOff();
 }
}



======================================================

 

여러대의 자동차를 표현하고자 할때

자동차의 속성을 따로 클래스로 빼내 정의할수 있다.


//Car.java


public class Car {
 static final int COUPE = 1;
 static final int CONVERTIBLE = 2;
 static final int T_TOP = 3;
 
 static final int V4 = 1;
 static final int V6 = 2;
 static final int V8 = 3;
 static final int V10 = 4;
 
 static int engineType;
 static int bodyType;
 static int topSpeed;
 static int gas;
 static int oil;
 static boolean isRunning;
 static int currentSpeed;
}


//CarTest2.java


public class CarTest2 {


 public static void turnOn(Car c) {
  c.isRunning = true;
 }
 
 public static void turnOff(Car c) {
  c.isRunning = false;
 }
 
 public static void accelerate(Car c) {
  switch(c.engineType) {
  case Car.V4:
   speedUp(c, 2);
   break;
  case Car.V6:
   speedUp(c, 3);
   break;
  case Car.V8:
   speedUp(c, 4);
   break;
  case Car.V10:
   speedUp(c, 5);
   break;
  }
 }
 
 public static void speedUp(Car c, int amount) {
  if(c.isRunning == false) {
   return;
  }
 
  if((c.currentSpeed + amount) >= c.topSpeed) {
   c.currentSpeed = c.topSpeed;
  }
 
  else {
   c.currentSpeed += amount;
  }
 }
 
 public static void decelerate(Car c) {
  if(c.isRunning == false) {
   return;
  }
  if((c.currentSpeed - 5) <= 0) {
   c.currentSpeed = 0;
  }
  else {
   c.currentSpeed -= 5;
  }
 }
 
 public static void main(String[] args) {
  Car c1 = new Car();           //자동차의 속성을 정의한다. 객체를 생성한다.
  c1.engineType = Car.V10;
  c1.bodyType = Car.CONVERTIBLE;
  c1.topSpeed = 185;
  c1.isRunning = false;
  c1.currentSpeed = 0;
 
  turnOn(c1);
  for(int i=0; i<10; i++) {
   accelerate(c1);
   System.out.println("Current Speed a: " + c1.currentSpeed);
  }
  for(int i=0; i<5; i++) {
   decelerate(c1);
   System.out.println("Current Speed d: " + c1.currentSpeed);
  }
  turnOff(c1);
 }
}


Posted by 청웨일