The goal of this exercise is to familiarise ourselves with:
This repo contains starter code for the Java Primitives exercise. After opening your repo in your IDE, ensure you've navigated to this directory in your terminal.
For this section, open the src/main/java/com/cbfacademy/strings/
directory. It contains a Java program with three methods to implement: concatenate
, areEqual
and format
.
Using the String Java documentation as a guide, implement the methods in App.java
. In each method, replace throw new RuntimeException("Not implemented")
with your code.
Implement the concatenate
method in such a way that it creates a new String by concatenating the provided parameters.
public static String concatenate(String word1, String word2, String word3) {
// TODO: Write code that concatenates the input parameters and returns the result
throw new RuntimeException("Not implemented");
}
Implement the areEqual
method, which determines whether the two provided parameters are equal strings. The equality check should be case-sensitive (i.e. hello != HELLO)
public static Boolean areEqual(String word1, String word2) {
// TODO: Write code to determine whether the input parameters are equal strings
throw new RuntimeException("Not implemented");
}
Implement the format
method, which returns a formatted string containing the provided parameters. The price value should be displayed with a pound (£) symbol and two decimal places.
public static String format(String item, int quantity, double price) {
// TODO: Write code to return a string formatted as follows: "Item: [item]. Price: £[amount]. Quantity: [quantity]"
throw new RuntimeException("Not implemented");
}
Example
String lineItem = format("Apple iPhone 15 Pro", 47, 1199.99);
System.out.println(lineItem); // Output: Item: Apple iPhone 15 Pro. Price: £1199.99. Quantity: 47
We've created a unit test suite to verify your solution. To verify that your code works as expected, ensure that you are in the root directory of this repository in your terminal, then run the following command:
./mvnw clean test -Dtest=com.cbfacademy.strings.AppTest
The -Dtest=com.cbfacademy.strings.AppTest
flag sets the specific test suite to be executed. If you want to run all the unit tests for the session, simply omit that option.
Your implementation is correct when all tests pass:
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] ├─ String Exercises - 0.072s
[INFO] │ ├─ ✔ areEqual() method returns equality of two strings[1] Java, Java, true - 0.018s
[INFO] │ ├─ ✔ areEqual() method returns equality of two strings[2] HTML, HTML, true - 0.001s
[INFO] │ ├─ ✔ areEqual() method returns equality of two strings[3] beta, beta, true - 0.001s
[INFO] │ ├─ ✔ areEqual() method returns equality of two strings[4] camel-case, camel_case, false - 0.001s
[INFO] │ ├─ ✔ areEqual() method returns equality of two strings[5] WET, wet, false - 0.001s
[INFO] │ ├─ ✔ areEqual() method returns equality of two strings[6] dry, DRY, false - 0s
[INFO] │ ├─ ✔ concatenate() method returns correctly concatenated string[1] Red, Green, Blue, RedGreenBlue - 0.005s
[INFO] │ ├─ ✔ concatenate() method returns correctly concatenated string[2] one, two, three, onetwothree - 0s
[INFO] │ ├─ ✔ concatenate() method returns correctly concatenated string[3] QUICK, BROWN, FOX, QUICKBROWNFOX - 0s
[INFO] │ ├─ ✔ concatenate() method returns correctly concatenated string[4] www., google., com, www.google.com - 0s
[INFO] │ ├─ ✔ concatenate() method returns correctly concatenated string[5] Intro , to , Java, Intro to Java - 0s
[INFO] │ ├─ ✔ format() method returns formatted string[1] Laptop, 27, 1999.99 - 0.008s
[INFO] │ ├─ ✔ format() method returns formatted string[2] Mobile phone, 200, 999.99 - 0.001s
[INFO] │ ├─ ✔ format() method returns formatted string[3] Tablet, 85, 1199.99 - 0s
[INFO] │ ├─ ✔ format() method returns formatted string[4] Charger, 467, 29.99 - 0s
[INFO] │ └─ ✔ format() method returns formatted string[5] USB cable, 883, 8.99 - 0s
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 16, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.098 s
[INFO] Finished at: 2023-10-26T02:34:16+01:00
[INFO] ------------------------------------------------------------------------
For this section, open the src/main/java/com/cbfacademy/operators/
directory. It contains a Java program with a number of methods to implement.
Using the Java Operators documentation as a guide, implement the methods in App.java
. In each method, replace throw new RuntimeException("Not implemented")
with your code.
Implement the methods in App.java
to add, subtract and multiply decimal numbers. In each method, replace throw new RuntimeException("Not implemented")
with your code:
public static double add(double operand1, double operand2)
public static double subtract(double operand1, double operand2)
public static double multiply(double operand1, double operand2)
Implement the methods in App.java
to compare equality and evaluate the largest and smallest given decimal numbers. In each method, replace throw new RuntimeException("Not implemented")
with your code:
public static Boolean areEqual(double operand1, double operand2)
public static Boolean isLessThan(double operand1, double operand2)
public static Boolean isMoreThan(double operand1, double operand2)
What do the following expressions evaluate to:
int number1 = 12;
int number2 = 12;
System.out.println(number1++);
System.out.println(++number2);
Why is that?
To verify that your code works as expected, run the following command in your terminal:
./mvnw clean test -Dtest=com.cbfacademy.operators.AppTest
For this section, open the src/main/java/com/cbfacademy/arrays/
directory. It contains a Java program with several methods to implement:
public static Integer[] createIntegerArray()
: Create and return an array of 8Integer
elements.public static Float[] createFloatArray()
: Create and return an array of 12Float
elements.public static Double[] createDoubleArray()
: Create and return an array of 5Double
elements.public static Boolean[] createBooleanArray()
: Create and return an array of 6Boolean
elements.public static void printFifthElements()
: Print the fifth element of each array to the screen.
In each method, replace throw new RuntimeException("Not implemented")
with your code.
Run the following command in your terminal:
./mvnw clean test -Dtest=com.cbfacademy.arrays.AppTest
Write a method that initialises a matrix with the 4 arrays created above. (Hint: use the Object
type.)
createMatrix
: Initialise and return a matrix (2D array) containing the 4 arrays above as rows.
Note: No unit tests are provided for the stretch task.