Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New to the CF scene
    Join Date
    Jul 2010
    Location
    England
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Class called Train

    I am in the process of creating a new class called train. Ideally I am looking for some coding guidance for the sections in bold. If somebody inserts the completed code then thats cool. Although any explanations/pointers would obviously aid learning rather than me just copying the completed code. This is what I have so far which appears to be correct.

    /**
    * Class Train -
    * This class causes a representation of a train to be created in the Shapes
    * Window, and for it to move across the screen
    *
    * @author ()
    * @version (1.0)
    */
    import ou.*;
    public class Train
    {
    private Square engine; // represents the train's engine
    private Square cabin; // represents the train's cabin
    private Square funnel; // represents the train's funnel
    private Square window; // represents the train's window
    private Circle frontWheel; // represents the train's frontwheel
    private Circle backWheel; // represents the train's backwheel
    private Circle smoke; // represents a puff of smoke


    /**
    * Constructor for objects of class Train
    */
    public Train(Square e, Square c, Square f, Square w, Circle fw, Circle bw, Circle s)
    {
    super();
    this.engine = e;
    this.cabin = c;
    this.funnel = f;
    this.window = w;
    this.frontWheel = fw;
    this.backWheel = bw;
    this.smoke = s;
    this.makeTrain(); // code to be written same as in bold below
    }

    /**
    * Moves the cabin of the receiver Train object to its initial position
    */
    private void setInitialPosCabin()
    {
    this.getCabin().setLength(80);
    this.getCabin().setYPos(220);
    this.getCabin().setXPos(0);
    this.getCabin().setColour(OUColour.RED);
    }

    /**
    * Moves the engine of the receiver Train object to its initial position
    */
    private void setInitialPosEngine()
    {
    this.getEngine().setColour(OUColour.RED);
    this.getEngine().setLength(50);
    this.getEngine().setYPos(250);
    this.getEngine().setXPos(80);
    }

    /**
    * Moves the funnel of the receiver Train object to its initial position
    */
    private void setInitialPosFunnel()
    {
    this.getFunnel().setColour(OUColour.BLACK);
    this.getFunnel().setLength(20);
    this.getFunnel().setYPos(230);
    this.getFunnel().setXPos(110);
    }

    /**
    * Moves the window of the receiver Train object to its initial position
    */
    private void setInitialPosWindow()
    {
    this.getWindow().setColour(OUColour.BLACK);
    this.getWindow().setLength(20);
    this.getWindow().setYPos(230);
    this.getWindow().setXPos(50);
    }


    /**
    * Moves the frontwheel of the receiver Train object to its initial position
    */
    private void setInitialPosFrontWheel()
    {
    this.getFrontWheel().setDiameter(30);
    this.getFrontWheel().setXPos(100);
    this.getFrontWheel().setYPos(295);
    }

    /**
    * Moves the backwheel of the receiver Train object to its initial position
    */
    private void setInitialPosBackWheel()
    {
    this.getBackWheel().setDiameter(30);
    this.getBackWheel().setXPos(0);
    this.getBackWheel().setYPos(295);
    }

    /**
    * Moves the smoke of the receiver Train object to a home
    * position relative to the funnel.
    */
    private void setHomePosSmoke()
    {
    //code to be written
    }

    /**
    * Returns the Square object representing the engine of the receiver Train object
    */
    private Square getEngine()
    {
    return this.engine;
    }

    /**
    * Returns the Square object representing the cabin of the receiver Train object
    */
    private Square getCabin()
    {
    return this.cabin;
    }

    /**
    * Returns the Square object representing the funnel of the receiver Train object
    */
    private Square getFunnel()
    {
    return this.funnel;
    }

    /**
    * Returns the Square object representing the window of the receiver Train object
    */
    private Square getWindow()
    {
    return this.window;
    }

    /**
    * Returns the Circle object representing the frontwheel of the receiver Train object
    */
    private Circle getFrontWheel()
    {
    return this.frontWheel;
    }

    /**
    * Returns the Circle object representing the backwheel of the receiver Train object
    */
    private Circle getBackWheel()
    {
    return this.backWheel;
    }


    /**
    * Returns the Circle object representing the smoke of the receiver Train object
    */
    private Circle getSmoke()
    {
    return this.smoke;
    }

    /**
    * Moves all the component parts of the train into their initial positions
    */
    private void makeTrain()
    {
    // code to be written
    }

    /**
    * Moves each component of the receiver Train object (except the smoke)
    * by anInt units to the right
    */
    public void moveTrainBy(int anInt)
    {
    // code to be written
    }

    /**
    * Repeatedly moves the train int1 units to the right (except the smoke)
    * by int2 number of times. The smoke, however moves separately as follows:
    * Every move the train makes, the smoke moves 5 units backwards and 10 units up,
    * and its diameter increases by 3. However at every sixth move, the smoke
    * is reset to its initial size and position above the funnel.
    */
    public void animateTrain(int int1, int int2)
    {
    // Code to be written. Might need to include the message
    // this.delay(100) as the last statement in the loop (alter the size
    // of the argument to speed up or slow down the motion).
    }

    /**
    * Gets the number of units for each move from the user, then the
    * method gets the number of moves from the user.
    * If (number of units * number of moves) is more than 890, the user
    * is told that the train will not run and the method ends.
    * Otherwise the train moves as required.
    */
    public void run()
    {
    // code to be written
    }

    /**
    * Causes execution to pause by time number of milliseconds
    */
    private void delay(int time)
    {
    try
    {
    Thread.sleep(time);
    }
    catch (Exception e)
    {
    System.out.println(e);
    }
    }

    }



    By executing the following lines one at a time:
    Square s1 = new Square();
    Square s2 = new Square();
    Square s3 = new Square();
    Square s4 = new Square();
    Circle c1 = new Circle();
    Circle c2 = new Circle();
    Circle c3 = new Circle();
    Train t = new Train(s1,s2,s3,s4,c1,c2,c3);
    Last edited by Hyp3r; 11-28-2010 at 10:25 PM.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •