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, 22.06.2019 10:30
How can a user open a blank presentation? 1.on the file menu, click new, and then click recent templates 2.on the file menu, click new, and then click blank presentation 3. on the view menu, click templates, and then click recent templates 4. on the view menu, click samples, and then click blank presentation
Answers: 1
question
Computers and Technology, 23.06.2019 21:10
Asample of 200 rom computer chips was selected on each of 30 consecutive days, and the number of nonconforming chips on each day was as follows: 8, 19, 27, 17, 38, 18, 4, 27, 9, 22, 30, 17, 14, 23, 15, 14, 12, 20, 13, 18, 14, 20, 9, 27, 30, 13, 10, 19, 12, 26. construct a p chart and examine it for any out-of-control points. (round your answers to four decimal places.)
Answers: 2
question
Computers and Technology, 24.06.2019 01:00
Answer these and get 40 points and brainliest
Answers: 1
question
Computers and Technology, 24.06.2019 01:30
How would you cite different books by the same author on the works cited page? moore, jack h. folk songs and ballads. salem: poetry press, 1999. print. moore, jack h. ballads in poetry – a critical review. dallas: garden books, 1962. print. moore, jack h. folk songs and ballads. salem: poetry press, 1999. print. –––. ballads in poetry – a critical review. dallas: garden books, 1962. print. moore, jack h. ballads in poetry – a critical review. dallas: garden books, 1962. print. moore, jack h. folk songs and ballads. salem: poetry press, 1999. print. moore, jack h. ballads in poetry – a critical review. dallas: garden books, 1962. print. –––. folk songs and ballads. salem: poetry press, 1999. print.
Answers: 2
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
Computers and Technology, 10.11.2020 21:40
question
Mathematics, 10.11.2020 21:40
question
Arts, 10.11.2020 21:40
question
Mathematics, 10.11.2020 21:40
question
Arts, 10.11.2020 21:40
question
Mathematics, 10.11.2020 21:40
question
Physics, 10.11.2020 21:40
Questions on the website: 13722367