6 Replies - 36884 Views - Last Post: 25 January 2009 - 10:30 PM Rate Topic: -----

#1 undergroundthry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 24-June 08

Java Clock Program

Posted 19 January 2009 - 07:34 PM

A friend gave me a practice problem where I have to create a clock (not GUI; I'm still learning how to program) and I'm not sure where or how to start. I have to include a value called "precision" which determines how fast the clock will move. I tried to map it out using pseudocode and this is all I came up with...

"Create a class "Clock"

Initialize the hour, minute, and second aspects of the clock
Double precision

Create a constructor to initialize the clock
If the seconds is greater than or equal to 60 seconds add a count to minute
If the minutes is greater than or equal to 3600 seconds, add a count to hour

Create a method to establish the precision, called "Tick"
"

I'm not sure if this is remotely right. I'm also stuck on creating a method to initiate the precision. Precision should range from 0 to 3600 seconds. Could somebody help walk me through this? I'm still trying to understand how the program should work.

Is This A Good Question/Topic? 0
  • +

Replies To: Java Clock Program

#2 Hurshai  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 51
  • Joined: 14-September 08

Re: Java Clock Program

Posted 19 January 2009 - 07:49 PM

http://java.sun.com/...util/Timer.html

Browse through that. Once you know how to use the timer class this should be pretty easy for you to figure out.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Clock Program

Posted 19 January 2009 - 08:32 PM

System.currentTimeMillis();

returns in a long the current system time in milliseconds
this milliseconds are the number of milliseconds seconds since Jan 1st 1970 00:00:00.00
a lot of method will let you play/convert these milliseconds

all that to say, do not use a double but a long
Was This Post Helpful? 0
  • +
  • -

#4 undergroundthry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 24-June 08

Re: Java Clock Program

Posted 19 January 2009 - 09:02 PM

Here's what I've done so far in Java code:

public class Clock{
	
	private int hour; // 0-23 hours
	private int minute; // 0-59 minutes
	private int second; // 0-59 seconds
	private double grain; // 0-3600 seconds

public Clock (int h, int m, int s) // set the clock to midnight, 0:0:0;  creating the clock
{
	hour = ( (h >= 0 && < 23) ? h : 0;
	minute = ( (m >=0 && <59) ? m : 0;
	second = ( (s >= 0 && <59) ? s : 0;
}

	Clock c = new Clock ();

public void Tick // how the clock works

	if ( second > 59)
{
	minute++;
}
	else
		if ( minute >= 3600 )
{
	hour++;
}

	if ( hour >=24)
{
	hour == 0;
}

}



I'm not sure if this is complete. I just read your replies, so thank you, I will look into those. I'm using Deitel and Deitel's Java: How to Program 7th ed. as a reference and the Java tutorials on Sun.

What I was trying to do in my attempt was to create a clock and set it equal to zero, essentially midnight. Then I created a code to tell the program how the clock should move. I'm still not sure how to incorporate precision into the program. I think I'm missing a counter as well to keep track of the time.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Clock Program

Posted 19 January 2009 - 09:37 PM

Good start
but a few corrections

public class Clock{
	
	private int hour; // 0-23 hours
	private int minute; // 0-59 minutes
	private int second; // 0-59 seconds
	private double grain; // 0-3600 seconds

public Clock (int h, int m, int s) // set the clock to midnight, 0:0:0;  creating the clock
{
	hour = ( (h >= 0 && < 23) ? h : 0;
	minute = ( (m >=0 && <59) ? m : 0;
	second = ( (s >= 0 && <59) ? s : 0;
}

// outside a method...	Clock c = new Clock ();

public void Tick // how the clock works

	if ( second > 59)
{
	minute += second / 60;
                second %= 60;
                
}

// not an else you might have switch the minutes over 60 in last statements 	else
	if ( minute > 59 )      // 59 not 3600
{
	hour += minute / 60;
           minute %= 60;
}

	if ( hour >=24)
{
	hour == 0;
}

}

Was This Post Helpful? 1
  • +
  • -

#6 undergroundthry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 24-June 08

Re: Java Clock Program

Posted 25 January 2009 - 07:16 PM

I need to implement a method to calculate and determine the angle the hour hand makes with the minute hand, but I'm not sure how to create such a method. I determined that the hour hand moves a certain amount of degrees per minute, but I don't know how to input an angle into the program. Any suggestions?
Was This Post Helpful? 0
  • +
  • -

#7 undergroundthry  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 24-June 08

Re: Java Clock Program

Posted 25 January 2009 - 10:30 PM

Can somebody help me here? I've changed the word precision to grain.

The program won't run and gets this "fatal" error.


public class Clock {

	private int hour; // 0-23 hours
	private int minute; // 0-59 minutes
	private double second; // 0-59 seconds
	private double grain; // 0-3600 seconds
	
	public Clock (int h, int m, double s, double g)
	{
		if (h < 0 || h > 23)
		minute = ( (m >= 0 && h < 59) ? m : 0);
		second = ( (s >= 0.0 && h < 59) ? s : 0.0);
		grain = 0.0;
	
	}
	
	public void setHour(int h)
	{
		hour = h;
	}
	
	public int getHour()
	{
		return hour;
	}
	
	public void setMinute(int m)
	{
		minute = m;
	}
	
	public int getMinute()
	{
		return minute;
	}
	
	public void setSecond(double s)
	{
		second = s;
	}
	
	public double getSecond()
	{
		return second;
	}
	
	public void setGrain(double g)
	{
		grain = g;
	}
	
	public double getGrain()
	{
		return grain;
	}
	
	public void tick () // how the clock works
	{
		second += grain;
		
		if (second >= 60.0) // Method for outputting seconds
		{
			minute += (int)(second/60);
			second %= 60;
		}
		
		if (minute > 59) // Method for outputting minutes
		{
			hour += minute/60;
			minute %= 60;
		}
		
		if (hour >= 24) // Method for outputting hours
		{
			hour = 0;
		}
	}
	
	public String toString() //Output time
	{
		return String.format("%02d:%02d:%02d", hour, minute, second);
	}
	
	public void AngleMeasure ()
	{
		throw new IllegalArgumentException();
	}
}




And here's the tester. The purpose of my program is so the program can determine when the hour hand and minute hand form a straight line:


public class ClockTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Clock c = new Clock(2,42,38,400);
		
		System.out.printf("The time is:", c.getHour(), c.getMinute(), c.getSecond(), c.getGrain());
	}

}


This post has been edited by undergroundthry: 26 January 2009 - 12:15 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1