Computers and Technology, 25.02.2020 22:31, dragonmaster837
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;
}
}
Answers: 1
Computers and Technology, 22.06.2019 06:30, westjayson69
Requirement types discussed during software development include functional and color scheme nonfunctional and code style constraint and nonfunctional fashionable and functional.
Answers: 2
Computers and Technology, 22.06.2019 18:30, yayamcneal05
Which cultural aspect does this type of song best portray? a german polka dance
Answers: 1
Computers and Technology, 22.06.2019 20:00, hannahliebl2000
Need asap write a short paper describing the history and differences between six sigma, waterfall, agile, and scrum models. understanding these models can give you a good idea of how diverse and interesting it development projects can be. describe what the rationale for them is and describe their key features. describe the history behind their development. at least 400 words
Answers: 1
I am working specifically with java on BlueJ. I was given code for a 24-hour clock (00:00-23:59) and...
Geography, 25.11.2021 08:00
History, 25.11.2021 08:00
English, 25.11.2021 08:00
English, 25.11.2021 08:00