package chaptor06Example;
public class NewCar {
// 필드
private double speed;
private String color;
private static final double MAX_SPEED = 200.0 / 1.6;
// 생성자
public NewCar() {
}
public NewCar(String color) {
this.color = color;
}
// 메소드
public double getSpeed() {
return mileToKilo(speed);
}
public void setSpeed(double speed) {
this.speed = killoToMile(speed);
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean speedUp(double speed) {
speed = killoToMile(speed);
if (speed + this.speed >= 0 && speed + this.speed <= MAX_SPEED) {
this.speed += speed;
return true;
} else {
return false;
}
}
static double getMaxSpeed() {
return mileToKilo(MAX_SPEED);
}
static private double killoToMile(double distance) {
return distance / 1.6;
}
static private double mileToKilo(double distance) {
return distance * 1.6;
}
public String toString() {
return String.format("%d", speed);
}
}
package chaptor06Example;
public class NewCarTest {
public static void main(String[] args) {
NewCar myCar = new NewCar("red");
System.out.println("myCar의 색 :"+ myCar.getColor());
System.out.println("차의 최대 속력: "+ NewCar.getMaxSpeed() + "km/h");
System.out.print("첫번째 speedup(100.0km/h): ");
if(myCar.speedUp(100.0)) {
System.out.print("속도 변경 가능, ");
}else {
System.out.print("속도 변경 불가능, ");
}
System.out.println("현재 차의 속력:" + myCar.getSpeed()+"km/h");
System.out.print("두번째 speedUp(90.0km/h): ");
if(myCar.speedUp(90.0)) {
System.out.print("속도 변경 가능, ");
}else {
System.out.print("속도 변경 불가능, ");
}
System.out.println("현재 차의 속력:"+ myCar.getSpeed()+"km/h");
NewCar yourCar = new NewCar("blue");
System.out.println();
System.out.println("yourCar의 색 :" + yourCar.getColor());
System.out.println("차의 최대 속력: "+ NewCar.getMaxSpeed()+"km/h");
System.out.print("첫번째 speedp(-100.0km/h): ");
if(yourCar.speedUp(-100.0)) {
System.out.print("속도변경가능, ");
}else {
System.out.print("속도 변경 불가능, ");
}
System.out.println("현재 차의 속력: "+ yourCar.getSpeed()+"km/h");
System.out.print("두번째 speedUp(210.0km/h): ");
if(yourCar.speedUp(210.0)) {
System.out.print("속도변경가능, ");
}else {
System.out.print("속도 변경 불가능, ");
}
System.out.println("현재 차의 속력:"+ yourCar.getSpeed()+"km/h");
}
}