Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to convert the string argument to an integer in the test method.
JUnit
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; public class ConversionTest { @Test void testStringToInt() { String input = "123"; int number = Integer.[1](input); assertEquals(123, number); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using valueOf returns an Integer object, not a primitive int.
Using toString is for converting to String, not from String.
intValue is a method on Integer objects, not static.
✗ Incorrect
The method Integer.parseInt converts a String to a primitive int, which is needed here for the assertion.
2fill in blank
mediumComplete the code to convert the string argument to a double in the test method.
JUnit
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; public class ConversionTest { @Test void testStringToDouble() { String input = "3.14"; double value = Double.[1](input); assertEquals(3.14, value); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using valueOf returns a Double object, not a primitive double.
toString is unrelated to conversion from String.
doubleValue is an instance method, not static.
✗ Incorrect
Double.parseDouble converts a String to a primitive double, which matches the variable type.
3fill in blank
hardFix the error in the code to correctly convert the string argument to a boolean.
JUnit
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; public class ConversionTest { @Test void testStringToBoolean() { String input = "true"; boolean flag = Boolean.[1](input); assertTrue(flag); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using valueOf returns a Boolean object, not primitive boolean.
booleanValue is an instance method, not static.
toString converts to String, not from String.
✗ Incorrect
Boolean.parseBoolean converts a String to a primitive boolean, which is needed here for assertTrue.
4fill in blank
hardFill both blanks to convert a string to an Integer object and then get its int value.
JUnit
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; public class ConversionTest { @Test void testStringToIntValue() { String input = "456"; Integer obj = Integer.[1](input); int number = obj.[2](); assertEquals(456, number); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using parseInt returns primitive int, not Integer object.
Using toString is unrelated to conversion.
Using parseInt then calling intValue causes error because int is primitive.
✗ Incorrect
Integer.valueOf converts String to Integer object; intValue gets primitive int from Integer.
5fill in blank
hardFill all three blanks to convert a string to a Double object, get its double value, and assert equality.
JUnit
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; public class ConversionTest { @Test void testStringToDoubleValue() { String input = "9.81"; Double obj = Double.[1](input); double value = obj.[2](); assertEquals([3], value); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using parseDouble returns primitive double, not Double object.
Using toString is unrelated.
Using wrong literal in assertEquals causes test failure.
✗ Incorrect
Double.valueOf converts String to Double object; doubleValue gets primitive double; assertEquals compares with 9.81.