Skip to content
Auf dieser Seite

Durch zwei teilbar

Category: Java
Time: 10 min
Difficulty: Easy

Implementieren Sie eine Java-Methode mit dem Namen isDivisibleByTwo, die eine Ganzzahl als Parameter erwartet und überprüft, ob diese Ganzzahl durch 2 teilbar ist. Die Methode soll einen boolean-Wert zurückgeben, der true ist, wenn die Zahl durch 2 teilbar ist, andernfalls soll er false sein.

Zusätzlich soll ein Test erstellt werden welcher überprüft, ob die Implementierung auch fehlerfrei funktioniert.

Implementierung

java
package net.bitelligence.training.exercise.modulo.divisible_by_two;

public class DivisibleByTwo {

    private static final int DIVISOR = 2;

    /**
     * A number divided by two, which doesn't result in a rest of zero
     * is divisable by two.2
     * @param number dividend
     * @return boolean is number diivisable by two
     */

     public static boolean isDivisibleByTwo(int number) {
        // ...
        return false;
     }

}
java
package net.bitelligence.training.exercise.modulo.divisible_by_two;

public class DivisibleByTwo {

    @Test
    public void shouldBeDivisibleByTwo() {
        int two = 2;
        assertTrue(DivisibleByTwo.isDivisibleByTwo(two));
    }

    @Test
    public void shouldNotBeDivisibleByTwo() {
        int three = 3;
        assertFalse(DivisibleByTwo.isDivisibleByTwo(three));
    }

}
java
package net.bitelligence.training.exercise.modulo.divisible_by_two;

public class DivisibleByTwo {

    private static final int DIVISOR = 2;

    /**
     * A number divided by two, which doesn't result in a rest of zero
     * is divisable by two.2
     * @param number dividend
     * @return boolean is number diivisable by two
     */

     public static boolean isDivisibleByTwo(int number) {
        return number % DIVISOR == 0;
     }

}

85296 Rohrbach | Hofmarkstraße 24 | Handelsregister: HRB 9299 | Registergericht: Amtsgericht Ingolstadt | Umsatzsteuer-Identifikationsnummer: DE328161891 | Vertreten: Markus Keck