Skip to content

cbfacademy/java-exercises-primitives

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Working With Primitives

Java Language JUnit5 Testing Framework Maven Dependency Manager

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.

📌 Strings

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.

String Concatenation

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");
}

String Comparison

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");
}

String Formatting

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

✅ Verify Your Implementation

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.

Test Results

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] ------------------------------------------------------------------------

📌 Operators

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.

Arithmetic Operators

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)

Relational Operators

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)

Unary Operators

What do the following expressions evaluate to:

int number1 = 12;
int number2 = 12;

System.out.println(number1++);
System.out.println(++number2);

Why is that?

✅ Verify Your Implementation

To verify that your code works as expected, run the following command in your terminal:

./mvnw clean test -Dtest=com.cbfacademy.operators.AppTest

📌 Arrays

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 8 Integer elements.
  • public static Float[] createFloatArray(): Create and return an array of 12 Float elements.
  • public static Double[] createDoubleArray(): Create and return an array of 5 Double elements.
  • public static Boolean[] createBooleanArray(): Create and return an array of 6 Boolean 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.

✅ Verify Your Implementation

Run the following command in your terminal:

./mvnw clean test -Dtest=com.cbfacademy.arrays.AppTest

Stretch Task

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages