public class Car {
// フィールド
private String model;
private String color;
private int speed;
// コンストラクタ
public Car(String model, String color) {
this.model = model;
this.color = color;
this.speed = 0; // 車ãŒç”Ÿæˆã•れãŸã¨ãã®åˆæœŸé€Ÿåº¦ã¯0ã¨ã—ã¾ã™ã€‚
}
// ゲッターã¨ã‚»ãƒƒã‚¿ãƒ¼
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
// é€Ÿåº¦ã‚’å¢—åŠ ã•ã›ã‚‹ãƒ¡ã‚½ãƒƒãƒ‰
public void accelerate(int speedIncrease) {
this.speed += speedIncrease;
}
// 速度を減少ã•ã›ã‚‹ãƒ¡ã‚½ãƒƒãƒ‰
public void decelerate(int speedDecrease) {
// 速度ãŒ0未満ã«ãªã‚‰ãªã„よã†ã«åˆ¶å¾¡
if (this.speed - speedDecrease >= 0) {
this.speed -= speedDecrease;
} else {
this.speed = 0;
}
}
// ç¾åœ¨ã®é€Ÿåº¦ã‚’表示ã™ã‚‹ãƒ¡ã‚½ãƒƒãƒ‰
public void displaySpeed() {
System.out.println("Current Speed: " + speed + " km/h");
}
}
ã“ã®ä¾‹ã§ã¯ã€Car クラスãŒè»Šç¨®ã€è‰²ã€é€Ÿåº¦ã‚’ä¿æŒã—ã€ãれãžã‚Œã®æƒ…å ±ã‚’å–å¾—ãŠã‚ˆã³è¨å®šã™ã‚‹ãŸã‚ã®ã‚²ãƒƒã‚¿ãƒ¼ã¨ã‚»ãƒƒã‚¿ãƒ¼ã‚’æä¾›ã—ã¦ã„ã¾ã™ã€‚ã¾ãŸã€accelerate() メソッド㨠decelerate() メソッドã§é€Ÿåº¦ã‚’増減ã•ã›ã€displaySpeed() メソッドã§ç¾åœ¨ã®é€Ÿåº¦ã‚’表示ã—ã¾ã™ã€‚è‡ªåˆ†ã§æ‰‹ã‚’å‹•ã‹ã—ã¦ã€ã“ã®ä¾‹ã‚’ã‚‚ã¨ã«å®Ÿéš›ã®ã‚³ãƒ¼ãƒ‡ã‚£ãƒ³ã‚°ã‚’行ã£ã¦ã¿ã¦ãã ã•ã„。
「007 クラスã€å•題集リスト
🎯 実習ã§ç†è§£ã‚’æ·±ã‚よã†
ã“ã®å†…容ã«ã¤ã„ã¦JavaDrillã§å®Ÿéš›ã«æ‰‹ã‚’å‹•ã‹ã—ã¦å¦ç¿’ã§ãã¾ã™


コメント