Skip to content

Herman Gard Stornes #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 81 additions & 56 deletions src/main/java/com/booleanuk/core/Exercise.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,37 @@
import com.booleanuk.helpers.ExerciseBase;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Exercise extends ExerciseBase {
/*
A List is like an array but provides a much easier interface to the items it stores, for example:
- A list doesn't need to have a predefined size, we can add and remove as many things as memory allows
- We can more easily run looping operations on lists
- A list is an interface that has multiple implementations depending on the data structure we want,
e.g. ArrayList, LinkedList, Stack
- This exercise will focus on using the ArrayList
* A List is like an array but provides a much easier interface to the items it
* stores, for example:
* - A list doesn't need to have a predefined size, we can add and remove as
* many things as memory allows
* - We can more easily run looping operations on lists
* - A list is an interface that has multiple implementations depending on the
* data structure we want, e.g. ArrayList, LinkedList, Stack
* - This exercise will focus on using the ArrayList
*/

/*
Take some time to read and understand the method below.
- It creates an instance of an ArrayList that will contain Integer types
We define the data type that will be stored in a list using angled brackets:
ArrayList<Datatype>
We can store any data type in a list, even our own custom classes.
ArrayList<String>
ArrayList<Boolean>
ArrayList<Exercise>

- It adds a few numbers to the list
- It outputs the list that now contains a few numbers

Documentation: https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/ArrayList.html
* Take some time to read and understand the method below.
* - It creates an instance of an ArrayList that will contain Integer types
* We define the data type that will be stored in a list using angled brackets:
* ArrayList<Datatype> We can store any data type in a list, even our own custom
* classes.
* ArrayList<String>
* ArrayList<Boolean>
* ArrayList<Exercise>
*
* - It adds a few numbers to the list
* - It outputs the list that now contains a few numbers
*
* Documentation:
* https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/
* ArrayList.html
*/
public ArrayList<Integer> getFavouriteNumbers() {
ArrayList<Integer> list = new ArrayList<>();
Expand All @@ -40,57 +46,76 @@ public ArrayList<Integer> getFavouriteNumbers() {
}

/*
TODO: 1. Create a method named getSecondNumber that returns a whole number. It must return the
second number contained in the list that is returned from getFavouriteNumbers
* TODO: 1. Create a method named getSecondNumber that returns a whole number.
* It must return the
* second number contained in the list that is returned from getFavouriteNumbers
*/


public int getSecondNumber() {
return this.getFavouriteNumbers().get(1);
}

/*
TODO: 2. Create a method named multiply that accepts two parameters in this order:
- A list of whole numbers
- A whole number
The method must multiply each number in the provided list by the number provided in the second
parameter, and then return the updated list.
Use the ArrayList's replaceAll method to iterate through the ArrayList and replace each value with its double
https://www.programiz.com/java-programming/library/arraylist/replaceall
* TODO: 2. Create a method named multiply that accepts two parameters in this
* order:
* - A list of whole numbers
* - A whole number
* The method must multiply each number in the provided list by the number
* provided in the second
* parameter, and then return the updated list.
* Use the ArrayList's replaceAll method to iterate through the ArrayList and
* replace each value with its double
* https://www.programiz.com/java-programming/library/arraylist/replaceall
*/


public ArrayList<Integer> multiply(ArrayList<Integer> list, int factor) {
// Just doing it this way for fun, even if its not the recommended way
return list.stream().map(x -> x * factor).collect(Collectors.toCollection(ArrayList::new));
}

/*
TODO: 3. Create a method named isEmpty that accepts one parameter:
- A list of strings
The method must return a boolean that indicates whether the provided list is empty or not
* TODO: 3. Create a method named isEmpty that accepts one parameter:
* - A list of strings
* The method must return a boolean that indicates whether the provided list is
* empty or not
*/


public boolean isEmpty(ArrayList<String> strings) {
return strings.isEmpty();
}

/*
TODO: 4. Create a method named addIngredient that accepts two parameters in this order:
- A list of strings
- A string
The method must add the second parameter into the list provided and then return the list
* TODO: 4. Create a method named addIngredient that accepts two parameters in
* this order:
* - A list of strings
* - A string
* The method must add the second parameter into the list provided and then
* return the list
*/


public ArrayList<String> addIngredient(ArrayList<String> ingredients, String ingredient) {
ingredients.add(ingredient);
return ingredients;
}

/*
TODO: 5. Create a method named removeIngredient that accepts two parameters in this order:
- A list of strings
- A string
The method must remove the second parameter from the list and then return the list
* TODO: 5. Create a method named removeIngredient that accepts two parameters
* in this order:
* - A list of strings
* - A string
* The method must remove the second parameter from the list and then return the
* list
*/


public ArrayList<String> removeIngredient(ArrayList<String> ingredients, String ingredient) {
ingredients.remove(ingredient);
return ingredients;
}

/*
TODO: 6. Create a method named containsIngredient that accepts two parameters in this order:
- A list of strings
- A string
The method must return a boolean that indicates whether the second parameter exists in the provided list
* TODO: 6. Create a method named containsIngredient that accepts two parameters
* in this order:
* - A list of strings
* - A string
* The method must return a boolean that indicates whether the second parameter
* exists in the provided list
*/



public boolean containsIngredient(ArrayList<String> ingredients, String ingredient) {
return ingredients.contains(ingredient);
}
}