How can I roll-over in java?

I am coding for public void addMinutes right now. like 1570minutes is 2 hours 10 minutes and 65minutes is 1 hour 5minutes, I have to increase it by 1 hour every 60 minutes. Help me please.

public class AlarmClockController {

public AlarmClockController() {
}
public void addHours(int hours) {
    if (hours < 0) {
        throw new IllegalArgumentException();
    }
    for(int i = 0; i < hours; i++) {
        this.theClock.incrementHour();
    }
    
}

public void addMinutes(int minutes) {
    if (minutes < 0) {
        throw new IllegalArgumentException();
    }
    for(int i = 0; i < minutes; i++) {
        if (minutes < 60) {
            this.theClock.incrementMinutes();
        }
        else { 

        }
    }
    
public AlarmClock getTheClock() {
    return this.theClock;
}

}

public class AlarmClock {

private int hour;
private int minutes;


public AlarmClock() {
    this.hour = 0;
    this.minutes = 0;
}

AND getSeconds()==0

public AlarmClock(int hour, int minutes) {
    if (hour < 0 || hour > 23) {
        throw new IllegalArgumentException("hour should be between 0 and 23");
    }
    
    if (minutes < 0 || minutes > 59) {
        throw new IllegalArgumentException("minutes shoulbe be between 0 and 59");
    }
    
    this.hour = hour;
    this.minutes = minutes;
}

public int getHour() {
    return this.hour;
}

public int getMinutes() {
    return this.minutes;
}

public void incrementHour() {
    this.hour = (this.hour + 1) % 24;
}

public void incrementMinutes() {
    this.minutes = (this.minutes + 1) % 60;
}

}

This is for testing

public class TestAddMinutes {

@Test
public void testShouldNotAllowMinutesLessThanZero() {
    // Arrange: create a controller
    AlarmClockController controller = new AlarmClockController();
    
    // Act & Assert: call addMinutes with a negative value
    // and assert it throws an exception
    assertThrows(IllegalArgumentException.class, () -> {
        controller.addMinutes(-1);
    });
}

@Test
public void testShouldAddZeroMinutes() {
    // Arrange: create a controller
    AlarmClockController controller = new AlarmClockController();
    
    // Act: call the method with an appropriate parameter
    controller.addMinutes(0);
    
    // Assert: that the clock has the expected number of minutes
    AlarmClock clock = controller.getTheClock();
    assertEquals(0, clock.getHour());
    assertEquals(0, clock.getMinutes());
}

@Test
public void testShouldAddOneMinute() {
    // Arrange: create a controller
    AlarmClockController controller = new AlarmClockController();
    
    // Act: call the method with an appropriate parameter
    controller.addMinutes(1);
    
    // Assert: that the clock has the expected number of minutes
    AlarmClock clock = controller.getTheClock();
    assertEquals(0, clock.getHour());
    assertEquals(1, clock.getMinutes());
}

@Test
public void testShouldAddSeveralMinutes() {
    // Arrange: create a controller
    AlarmClockController controller = new AlarmClockController();
    
    // Act: call the method with an appropriate parameter
    controller.addMinutes(10);
    
    // Assert: that the clock has the expected number of minutes
    AlarmClock clock = controller.getTheClock();
    assertEquals(0, clock.getHour());
    assertEquals(10, clock.getMinutes());
}

@Test
public void testShouldAddSeveralMinutesWithMinutesRollover() {
    // Arrange: create a controller
    AlarmClockController controller = new AlarmClockController();
    
    // Act: call the method with an appropriate parameter
    controller.addMinutes(65);
    
    // Assert: that the clock has the expected number of minutes
    AlarmClock clock = controller.getTheClock();
    assertEquals(1, clock.getHour());
    assertEquals(5, clock.getMinutes());
}

@Test
public void testShouldAddSeveralMinutesWithHoursRollover() {
    // Arrange: create a controller
    AlarmClockController controller = new 
    AlarmClockController();
    
    // Act: call the method with an appropriate parameter
    controller.addMinutes(26*60 + 10);
    
    // Assert: that the clock has the expected number of minutes
    AlarmClock clock = controller.getTheClock();
    assertEquals(2, clock.getHour());
    assertEquals(10, clock.getMinutes());
}

} fffff

Answer

The code inside addMinutes should be:

if (minutes < 0) {
    throw new IllegalArgumentException();
}
int hours = minutes/60;
for(int i = 0; i<hours; i++) {
    this.theClock.incrementHour();
}
minutes %= 60; // remaining minutes
for(int i = 0; i < minutes; i++) {
   this.theClock.incrementMinutes();
}

Also, I would recommend not to increment the hours and minutes one by one. Replace the increment methods using the below template:

public void incrementHours(int value) {
    this.hour = (this.hour + value) % 24; 
}

Leave a Reply

Your email address will not be published. Required fields are marked *