Hi,
I am trying to construct a class program in Java but cannot get my method to work? In my frustration I could be missing something very obvious but as I am new to all this I am not sure.
When the code is run it should build a train but all I can get is the smoke?
I think it is a problem with the method.... this.makeTrain();
But I could be completly mistaken?
Could anyone advise on where I am going wrong?
I have posted my complete code below for reference. All help appreciated.
/** * 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 () */ 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(); // will be written in Q4(i) } /** * 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() { // to be written in Q4(ii) } /** * 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() { //to be written in Q4(i) } /** * Moves each component of the receiver Train object (except the smoke) * by anInt units to the right */ public void moveTrainBy(int anInt) { // to be written in Q4(iii) } /** * 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) { // To be written in Q4(iv). You might need to include the message // this.delay(100) as the last statement in your 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() { // To be written in Q4(v) } /** * 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); } } }