Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Method & Class confusion

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Method & Class confusion

    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);
    } 
    }
     
    }
    Last edited by helloworld922; December 6th, 2010 at 10:49 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Method & Class confusion

    When posting code, make sure you use the code tags to preserve formatting. Also, code should be in the form of an SSCCE- you posted a bunch of extra stuff that we don't need to see.

    But just glancing at it, your makeTrain() method is empty. What did you expect it to do?

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Method & Class confusion

    Why do you have a call to super when the only superclass Train appears to have is Object?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Method & Class confusion

    Quote Originally Posted by javapenguin View Post
    Why do you have a call to super when the only superclass Train appears to have is Object?
    Why not? Sure, it's not really "doing" anything, but what's it hurting? He has other more important errors than this, so pointing this out seems only distracting.

  5. #5
    Junior Member
    Join Date
    Dec 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Red face Re: Method & Class confusion

    I have figured out the first part now. A bit of time away from the computer and I realised, as correctly pointed out, that my makeTrain() method was completely empty.



    The only bit I am still confused about is the:

    public void moveTrainBy(int anInt)
    {
    }

    in the fact that I am not sure how to specify how I would like the program to move? How do I tie is back to the original variable?


  6. #6
    Junior Member
    Join Date
    Dec 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Method & Class confusion

    This is what I have written so far but I am confused about what the necessary methods are. Any help to point me into the correct direction very much appeciated:

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

    {
    this.setInitialPosCabin(this.getCabin() + anInt);
    this.setInitialPosEngine(this.getEngine() + anInt);
    this.setInitialPosFunnel(this.getFunnel() + anInt);
    this.setInitialPosWindow(this.getWindow() + anInt);
    this.setInitialPosFrontWheel(this.getFrontWheel() + anInt);
    this.setInitialPosBackWheel(this.getBackWheel() + anInt);
    }

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Method & Class confusion

    Sorry, but we don't have your requirements, we don't have your code, and honestly, most people wouldn't have the time to read through them anyway. This isn't a homework service. If you're really that confused about what you're supposed to be doing, I suggest you consult with your instructor.

    Otherwise, ask a specific question and post an SSCCE. And don't forget the code tags.

  8. #8
    Junior Member
    Join Date
    Dec 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Re: Method & Class confusion

    Many thanks for your help and my most sincere apologies about wasting your time

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Method & Class confusion

    Quote Originally Posted by jog98 View Post
    Many thanks for your help and my most sincere apologies about wasting your time
    I didn't say you were wasting our time. I said that if you want help, you have to provide more concise code and ask a specific question.

Similar Threads

  1. Override class method
    By mekie in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 31st, 2010, 07:06 PM
  2. Accessing a method of one class in another class
    By Sai in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 23rd, 2010, 04:06 PM
  3. Help Calling Method From Another Class
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 15th, 2010, 10:24 AM
  4. Scanner class error "java.lang.Error"
    By Lheviathan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 21st, 2009, 02:23 AM
  5. Confusion in creating class with OOPS concept
    By grbsmj in forum Object Oriented Programming
    Replies: 3
    Last Post: May 6th, 2009, 03:14 AM