Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objects First With Java BluJ -- 6th Edition -- Exercise 3.38 Challenge exercise

ID: 3836682 • Letter: O

Question

Objects First With Java BluJ -- 6th Edition -- Exercise 3.38

Challenge exercise change the clock from a 24-hour clock to a 12-hour clock. Be careful: This is not as easy as it might seem. In a 12-hour clock, the hours after midnight and after noon are not shown as 00:30, but as 12:30. Thus, the minute display shows values from 0 to 59, while the hour display shows values from 1 to 12!

Using the below java code, in BlueJ, edit the code to change the 24-hour clock to a 12-hour clock using the below restrictions.

Extra restrictions:

You cannot modify in any way anything in the NumberDisplay.java file. This includes adding anything. Do not make any changes to this file.

You cannot modify in any way the getTime method in the ClockDisplay.java file

You cannot modify in any way the updateDisplay method in the ClockDisplay.java file

You cannot add any additional fields to ClockDisplay.java.

You cannot add any additional methods to ClockDisplay.java.

You cannot add any additional classes to the project.

Other than those restrictions you can solve the project in any way that works.

You can make any modifications you need to to these methods in ClockDisplay.java:

either of the ClockDisplay constructors

timeTick

setTime

Source files from project:


public class ClockDisplay
{
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString;   
  
/**
* 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()
{
displayString = hours.getDisplayValue() + ":" +
minutes.getDisplayValue();
}
}


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

/**
* Constructor for objects of class NumberDisplay.
* Set the limit at which the display rolls over.
*/
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;
}
}

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

Explanation / Answer

Modified code for ClockDisplay which will always print timings in 12 hrs format I have added main method at the end to test any sample test cases Examples , I: 13:01 , O: 1:01, I:0:01, O: 12:01 public class ClockDisplay { private NumberDisplay hours; private NumberDisplay minutes; private String displayString; /** * 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(); /* converting it to 12 hrs format if hour > 12*/ if(hours.getValue()>12) { hours.setValue(hours.getValue() - 12); } if(hours.getValue()==0){ hours.setValue(12); } } updateDisplay(); } /** * Set the time of the display to the specified hour and * minute. */ public void setTime(int hour, int minute) { /* converting it to 12 hrs format if hour > 12 */ if(hour > 12){ hour = hour%12; } if(hour==0){ hour =12; } 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() { displayString = hours.getDisplayValue() + ":" + minutes.getDisplayValue(); } public static void main(String args[]){ ClockDisplay cd = new ClockDisplay(12,0); System.out.println(cd.getTime()); } }