匿名使用者
匿名使用者 發問時間: 電腦與網際網路程式設計 · 8 年前

java程式語言

public class EX203_1 {

class Car{

int year = 0;

String brand = "";

int speed = 0;

void Constructor(int y,String b){

year = y;

brand = b;

}

void Constructor(int y,String b,int s){

year =y;

brand = b;

speed = s;

}

void Accessors(int y,String b,int s){

this.year = y;

this.brand = b;

this.speed = s;

}

void Accelerate(){

speed += 10;

}

void Brack(){

if(speed>=10)speed -= 10;

else speed = 0;

}

}

class SportsCar extends Car{

void Accelerate(){

speed += 20;

}

void Brack(){

if(speed>=20)speed -= 20;

else speed = 0;

}

}

class Bus extends Car{

void Accelerate(){

speed += 5;

}

void Brack(){

if(speed>=5)speed -= 5;

else speed = 0;

}

}

public static void main(String args[]){

}

}

麻煩哪位大大可以幫忙,因為真的寫不下去了,感謝~!

撰寫一測試程式,各產生一個Car object,一個SportsCar object和一個Bus object (分別利用不同參數的Constructor產生),然後顯示目前三種汽車的速度。接下來分別呼叫accelerate method和brake method 2次,每次呼叫後必須顯示目前汽車的速度。

已更新項目:

還有同學寫其他部分,所以統一用java ^^~

2 個已更新項目:

高手大大您好~可以方便給個註解

小妹剛學程式不久,有點看不懂

例如:

System.exit(0);

@Override

用途是???

3 個解答

評分
  • 8 年前
    最佳解答

    public static class Car{

    int year = 0;

    String brand = "";

    int speed = 0;

    public Car(int y,String b){

    this.year = y;

    this.brand = b;

    }

    public Car(int y,String b,int s){

    this.year =y;

    this.brand = b;

    this.speed = s;

    }

    void Accessors(int y,String b,int s){

    this.year = y;

    this.brand = b;

    this.speed = s;

    }

    void Accelerate(){

    speed += 10;

    System.out.println("目前車速:"+speed);

    }

    void Brack(){

    if(speed>=10)speed -= 10;

    else speed = 0;

    System.out.println("目前車速:"+speed);

    }

    }

    static class SportsCar extends Car{

    public SportsCar(int y, String b, int s) {

    super(y, b, s);

    }

    void Accelerate(){

    speed += 20;

    System.out.println("目前車速:"+speed);

    }

    void Brack(){

    if(speed>=20)speed -= 20;

    else speed = 0;

    System.out.println("目前車速:"+speed);

    }

    }

    static class Bus extends Car{

    public Bus(int y, String b, int s) {

    super(y, b, s);

    }

    void Accelerate(){

    speed += 5;

    System.out.println("目前車速:"+speed);

    }

    void Brack(){

    if(speed>=5)speed -= 5;

    else speed = 0;

    System.out.println("目前車速:"+speed);

    }

    }

    public class EX203 {

    public static void main(String args[]){

    Car car = new Car(2000,"toyota",0);

    SportsCar sc = new SportsCar(2001,"JAGUAR",0);

    Bus bus = new Bus(2002,"hino",0);

    System.out.println("目前是CAR:");

    car.Accelerate();

    car.Accelerate();

    car.Brack();

    car.Brack();

    System.out.println("目前是SportsCar:");

    sc.Accelerate();

    sc.Accelerate();

    sc.Brack();

    sc.Brack();

    System.out.println("目前是bus:");

    bus.Accelerate();

    bus.Accelerate();

    bus.Brack();

    bus.Brack();

    }

    }

    參考資料: 稍微修改一下
  • John
    Lv 7
    8 年前

    public class EX203_1 {

    public static void main(String[] args) {

    Car sportCar = new SportCar(2013, "porsche");

    Car bus = new Bus(2000, "toyota");

    Car familyCar = new Car(2013, "honda");

    for (int i = 0; i < 2; i++) {

    sportCar.accelerate();

    bus.accelerate();

    familyCar.accelerate();

    }

    for (int i = 0; i < 2; i++) {

    sportCar.brake();

    bus.brake();

    familyCar.brake();

    }

    System.exit(0);

    }

    }

    class Car {

    private int year = 0;

    private String brand;

    private int speed;

    public Car(int year, String brand) {

    this.year = year;

    this.brand = brand;

    }

    public void setSpeed(int speed) {

    this.speed = speed;

    }

    public int getSpeed() {

    return this.speed;

    }

    public void increasedBy(int speed) {

    this.speed += speed;

    showDashboard();

    }

    public void decreasedBy(int speed) {

    if (getSpeed() >= speed) {

    this.speed -= speed;

    } else {

    this.stop();

    }

    showDashboard();

    }

    private void stop() {

    this.speed = 0;

    }

    public void accelerate() {

    increasedBy(10);

    }

    public void brake() {

    decreasedBy(10);

    }

    public String getBrand() {

    return this.brand;

    }

    public int getYear() {

    return this.year;

    }

    public void showDashboard() {

    System.out.format("%s (%d) speed: %d%n", getBrand(), getYear(),

    getSpeed());

    }

    }

    class SportCar extends Car {

    public SportCar(int year, String brand) {

    super(year, brand);

    }

    @Override

    public void accelerate() {

    super.increasedBy(20);

    }

    @Override

    public void brake() {

    super.decreasedBy(20);

    }

    }

    class Bus extends Car {

    public Bus(int year, String brand) {

    super(year, brand);

    }

    @Override

    public void accelerate() {

    super.increasedBy(5);

    }

    @Override

    public void brake() {

    super.decreasedBy(5);

    }

    }

  • 8 年前

    為何,不一開始就用「C語言」開發?

還有問題?馬上發問,尋求解答。