2012-10-02 50 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

非常不错 –