samedi 10 octobre 2015

If Else StatementsTraffic Light Coding Java Blue J

I basically have to code a traffic light to start at red, then red to green and back to red again. I manage to get the traffic light from flashing red to green but not vice versa, can someone look at my code and see what I have done wrong on the if and else statements.

    /**  * Write a description of class TrafficLight here.  *   *
    @author (your name)   * @version (a version number or a date)  */
    public class TrafficLight {
        // instance variables - replace the example below with your own
        private Circle red;
        private Circle yellow;
        private Circle green;
        private Boolean stop;

        /**
         * Constructor for objects of class TrafficLight
         */
        public TrafficLight()
        {

            red = new Circle();
            red.changeColor("red");
            red.moveHorizontal(200);
            red.moveVertical(200);
            red.changeSize(60);
            red.makeVisible();
            stop=true;
            stop=false;

            yellow = new Circle();
            yellow.changeColor("black");
            yellow.moveHorizontal(200);
            yellow.moveVertical(260);
            yellow.changeSize(60);
            yellow.makeVisible();

            green = new Circle();
            green.changeColor("black");
            green.moveHorizontal(200);
            green.moveVertical(320);
            green.changeSize(60);
            green.makeVisible();


            // Construct the circles
            // Set their color and make them visible
            // Set stop to be true;
        }

        /**
         * If the traffic light shows STOP, it changes to GO.
         * If the traffic light shows GO, it changes to STOP.
         */
        public void change() {

            // read the instructions for the user story 
            // of how this method should work

            if (stop==false) {
                red.changeColor("red");
                yellow.changeColor("yellow");
                green.changeColor("black");
                pause();
                red.changeColor("black");
                yellow.changeColor("black");
                green.changeColor("green");
                pause();

            } else{ 
                red.changeColor("black");
                yellow.changeColor("yellow");
                green.changeColor("black");
                pause();
                red.changeColor("red");
                yellow.changeColor("black");
                green.changeColor("black");
            }

            /*
             * Do not change anything in the pause method
             */

        }   

        private void pause() {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
        }

    }

Aucun commentaire:

Enregistrer un commentaire