Java新手 - 汽车应用程序

时间:2012-10-02 00:47:52

标签: 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

很抱歉,如果这贴了所有的东西。我打字很好,但预览看起来很棘手。

8 个答案:

答案 0 :(得分: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,而不是将当前状态分配给showState变量...我是哪个已使用enum

进行了更正

答案 1 :(得分: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;

答案 2 :(得分:1)

使用enum是个不错的主意。 这是一个使用Enum的实现,Enum的默认实现和使用类型系统的自己的实现。 此外,没有任何条件,如if或switch使用。 只是纯粹而美丽的Java Code。

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);
  }
}

答案 3 :(得分: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);

这不是解决方案,但它提供了解决方案。你会找到解决方案,你会觉得它的味道

答案 4 :(得分: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.isStarted写为isStarted == false的简写。

答案 5 :(得分: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);

答案 6 :(得分: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");
}

}

我希望它有所帮助。干杯

答案 7 :(得分: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;
    }

}