diff --git a/Doc/Sd1/LanguageFundamentals/languageFundamentals.xml b/Doc/Sd1/LanguageFundamentals/languageFundamentals.xml index ecea90536b8e95d6a8ec03a1481f569325ebe953..2058ae36bfcd194e5c08723d85918f2e355be635 100644 --- a/Doc/Sd1/LanguageFundamentals/languageFundamentals.xml +++ b/Doc/Sd1/LanguageFundamentals/languageFundamentals.xml @@ -5488,7 +5488,7 @@ _____________________________________ </qandaset> <qandaset defaultlabel="qanda" xml:id="sw1QandaByteOverflow"> - <title>Strange things happen</title> + <title>Strange things with operator <code>++</code></title> <qandadiv> <qandaentry> @@ -5538,10 +5538,9 @@ System.out.println("New value=" + a);</programlisting> <para>A <code language="java">byte</code> variable ranges from -128 to +127. The <code language="java">++</code> operator is being defined as the binary addition of 1 on - machine level discarding a possible carryover.Thus - incrementing 127 by 1 yields -128 due to <code - language="java">byte</code> using 2-complement - representation:</para> + machine level discarding a possible carryover. Thus + incrementing 127 by 1 yields -128 due to 2-complement <code + language="java">byte</code> representation:</para> <informaltable border="1"> <tr> diff --git a/Klausuren/Sd1/2023summer/Exam/src/test/java/de/hdm_stuttgart/mi/sd1/task1/B_IrrigationTest.java b/Klausuren/Sd1/2023summer/Exam/src/test/java/de/hdm_stuttgart/mi/sd1/task1/B_IrrigationTest.java index efb8d9e35ea585c52c16b62ced833328b9e1b70f..8f9fc1597437e9eb306aca3e63b928eabd1284ee 100644 --- a/Klausuren/Sd1/2023summer/Exam/src/test/java/de/hdm_stuttgart/mi/sd1/task1/B_IrrigationTest.java +++ b/Klausuren/Sd1/2023summer/Exam/src/test/java/de/hdm_stuttgart/mi/sd1/task1/B_IrrigationTest.java @@ -7,74 +7,54 @@ import org.junit.Test; import static de.hdm_stuttgart.mi.sd1.task1.B_Irrigation.irrigationIsActive; public class B_IrrigationTest extends ExaminationTestDefaults { - @Test - @Marking(points = 13) - public void testIrrigation () { + static private void test(final boolean expectedIrrigationState, + final int fromHour, final int toHour, + final int fromMinute, + final int toMinute) { - boolean expectedIrrigationState = false; // 0:00 till 4:59 - for (int hour = 0; hour <= 4; hour++) { - for (int minute = 0; minute <= 59; minute++) { - final String errMmsg = getErrorMessage(expectedIrrigationState, hour, minute); - Assert.assertEquals(errMmsg, expectedIrrigationState, irrigationIsActive(hour, minute)); + for (int hour = fromHour; hour <= toHour; hour++) { + for (int minute = fromMinute; minute <= toMinute; minute++) { + Assert.assertEquals("At " + hour + ":" + minute + " Irrigation is supposed to be " + + (expectedIrrigationState ? "active" : "inactive"), + expectedIrrigationState, + irrigationIsActive(hour, minute)); } } + } - expectedIrrigationState = true; // 5:00 till 5:59 - for (int minute = 0; minute <= 59; minute++) { - final String errMmsg = getErrorMessage(expectedIrrigationState, 5, minute); - Assert.assertEquals(errMmsg, expectedIrrigationState, irrigationIsActive(5, minute)); - } - - expectedIrrigationState = true; // 6:00 till 6:30 - for (int minute = 0; minute <= 30; minute++) { - final String errMmsg = getErrorMessage(expectedIrrigationState, 6, minute); - Assert.assertEquals(errMmsg, expectedIrrigationState, irrigationIsActive(6, minute)); - } - - expectedIrrigationState = false; // 6:31 till 6:59 - for (int minute = 31; minute <= 59; minute++) { - final String errMmsg = getErrorMessage(expectedIrrigationState, 6, minute); - Assert.assertEquals(errMmsg, expectedIrrigationState, irrigationIsActive(6, minute)); - } - - expectedIrrigationState = false; // 7:00 till 20:59 - for (int hour = 7; hour <= 20; hour++) { - for (int minute = 0; minute < 60; minute++) { - final String errMmsg = getErrorMessage(expectedIrrigationState, hour, minute); - Assert.assertEquals(errMmsg, expectedIrrigationState, irrigationIsActive(hour, minute)); - } - } - - expectedIrrigationState = false; // 21:00 till 21:29 - for (int minute = 0; minute <= 29; minute++) { - final String errMmsg = getErrorMessage(expectedIrrigationState, 21, minute); - Assert.assertEquals(errMmsg, expectedIrrigationState, irrigationIsActive(21, minute)); - } - - expectedIrrigationState = true; // 21:30 till 21:59 - for (int minute = 30; minute <= 59; minute++) { - final String errMmsg = getErrorMessage(expectedIrrigationState, 21, minute); - Assert.assertEquals(errMmsg, expectedIrrigationState, irrigationIsActive(21, minute)); - } + static private void test(final boolean expectedIrrigationState, + final int fromHour, final int toHour) + { + test(expectedIrrigationState, fromHour, toHour, 0, 59); + } + static private void test(final boolean expectedIrrigationState, + final int hour) + { + test(expectedIrrigationState, hour, hour, 0, 59); + } - expectedIrrigationState = true; // 22:00 till 22:30 - for (int minute = 0; minute <= 30; minute++) { - final String errMmsg = getErrorMessage(expectedIrrigationState, 22, minute); - Assert.assertEquals(errMmsg, expectedIrrigationState, irrigationIsActive(22, minute)); - } + static private void test(final boolean expectedIrrigationState, + final int hour, + final int fromMinute, + final int toMinute) + { + test(expectedIrrigationState, hour, hour, fromMinute, toMinute); + } - // 22:31 till 22:59 - expectedIrrigationState = false; // 22:31 till 22:59 - for (int minute = 31; minute <= 59; minute++) { - final String errMmsg = getErrorMessage(expectedIrrigationState, 22, minute); - Assert.assertEquals(errMmsg, expectedIrrigationState, irrigationIsActive(22, minute)); - } + @Test + @Marking(points = 13) + public void testIrrigation () { - expectedIrrigationState = false; // 23:00 till 23:59 - for (int minute = 0; minute <= 59; minute++) { - final String errMmsg = getErrorMessage(expectedIrrigationState, 23, minute); - Assert.assertEquals(errMmsg, expectedIrrigationState, irrigationIsActive(23, minute)); - } + test(false, 0, 4); // 0:00 till 4:59 + test(true, 5); // 5:00 till 5:59 + test(true, 6, 0, 30); // 6:00 till 6:30 + test(false, 6, 31, 59); // 6:31 till 6:59 + test(false, 7, 20); // 7:00 till 20:59 + test(false, 21, 0, 29); // 21:00 till 21:29 + test(true, 21, 30, 59); // 21:30 till 21:59 + test(true, 22, 0, 30); // 22:00 till 22:30 + test(false, 22, 31, 59); // 22:31 till 22:59 + test(false, 23); // 23:00 till 23:59 } private String getErrorMessage(final boolean expectedIrrigationState, final int hour, final int minute) { diff --git a/Klausuren/Sd1/2023summer/Solve/src/main/java/de/hdm_stuttgart/mi/sd1/task1/C_Switch.java b/Klausuren/Sd1/2023summer/Solve/src/main/java/de/hdm_stuttgart/mi/sd1/task1/C_Switch.java index f4f6ed75ac1d8e41d2f6e99cbeaae4f9d589340e..d1518a2b5f3c904072ca2e9f8e1b3efdb64d7183 100644 --- a/Klausuren/Sd1/2023summer/Solve/src/main/java/de/hdm_stuttgart/mi/sd1/task1/C_Switch.java +++ b/Klausuren/Sd1/2023summer/Solve/src/main/java/de/hdm_stuttgart/mi/sd1/task1/C_Switch.java @@ -122,12 +122,10 @@ public class C_Switch { commaCount++; } } - if (1 != commaCount || // Not exactly one comma - ',' == stringCharacter[0] || // Comma at beginning - ',' == stringCharacter[agencyStyleName.length() -1]// comma at end + if (1 == commaCount & // Exactly one comma + ',' != stringCharacter[0] & // Comma not at beginning + ',' != stringCharacter[agencyStyleName.length() -1]// Comma not at end ) { - return null; - } else { final String[] nameComponents = agencyStyleName.split(","); if (nameComponents[0].trim().equals("") || // Surname just whitespace nameComponents[1].trim().equals("")) // First name just whitespace @@ -136,6 +134,8 @@ public class C_Switch { } else { return nameComponents[1].trim().concat(" ").concat(nameComponents[0].trim()); } + } else { // No or multiple commas or at beginning or end + return null; } } } diff --git a/Klausuren/Sd1/2023summer/Solve/src/test/java/de/hdm_stuttgart/mi/sd1/task1/B_IrrigationTest.java b/Klausuren/Sd1/2023summer/Solve/src/test/java/de/hdm_stuttgart/mi/sd1/task1/B_IrrigationTest.java index efb8d9e35ea585c52c16b62ced833328b9e1b70f..8f9fc1597437e9eb306aca3e63b928eabd1284ee 100644 --- a/Klausuren/Sd1/2023summer/Solve/src/test/java/de/hdm_stuttgart/mi/sd1/task1/B_IrrigationTest.java +++ b/Klausuren/Sd1/2023summer/Solve/src/test/java/de/hdm_stuttgart/mi/sd1/task1/B_IrrigationTest.java @@ -7,74 +7,54 @@ import org.junit.Test; import static de.hdm_stuttgart.mi.sd1.task1.B_Irrigation.irrigationIsActive; public class B_IrrigationTest extends ExaminationTestDefaults { - @Test - @Marking(points = 13) - public void testIrrigation () { + static private void test(final boolean expectedIrrigationState, + final int fromHour, final int toHour, + final int fromMinute, + final int toMinute) { - boolean expectedIrrigationState = false; // 0:00 till 4:59 - for (int hour = 0; hour <= 4; hour++) { - for (int minute = 0; minute <= 59; minute++) { - final String errMmsg = getErrorMessage(expectedIrrigationState, hour, minute); - Assert.assertEquals(errMmsg, expectedIrrigationState, irrigationIsActive(hour, minute)); + for (int hour = fromHour; hour <= toHour; hour++) { + for (int minute = fromMinute; minute <= toMinute; minute++) { + Assert.assertEquals("At " + hour + ":" + minute + " Irrigation is supposed to be " + + (expectedIrrigationState ? "active" : "inactive"), + expectedIrrigationState, + irrigationIsActive(hour, minute)); } } + } - expectedIrrigationState = true; // 5:00 till 5:59 - for (int minute = 0; minute <= 59; minute++) { - final String errMmsg = getErrorMessage(expectedIrrigationState, 5, minute); - Assert.assertEquals(errMmsg, expectedIrrigationState, irrigationIsActive(5, minute)); - } - - expectedIrrigationState = true; // 6:00 till 6:30 - for (int minute = 0; minute <= 30; minute++) { - final String errMmsg = getErrorMessage(expectedIrrigationState, 6, minute); - Assert.assertEquals(errMmsg, expectedIrrigationState, irrigationIsActive(6, minute)); - } - - expectedIrrigationState = false; // 6:31 till 6:59 - for (int minute = 31; minute <= 59; minute++) { - final String errMmsg = getErrorMessage(expectedIrrigationState, 6, minute); - Assert.assertEquals(errMmsg, expectedIrrigationState, irrigationIsActive(6, minute)); - } - - expectedIrrigationState = false; // 7:00 till 20:59 - for (int hour = 7; hour <= 20; hour++) { - for (int minute = 0; minute < 60; minute++) { - final String errMmsg = getErrorMessage(expectedIrrigationState, hour, minute); - Assert.assertEquals(errMmsg, expectedIrrigationState, irrigationIsActive(hour, minute)); - } - } - - expectedIrrigationState = false; // 21:00 till 21:29 - for (int minute = 0; minute <= 29; minute++) { - final String errMmsg = getErrorMessage(expectedIrrigationState, 21, minute); - Assert.assertEquals(errMmsg, expectedIrrigationState, irrigationIsActive(21, minute)); - } - - expectedIrrigationState = true; // 21:30 till 21:59 - for (int minute = 30; minute <= 59; minute++) { - final String errMmsg = getErrorMessage(expectedIrrigationState, 21, minute); - Assert.assertEquals(errMmsg, expectedIrrigationState, irrigationIsActive(21, minute)); - } + static private void test(final boolean expectedIrrigationState, + final int fromHour, final int toHour) + { + test(expectedIrrigationState, fromHour, toHour, 0, 59); + } + static private void test(final boolean expectedIrrigationState, + final int hour) + { + test(expectedIrrigationState, hour, hour, 0, 59); + } - expectedIrrigationState = true; // 22:00 till 22:30 - for (int minute = 0; minute <= 30; minute++) { - final String errMmsg = getErrorMessage(expectedIrrigationState, 22, minute); - Assert.assertEquals(errMmsg, expectedIrrigationState, irrigationIsActive(22, minute)); - } + static private void test(final boolean expectedIrrigationState, + final int hour, + final int fromMinute, + final int toMinute) + { + test(expectedIrrigationState, hour, hour, fromMinute, toMinute); + } - // 22:31 till 22:59 - expectedIrrigationState = false; // 22:31 till 22:59 - for (int minute = 31; minute <= 59; minute++) { - final String errMmsg = getErrorMessage(expectedIrrigationState, 22, minute); - Assert.assertEquals(errMmsg, expectedIrrigationState, irrigationIsActive(22, minute)); - } + @Test + @Marking(points = 13) + public void testIrrigation () { - expectedIrrigationState = false; // 23:00 till 23:59 - for (int minute = 0; minute <= 59; minute++) { - final String errMmsg = getErrorMessage(expectedIrrigationState, 23, minute); - Assert.assertEquals(errMmsg, expectedIrrigationState, irrigationIsActive(23, minute)); - } + test(false, 0, 4); // 0:00 till 4:59 + test(true, 5); // 5:00 till 5:59 + test(true, 6, 0, 30); // 6:00 till 6:30 + test(false, 6, 31, 59); // 6:31 till 6:59 + test(false, 7, 20); // 7:00 till 20:59 + test(false, 21, 0, 29); // 21:00 till 21:29 + test(true, 21, 30, 59); // 21:30 till 21:59 + test(true, 22, 0, 30); // 22:00 till 22:30 + test(false, 22, 31, 59); // 22:31 till 22:59 + test(false, 23); // 23:00 till 23:59 } private String getErrorMessage(final boolean expectedIrrigationState, final int hour, final int minute) { diff --git a/Klausuren/Sd1/2023summer/Solve/src/test/java/de/hdm_stuttgart/mi/sd1/task1/C_SwitchTest.java b/Klausuren/Sd1/2023summer/Solve/src/test/java/de/hdm_stuttgart/mi/sd1/task1/C_SwitchTest.java index cbb173f1d3b080fa6bd5ed0b770d3d6c878de9a3..e77215e3ec0045c3fe1c5b4f3a1b056bfe4ece3b 100644 --- a/Klausuren/Sd1/2023summer/Solve/src/test/java/de/hdm_stuttgart/mi/sd1/task1/C_SwitchTest.java +++ b/Klausuren/Sd1/2023summer/Solve/src/test/java/de/hdm_stuttgart/mi/sd1/task1/C_SwitchTest.java @@ -14,6 +14,10 @@ import static de.hdm_stuttgart.mi.sd1.task1.C_Switch.switchFirstAndSurname; public class C_SwitchTest extends ExaminationTestDefaults { + static private void testNull(final String errorMessage, final String agencyFileName ) { + Assert.assertNull("Expecting null output: " + errorMessage, switchFirstAndSurname(agencyFileName)); + } + @Test @Marking(points = 2) public void test_050_null() { @@ -23,18 +27,18 @@ C_SwitchTest extends ExaminationTestDefaults { @Test @Marking(points = 2) public void test_100_error() { - Assert.assertNull(switchFirstAndSurname("")); - Assert.assertNull(switchFirstAndSurname(" ")); - Assert.assertNull(switchFirstAndSurname(" test ")); - Assert.assertNull(switchFirstAndSurname("Tucholski;Kurt")); - Assert.assertNull(switchFirstAndSurname("Macdonald Amy")); - Assert.assertNull(switchFirstAndSurname("Keynes, John, Maynard")); - Assert.assertNull(switchFirstAndSurname(",Hauser Kaspar")); - Assert.assertNull(switchFirstAndSurname("Hauser Kaspar,")); - Assert.assertNull(switchFirstAndSurname("Hauser, Kaspar,")); - Assert.assertNull(switchFirstAndSurname(",Hauser, Kaspar")); - Assert.assertNull(switchFirstAndSurname(",Hauser , Kaspar")); - Assert.assertNull(switchFirstAndSurname(",,,,,,,")); + testNull("Empty String", ""); + testNull("Just one space character", " "); + testNull("Missing comma", " test "); + testNull("Wrong separator ';' instead of comma", "Tucholski;Kurt"); + testNull("Missing comma", "Macdonald Amy"); + testNull("Two commas, just one being allowed", "Keynes, John, Maynard"); + testNull("No string before comma", ",Hauser Kaspar"); + testNull("No string after comma", "Hauser Kaspar,"); + testNull("Two commas, just one being allowed", "Hauser, Kaspar,"); + testNull("Two commas, just one being allowed", ",Hauser, Kaspar"); + testNull("Two commas, just one being allowed", ",Hauser , Kaspar"); + testNull("Multiple commas, just one being allowed", ",,,,,,,"); } @Test