Computers and Technology

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;
}
}

answer
Answers: 1

Other questions on the subject: Computers and Technology

image
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
image
Computers and Technology, 22.06.2019 11:10, 17795
Look at the far left lane in the picture. explain what the red car is doing and what it needs to do to travel safely.
Answers: 2
image
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
image
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
Do you know the correct answer?
I am working specifically with java on BlueJ. I was given code for a 24-hour clock (00:00-23:59) and...

Questions in other subjects:

Konu
English, 25.11.2021 08:00