subject

I am working specifically with java on BlueJ. I was given code for a 24-hour clock (00:00-23:59) and was tasked with changing it to a 12-hour clock with "am" or "pm" displayed next to the time. I am using the method of having the clock function internally as a 24-hour clock, but having the display string show the 12 hour version (ie: showing 4:30pm instead of 16:30). I am very close but I am running into two problems: 1. When the clock goes up from 11:59pm it displays 12:00pm rather than 12:00am; 2.When "0" is entered for hours, I want it to show "12".

The code I have so far is included below

Thank you in advance!

For ClockDisplay Class:

public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString; // simulates the actual display

/**
* Constructor for ClockDisplay objects. This constructor
* creates a new clock set at 00:00.
*/
public ClockDisplay()
{
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
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(24);
minutes = new NumberDisplay(60);
setTime(hour, minute);
}

/**
* 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();
}
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()
{
String pmam = ampm(hours. getValue());
displayString = hours. twelveHour() + ":" +
minutes. getDisplayValue()+" "+pmam;
}

private String ampm(int value)
{
if((value < 12) || (value == 24) || (value == 0))
{
return "am";
}
else
{
return "pm";
}
}
}

For the NumberDisplay Class:

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

/**
* Constructor for objects of class Display
*/
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;
}
}

public String twelveHour()
{
if(value < 10)
{
return "0" + value;
}
else if(value > 12)
{
value = value - 12;
return "" + value;
}
else if(value == 0)
{
return "12";
}
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;
}
}

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 23.06.2019 10:50
Your friend kayla is starting her own business and asks you whether she should set it up as a p2p network or as a client-server network. list three questions you might ask to kayla decide which network to use and how her answers to those questions would affect your recommendation.
Answers: 2
question
Computers and Technology, 24.06.2019 01:00
What are two ways to access the options for scaling and page orientation? click the home tab, then click alignment, or click the file tab. click the file tab, then click print, or click the page layout tab. click the page layout tab, or click the review tab. click the review tab, or click the home tab?
Answers: 2
question
Computers and Technology, 24.06.2019 08:00
Can someone work out the answer as it comes up in one of my computer science exams and i don't understand the cryptovariables
Answers: 1
question
Computers and Technology, 24.06.2019 18:30
Jacking is a crime that takes place when a hacker misdirects url to a different site. the link itself looks safe, but the user is directed to an unsafe page
Answers: 1
You know the right answer?
I am working specifically with java on BlueJ. I was given code for a 24-hour clock (00:00-23:59) and...
Questions
question
English, 01.04.2021 19:10
question
Mathematics, 01.04.2021 19:10
question
Mathematics, 01.04.2021 19:10
Questions on the website: 13722359