Results 1 to 3 of 3
  1. #1
    ajfonty is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default New to Methods -- need help with a counting program

    Hi all,

    I'm making a class that functions as a counting-based internal clock that can be referenced by other classes.

    So far, I have several methods in the clock class. My question pertains to only a few of them, which I've included below.

    Java Code:
    public class WorldTime
    {
        private int timevalue;
        public Boolean startclock;
        
        public void runinternalClock()
        {
            timevalue++;
            
            try 
            {
                Thread.sleep(1000);
            }
                
            catch (InterruptedException e) // dummy value so far.
            {
                    
            }
                
            if (timevalue == 2400)
            {
                resetTime();
            }
        }
                
        public void motorClock()
        {
            while (runTime(true))
            {
                runinternalClock();
            }
        }
        
        public int getTime()
        {
            return timevalue;
        }
            
        public void resetTime()
        {
            timevalue = 0;
        }
        
        public Boolean runTime(Boolean begin)
        {
            if (begin)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    I've written a test class to see if the internal clock works.

    Java Code:
    public class TimeTest 
    {
        public static void main(String args[])
        {
            System.out.println("Enter a value");
            Scanner scan = new Scanner(System.in);
            int x = scan.nextInt();
            
            if (x > 0)
            {
                try 
                {
                    Thread.sleep(5000);
                }
                
                catch (InterruptedException timevalue) // necessary.
                {                
                }
                
                System.out.println("The game's time is: " + openUp());
            }
        }
        
        public static int openUp()
        {
            TestRun tout = new TestRun();
            return tout.testRun();
        }
    }
    
    class TestRun extends TimeTest
    {
        WorldTime be = new WorldTime();
        public int testRun()
        {
            be.runTime(true);
            return be.getTime();
        }
    }
    However, I've received mixed results in my programming. Sometimes, the while loop fires only once, resulting in a timevalue = 1; other times, the output is 0, meaning the loop hasn't fired at all.

    Can anyone provide any advice for me?

    Thanks in advance!
    Last edited by ajfonty; 05-18-2012 at 08:09 PM.

  2. #2
    ajfonty is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default New to Methods -- need help with a counting program

    oops, double post.
    Last edited by ajfonty; 05-18-2012 at 08:09 PM.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,411
    Rep Power
    25

    Default Re: New to Methods -- need help with a counting program

    Can anyone provide any advice for me?
    Try debugging the code by Adding some println statements to see what it is doing and where the execution flow is going.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: 10-22-2011, 12:25 PM
  2. Program working fine until I add methods....
    By LadyMonsterFace in forum New To Java
    Replies: 3
    Last Post: 12-04-2010, 03:22 AM
  3. Incorporating Methods into my program
    By Moltisanti in forum New To Java
    Replies: 16
    Last Post: 10-12-2009, 06:48 AM
  4. Program To Call Random Methods
    By nyamtoko in forum Advanced Java
    Replies: 5
    Last Post: 12-01-2008, 11:49 AM

Posting Permissions

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