Skip to content

Ateeb Salam #87

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
65 changes: 57 additions & 8 deletions src/main/java/com/booleanuk/core/Exercise.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,16 @@ 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
*/

public int getSecondNumber(){
ArrayList<Integer> arr = getFavouriteNumbers();
int tall=0;
for (int i = 0; i<arr.size(); i++){
if (i==1){
tall= arr.get(i);
}
}
return tall;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be simplified, eg:

public int getSecondNumber() {
    ArrayList<Integer> arr = getFavouriteNumbers();
    if (arr.size() > 1) {
        return arr.get(1);
    } else {
        // Handle the case where the array does not have a second element
        // This could be returning a default value or throwing an exception
        return 0; // or throw new IllegalStateException("List does not have enough elements");
    }
}



/*
Expand All @@ -56,15 +65,35 @@ public ArrayList<Integer> getFavouriteNumbers() {
https://www.programiz.com/java-programming/library/arraylist/replaceall
*/

@Override
public ArrayList<Integer> multiply(ArrayList<Integer> list, int multiplier) {
int temp=0;
int multi=0;
for (int i=0; i<list.size(); i++){
temp = list.get(i);
multi = temp * multiplier;
list.set(i,multi);
}

return list;
}

/*
/*
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
*/


@Override
public boolean isEmpty(ArrayList<String> list) {
boolean empty;
if (list.isEmpty()){
empty=true;
}else{
empty=false;
}
return empty;
}

/*
TODO: 4. Create a method named addIngredient that accepts two parameters in this order:
Expand All @@ -73,7 +102,11 @@ public ArrayList<Integer> getFavouriteNumbers() {
The method must add the second parameter into the list provided and then return the list
*/


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

/*
TODO: 5. Create a method named removeIngredient that accepts two parameters in this order:
Expand All @@ -82,15 +115,31 @@ public ArrayList<Integer> getFavouriteNumbers() {
The method must remove the second parameter from the list and then return the list
*/

@Override
public ArrayList<String> removeIngredient(ArrayList<String> list, String ingredient) {
for (int i = 0; i<list.size(); i++){
if (list.get(i).equals(ingredient)){
list.remove(i);
}
}
return 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
*/



@Override
public boolean containsIngredient(ArrayList<String> list, String ingredient) {
boolean exist=false;
for (int i = 0; i<list.size(); i++){
if (list.get(i).equals(ingredient)){
exist=true;
}
}
return exist;
}
}