2012-10-02 49 views
0

我是學生,是的,這是我的作業。我花了上週的時間審閱筆記,閱讀這本書,並在網上研究相關主題,但我只是沒有明白問題所在。你能告訴我我做錯了什麼嗎?任何幫助將不勝感激。 (我只使用記事本和命令提示符。)Java新手 - 汽車應用

我得到的指導原則:創建一個由兩個類組成的Java應用程序。第一類將是你的應用程序類。第二類將是一個叫做Car的類。您的應用程序將創建一個名爲nova的Car實例,並將其驅動。

規則汽車:

  • 你不能開車,如果它未啓動(發送錯誤信息到控制檯)。
  • 如果汽車未啓動,則無法停車(向控制檯發送錯誤消息)。
  • 如果汽車已經啓動,則不能啓動汽車(向控制檯發送錯誤消息)。
  • 一旦你告訴汽車開車,你可以做的唯一的事情就是停止(發送消息到控制檯)
  • 一旦你呼叫停車,汽車將返回到初始狀態,用戶必須啓動汽車在嘗試執行任何其他功能之前。 (發送消息到控制檯)

showState方法的目的是提供一種方法來檢查汽車的狀態。它應該建立一條消息,然後可以發送到控制檯。

我的代碼:

public class MyAppAssignment3 
{ 
    public static void main (String[] args) 
    { 
     System.out.println("Scenario 1"); 
     Car nova1 = new Car(); 
     nova1.start(); 
     nova1.showState(); 
     nova1.drive(); 
     nova1.stop(); 
     nova1.showState(); 
     System.out.println(""); 

     System.out.println("Scenario 2"); 
     Car nova2 = new Car(); 
     nova2.showState(); 
     nova2.drive(); //needs to send error message - can't drive a car that's not started 
     nova2.stop(); 
     nova2.showState(); 
     System.out.println(""); 

     System.out.println("Scenario 3"); 
     Car nova3 = new Car(); 
     nova3.showState(); 
     nova3.start(); 
     nova3.showState(); 
     nova3.stop(); //needs to send error message - can't stop a car that's not driving 
     nova3.showState(); 
     nova3.drive(); 
     nova3.stop(); 
    } 
} 

class Car 
{ 
    private boolean isStarted; 
    private boolean isDriving; 
    private boolean isStopped; 
    private String showState; 

    public Car() 
    { 
     this.showState = showState; 
    } 

    public void start() 
    { 
     isStarted = true; 
     isDriving = false; 
     isStopped = false; 
     System.out.println("The car is " + this.showState); 
    } 

    public void drive() 
    { 
     isStarted = false; 
     isStopped = false; 
     isDriving = true; 
     System.out.println("The car is " + this.showState); 
    } 

    public void stop() 
    { 
     isStopped = true; 
     isStarted = false; 
     isDriving = false; 
     System.out.println("The car is " + this.showState); 
    } 

    public String showState() 
    { 
     if (isStarted) 
     { 
      showState = "started"; 
     } 
     else if(isDriving) 
     { 
      showState = "driving"; 
     } 
     else if(isStopped) 
     { 
      showState = "stopped"; 
     } 
     System.out.println("The car is " + this.showState); 
     return showState; 
    } 
} 

我的輸出(這是完全錯誤的 - 值是不正確的):

Scenario 1 
The car is null 
The car is started 
The car is started 
The car is started 
The car is stopped 

Scenario 2 
The car is null 
The car is null 
The car is null 
The car is stopped 

Scenario 3 
The car is null 
The car is null 
The car is started 
The car is started 
The car is stopped 
The car is stopped 
The car is stopped 

很抱歉,如果這個發佈的所有靠不住的。我輸入它很好,但預覽看起來很扭曲。

回答

0

這工作!感謝所有的幫助!

public class MyAppAssignment3 
{ 
    public static void main (String[] args) 
    { 
     System.out.println("Scenario 1"); 
     Car nova1 = new Car(); 
     nova1.start(); 
     nova1.showState(); 
     nova1.drive(); 
     nova1.stop(); 
     nova1.showState(); 
     System.out.println(""); 

     System.out.println("Scenario 2"); 
     Car nova2 = new Car(); 
     nova2.showState(); 
     nova2.drive(); 
     nova2.stop(); 
     nova2.showState(); 
     System.out.println(""); 

     System.out.println("Scenario 3"); 
     Car nova3 = new Car(); 
     nova3.showState(); 
     nova3.start(); 
     nova3.showState(); 
     nova3.stop(); 
     nova3.showState(); 
     nova3.drive(); 
     nova3.stop(); 
    } 
} 

class Car 
{ 
    private boolean isStarted; 
    private boolean isDriving; 
    private boolean isStopped; 
    private String showState; 

    public Car() 
    { 
     isStarted = false; 
     isDriving = false; 
     isStopped = true; 
    } 

    public void start() 
    { 
     if(isStarted == false) 
     { 
      isStopped = false; 
      isStarted = true; 
      showState(); 
     } 
     else 
     { 
      System.out.println("You can't start a car which is already started."); 
     } 

    } 

    public void drive() 
    { 
     if(isStarted) 
     { 
      isDriving = true; 
      showState(); 
     } 
     else 
     { 
      System.out.println("You can't drive a car which is not started."); 
     } 

    } 

    public void stop() 
    { 
     if(isStarted) 
     { 
      isStarted = false; 
      isDriving = false; 
      isStopped = true; 
      showState(); 
     } 
     else 
     { 
      System.out.println("You can't stop a car which is not started."); 
     } 

    } 

    public String showState() 
    { 
     if(isStarted && (isDriving == false)) 
     { 
      showState = "started"; 
     } 
     else if(isStarted && isDriving) 
     { 
      showState = "driving"; 
     } 
     else if(isStopped) 
     { 
      showState = "stopped"; 
     } 
     System.out.println("The car is " + this.showState + "."); 
     return showState; 
    } 

} 
0

我建議這是什麼,每節車廂都有自己獨特的ID:

class Car 
{ 
    private boolean isStarted; 
    private boolean isDriving; 
    private boolean isStopped; 
    private String showState; 
    private int id; 

    public Car(Integer id) 
    { 
     this.id = id; 
    } 
... 

} 

然後在你說打印出所有的地方,還包括ID:

System.out.println("The car id "+id+" is "+ this.showState); 

然後創建一個這樣的對象:

Car nova1 = new Car(1); 

Car nova2 = new Car(2); 

Car nova3 = new Car(3); 

這不是解決方案,但它提供瞭解決方案。你會找到解決方案,你會覺得它的味道

+0

+1爲ID的好方法來識別輸出; – MadProgrammer

2

對於你剛剛創建一個新的實例。你從來沒有真正爲這些實例設置默認值。

考慮至少是這樣的:

public Car() 
{ 
    isStopped = true; 
} 

,當你打電話給你的第一nova1.start();您可以檢查是否isStopped是允許它再次啓動之前,真的......

public void start() 
{ 
    if(isStopped) 
    { 
     isStarted = true;  
     isDriving = false;  
     isStopped = false; 
     showState = "started";  
     System.out.println("The car is " + this.showState); 
    } 
} 

就這樣一個例子。但是你可以很容易地使用它來推斷你的其他需求。我的觀點主要是你創建了一個實例,但是期望布爾值有一個值而不被指定。您可以在默認情況下或在構造函數中執行此操作。

例如:

private boolean isStarted = false; 
0

你必須在代碼翻譯,你一直在問什麼,並且你可以看到它甚至辦法接近實際的需求 - 例如:

你不能開車如果未開始(發送錯誤消息到控制檯)。

變爲:

public void drive() 
{ 

    if(this.isStarted == false){ 

     System.out.println("You should start the car first!"); 

    }else{ 

     System.out.println("Car is running!"); 

    } 

} 

注意,你可以寫!this.isStartedisStarted == false的簡寫。

4

這實際上並不做任何事情......

public Car() 
{ 
    this.showState = showState; 
} 

基本上,它只是重新分配相同的值回發到自身。我會改變到初始狀態,可能的stopped

我會使用enum我的車狀態,而不是依靠boolean狀態,這有可能成爲混亂......

public enum CarState { 
    Stopped, 
    Started, 
    Driving 
} 

然後簡單地分配它以一個單一的state變量...

class Car 
{ 
    private CarState state; 

    public Car() 
    { 
     this.state= CarState.Stopped; 
    } 

    public void start() 
    { 
     if (state.equals(State.Stopped)) { 
      state = CarState.Started; 
      showState(); 
     } else { 
      System.error.println("Car is not in a valid state to be started"); 
     } 
    } 

    public void drive() 
    { 
     if (state.equals(State.Started)) { 
      state = CarState.Driving; 
      showState(); 
     } else { 
      System.error.println("Car is not in a valid state to be driven"); 
     } 
    } 

    public void stop() 
    { 
     if (state.equals(State.Driving)) { 
      state = CarState.Stopped; 
      showState(); 
     } else { 
      System.error.println("Car is not in a valid state to be stopped"); 
     } 
    } 

    public String showState() 
    { 
     System.out.println("The car is " + state); 
    } 
} 

您遇到的另一個問題是,當你改變狀態showStatus沒有被調用,這是不分配當前統計e到showState變量...我已經通過使用enum

+0

枚舉在這裏絕對是一個好主意。 – tehdoommarine

0

嘗試輸出每個步驟中變量的值。邏輯流程中有幾個問題。例如,檢查構造函數。

public Car() 
{ 
    System.out.println(showState); 
    this.showState = showState; 
} 

沒有showState值被傳遞給構造函數,並且它沒有在函數內初始化。

另外,每個功能啓動裏面,停開車,你需要寫:的

System.out.println("The car is " + this.showState()); 

代替:

System.out.println("The car is " + this.showState); 
0

讓我們保持它的簡單,爲什麼要使用3個變量的時候,你只需要二?如果我錯了,請糾正,但如果一輛汽車沒有啓動,而且你沒有開着,那麼它就停了,對吧?看看我的班級:

public class car 
{ 
private boolean isStarted; 
private boolean isDriving; 

public car() 
{ 
    isStarted = false; 
    isDriving = false; 
    //Initial State 
    showState(); 
} 

public void start() 
{ 
    if(!isStarted) 
    { 
     if(!isDriving) 
      isStarted = true; 
    } 
    else 
     System.err.println("You can\'t start a car which is already started"); //You can’t start a car if it is already started (send an error message to the console). 
     showState(); 
} 

public void drive() 
{ 
    if(isStarted) 
     isDriving = true; 
    else 
     System.err.println("You can\'t drive a car which is not started"); 

    showState(); 
} 

public void stop() 
{ 
    if(isStarted) 
    { 
     isStarted = false; 
     isDriving = false; 
     // Once you call stop, the car will return to the initial state and the user must start the car before attempting to do any other functions. (Send a message to the console. (Below on ShowState) 
    } 
    else 
     System.err.println("You can\'t stop a car which is not started"); // You can’t stop a car if it is not started (send an error message to the console). 
    showState(); // Once you tell the car to drive, the only thing you can do is stop (Send a message to the console) 
} 

public void showState() 
{ 
    if(isStarted && isDriving) 
     System.out.println("It\'s Driving"); 
    if(!isStarted && !isDriving) 
     System.out.println("It\'s Stopped"); 
    if(isStarted && !isDriving) 
     System.out.println("It\'s Started"); 
} 

} 

我希望它有幫助。乾杯

+0

謝謝!它花了一段時間,但我拼湊了一些從你的回覆中起作用的東西。謝謝一堆! – user1713223

+0

不要忘了說一個答案是正確的。它爲你增加了聲譽點和誰回答。這是一個很好的方式,該網站找出你的問題,以及解決以及 –

1

使用枚舉是一個不錯的主意。 這是一個實現使用枚舉的枚舉的默認實現和使用類型系統自己的實現。 也沒有任何條件,如使用或開關使用。 只是純粹和美麗的Java代碼。

public class Car { 
private enum State { 
    OFF { 
    void start(Car c) { 
     System.out.println("Starting the car"); 
     c.state = State.STARTED; 
    } 
    }, 
    STARTED { 
    void stop(Car c) { 
     System.out.println("Stopping the car"); 
     c.state = State.OFF; 
    } 
    void drive(Car c) { 
     System.out.println("Driving the car"); 
     c.state = State.DRIVING; 
    } 
    }, 
    DRIVING { 
    void stop (Car c) { 
     System.out.println("Stopping the car"); 
     c.state = State.OFF; 
    } 
    }; 

    void start(Car c) { 
    System.err.println("Can't start"); 
    } 

    void stop(Car c) { 
    System.err.println("Can't stop"); 
    } 

    void drive(Car c) { 
    System.err.println("Can't drive"); 
    } 
} 
    private State state = State.OFF; 

    public void start(){ 
    state.start(this); 
    } 

    public void stop(){ 
    state.stop(this); 
    } 
    public void drive() { 
    state.drive(this); 
    } 

    public void showState(){ 
    System.out.println("The car is "+state); 
    } 
} 
+0

非常不錯 –