9 Replies - 10139 Views - Last Post: 18 September 2009 - 06:42 PM Rate Topic: -----

#1 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Converting 24-hour clock to 12-hour am/pm clock

Posted 17 September 2009 - 01:59 PM

My teacher throw me a curveball. He made us do a 24-hour military style clock and then at the last minute told us to change it to a 12-hour am/pm clock. This thing is due tomorrow and he gave us very little time to figure this out. I am pretty stumped on how to do this because of the rolling over to the am/pm and also figuring out how to make 12:30am in the style of "12:30am" and not "00:30". Any help on how to even start, go about, or how to do it entirely would be appreciated. Here's my two classes...

ClockDisplay class

public class ClockDisplay
{
	private NumberDisplay hours;
	private NumberDisplay minutes;
	private String displayString;
	
	public ClockDisplay()
	{
		hours = new NumberDisplay(12);
		minutes = new NumberDisplay(60);
		updateDisplay();
	}

	public ClockDisplay(int hour, int minute)
	{
		hours = new NumberDisplay(12);
		minutes = new NumberDisplay(60);
		setTime(hour, minute);
	}

	public void timeTick()
	{
		minutes.increment();
		if(minutes.getValue() == 0) {
			hours.increment();
		}
		updateDisplay();
	}

	public void setTime(int hour, int minute)
	{
		hours.setValue(hour);
		minutes.setValue(minute);
		updateDisplay();
	}
	
	public String getTime()
	{
		return displayString;
	}

	private void updateDisplay()
	{
		displayString = hours.getDisplayValue() + ":" + 
						minutes.getDisplayValue();
	}
}



NumberDisplay class

public class NumberDisplay
{
	private int limit;
	private int value;

	public NumberDisplay(int rollOverLimit)
	{
		limit = rollOverLimit;
		value = 0;
	}

	public int getValue()
	{
		return value;
	}

	public String getDisplayValue()
	{
		if(value < 10) {
			return "0" + value;
		}
		else {
			return "" + value;
		}
	}

	public void setValue(int replacementValue)
	{
		if((replacementValue >= 0) && (replacementValue < limit)) {
			value = replacementValue;
		}
	}

	public void increment()
	{
		value = (value + 1) % limit;
	}
}


This post has been edited by mattlyons: 17 September 2009 - 02:00 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Converting 24-hour clock to 12-hour am/pm clock

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9038
  • View blog
  • Posts: 33,525
  • Joined: 27-December 08

Re: Converting 24-hour clock to 12-hour am/pm clock

Posted 17 September 2009 - 02:49 PM

Your problem lies in the increment- it works fine for minutes but not for hours. For hours, you want to do this:

if(value > limit) value %= limit;



This way, it only modifies the value to keep it from going over 12:59.
Was This Post Helpful? 0
  • +
  • -

#3 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: Converting 24-hour clock to 12-hour am/pm clock

Posted 17 September 2009 - 11:19 PM

that didn't really work at all. any more help from others? running out of time here :(
Was This Post Helpful? 0
  • +
  • -

#4 thezboe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 18-September 09

Re: Converting 24-hour clock to 12-hour am/pm clock

Posted 18 September 2009 - 09:30 AM

You'll need to use a different method for incrementing your hours and minutes. Because there can be 0 minutes, your method is fine. But because hours goes from 12 to 1, the same method will not work. That is why you're having the problem of 0:00 when it hits 12:00.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9038
  • View blog
  • Posts: 33,525
  • Joined: 27-December 08

Re: Converting 24-hour clock to 12-hour am/pm clock

Posted 18 September 2009 - 09:34 AM

For a more OO approach, have an abstract super class NumberDisplay with increment() as the only abstract method. Then create subclasses for minutes and hours, and simply override the abstract method.
Was This Post Helpful? 0
  • +
  • -

#6 thezboe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 18-September 09

Re: Converting 24-hour clock to 12-hour am/pm clock

Posted 18 September 2009 - 09:38 AM

View Postmacosxnerd101, on 18 Sep, 2009 - 08:34 AM, said:

For a more OO approach, have an abstract super class NumberDisplay with increment() as the only abstract method. Then create subclasses for minutes and hours, and simply override the abstract method.


Agreed, but sort of overkill for this project don't you think?
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9038
  • View blog
  • Posts: 33,525
  • Joined: 27-December 08

Re: Converting 24-hour clock to 12-hour am/pm clock

Posted 18 September 2009 - 09:42 AM

Not really, no. It's easier to type the inherant methods to both in the super-class, and type only specific method in each subclass than it is to create two classes with many of the same methods. That's what OOP is all about- divide & conquer, and reusability of code.
Was This Post Helpful? 0
  • +
  • -

#8 thezboe  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 18-September 09

Re: Converting 24-hour clock to 12-hour am/pm clock

Posted 18 September 2009 - 09:48 AM

View Postmacosxnerd101, on 18 Sep, 2009 - 08:42 AM, said:

Not really, no. It's easier to type the inherant methods to both in the super-class, and type only specific method in each subclass than it is to create two classes with many of the same methods. That's what OOP is all about- divide & conquer, and reusability of code.


I agree with you that the end goal of the code should be as you said, I was just allowing the possibility that the OP maybe hasn't reached that point in his/her class yet. Nonetheless, we've got both bases covered here.
Was This Post Helpful? 0
  • +
  • -

#9 NeoTifa  Icon User is offline

  • ¿Dónde están mis pantalones?
  • member icon





Reputation: 1970
  • View blog
  • Posts: 14,482
  • Joined: 24-September 08

Re: Converting 24-hour clock to 12-hour am/pm clock

Posted 18 September 2009 - 11:12 AM

//pseudocode

//for incrementing
if (seconds >= 60) {
seconds = seconds - 60;
minute++;
}
//same applies to minutes and hours

//for changing military into am/pm

method militaryToCiviilan(time xxxx) {
if (time <= 1259) {
civilianTime = time;
if(time >=1200) { //internal loop, as noon is considered pm
amOrPm ="pm";
}
else {
amOrPm = "am";
}
}

else if (time >= 1300) { //assuming you already error checked to make sure it wasn't greater than 2359
civilianTime = time - 1200;
amOrPm = "pm";
}
}



Hope that helped a little.

This post has been edited by NeoTifa: 18 September 2009 - 11:14 AM

Was This Post Helpful? 1

#10 pbl  Icon User is offline

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

Reputation: 8027
  • View blog
  • Posts: 31,161
  • Joined: 06-March 08

Re: Converting 24-hour clock to 12-hour am/pm clock

Posted 18 September 2009 - 06:42 PM

Don't really remember if 00h10 is 12h10 AM or PM
but the easiest way is to make an array on which hours are which transalted in 12 hours system

int[] AmPmHours = {12, 1, 2, 3, 4 5, 6, 7, 8, 9, 10, 11, 12, 1, 2 , 3, 4, 5, 6, 7, 8, 9, 10, 11};

then you can just say

int hoursToDisplay = AmPmHours[realHour % 24];
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1