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 01:30
How will you cite information that is common knowledge in your research paper?
Answers: 1
question
Computers and Technology, 22.06.2019 04:50
Which are steps taken to diagnose a computer problem? a) reproducing the problem and using error codes b) reproducing the problem and troubleshooting c) using error codes and troubleshooting d) using error codes and stepping functions
Answers: 1
question
Computers and Technology, 23.06.2019 00:20
Ihave been given the number of guns per 100, and the total firearm-related deaths per 100,000. i have to find the actual number of guns per country and actual number of gun-related deaths. if somebody could show me how to do 1 question, i can finish the rest, i am just confused. tia
Answers: 3
question
Computers and Technology, 23.06.2019 06:00
Which statement is true of web-based social media? a.they allow consumers to interact with and update content. b.they cannot be updated easily, as compared to print media. c.they are expensive to produce and maintain, as compared to print and television. d.they can exist independent of the internet.
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
Mathematics, 10.11.2020 19:00
question
Mathematics, 10.11.2020 19:00
question
Mathematics, 10.11.2020 19:00
Questions on the website: 13722361