Making a 24 hour clock into a 12 hour clock, resetting to 1:00 not 00:00 without an if statement 0_o HELP?

Well we have this theoretical clock, It was 24 hours and we have to make it into a 12 hour clock for class. I figured out the am pm pretty easy with an if statement. However, our teacher said we would get no credit for the assignment if we used an if statement to make the clock reset to 1:00 and not 00:00. However he said we could only put code in the timeTick method. I have looked at this for four hours, and come to the conclusion that, because you are only making the hours reset it has to be conditional. Therefore impossible without an if statement.

However, I believe that my teacher is not stupid and I am just missing something could anyone help???? Here is the code for the ClockDisplay method, and below that the numberdisplay method. No making two different numberdisplay methods and making the value in numberdisplay four hours did not work. It works when u first start the clock but not after that.

public class ClockDisplay

{

private NumberDisplay hours;

private NumberDisplay minutes;

private String displayString; // simulates the actual display

private String ampm = " am";

/**

* Constructor for ClockDisplay objects. This constructor

* creates a new clock set at 00:00.

*/

public ClockDisplay()

{

hours = new NumberDisplay(13);

minutes = new NumberDisplay(60);

hours.setValue(1);

updateDisplay();

}

/**

* Constructor for ClockDisplay objects. This constructor

* creates a new clock set at the time specified by the

* parameters.

*/

public ClockDisplay(int hour, int minute)

{

hours = new NumberDisplay(13);

minutes = new NumberDisplay(60);

setTime(hour, minute);

hours.setValue(1);

}

/**

* This method should get called once every minute - it makes

* the clock display go one minute forward.

*/

public void timeTick()

{

minutes.increment();

if(minutes.getValue() == 0)

{ // it just rolled over!

hours.increment();

}

if(hours.getValue() == 12 && minutes.getValue() <= 0 && ampm == " am")

{

ampm = " pm";

}

else

{

if(hours.getValue() == 12 && minutes.getValue() <= 0 && ampm == " pm")

{

ampm = " am";

}

}

updateDisplay();

}

/**

* Set the time of the display to the specified hour and

* minute.

*/

public void setTime(int hour, int minute)

{

hours.setValue(hour);

minutes.setValue(minute);

updateDisplay();

}

/**

* Return the current time of this display in the format HH:MM.

*/

public String getTime()

{

return displayString;

}

/**

* Update the internal string that represents the display.

*/

private void updateDisplay()

{

displayString = hours.getDisplayValue() + ":" +

minutes.getDisplayValue() + ampm;

}

}

public class NumberDisplay

{

private int limit;

private int value;

/**

* Constructor for objects of class NumberDisplay.

* Set the limit at which the display rolls over.

*/

public NumberDisplay(int rollOverLimit)

{

limit = rollOverLimit;

value = 0;

}

/**

* Return the current value.

*/

public int getValue()

{

return value;

}

/**

* Return the display value (that is, the current value as a two-digit

* String. If the value is less than ten, it will be padded with a leading

* zero).

*/

public String getDisplayValue()

{

if(value < 10) {

return "0" + value;

}

else {

return "" + value;

}

}

/**

* Set the value of the display to the new specified value. If the new

* value is less than zero or over the limit, do nothing.

*/

public void setValue(int replacementValue)

{

if((replacementValue >= 0) && (replacementValue < limit)) {

value = replacementValue;

}

}

/**

* Increment the display value by one, rolling over to zero if the

* limit is reached.

*/

public void increment()

{

value = (value + 1) % limit;

}

}

Update:

Thank you a bunch for that help!!!! btw, i can use if statements for the ampm.

THANKS A TON AGAIN

2 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    First of all 00:00 is NOT 1:00, it is 12:00 am.

    If it is only 'if' statements that are not allowed, then you can use ( condition : result if true ? result if false ) construct.

    If all conditionals are disallowed then you'd need to do something more clever. Think of it this way:

    -Your current maximum is 24

    -Your desired maximum is 12

    -How much is currentHour / 12 ? (Hint: use ONLY the whole number part of the result)

    -How much is currentHour % 12?

    -what happens if you combine currentHour with results of division by 12 and modulo 12 in some way? (Hint: there is one multiplication and one addition involved in combining them - try it out with actual numbers to see what happens)

    You will also need to update your am/pm estimation process:

    -Make your ampm variable into an int (or byte) instead of String

    -Flip it between 0 and 1 by using the results of division from above. (Hint: 0 is for am, 1 is for pm)

    -In the updateDisplay function use a conditional to display am or pm according to the ampm variable value (see Hint above).

    Hope this helps. You should be able to figure this out now on your own. (If I just *gave* you the solution you probably won't be able to solve similar problems in the future ;-) )

    • Login to reply the answers
  • 4 years ago

    12

    • Login to reply the answers
Still have questions? Get your answers by asking now.