0% found this document useful (0 votes)
4 views4 pages

Simplified_Basic_Java_OOP_Tasks

The document outlines five basic Java OOP tasks involving lists. Tasks include adding unique numbers to a list, finding a word in a list, counting the length of words, removing a word from a list, and checking if a list is empty, each accompanied by example code and test cases. These tasks demonstrate fundamental operations on lists using Java's collection framework.

Uploaded by

Judith Nelson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Simplified_Basic_Java_OOP_Tasks

The document outlines five basic Java OOP tasks involving lists. Tasks include adding unique numbers to a list, finding a word in a list, counting the length of words, removing a word from a list, and checking if a list is empty, each accompanied by example code and test cases. These tasks demonstrate fundamental operations on lists using Java's collection framework.

Uploaded by

Judith Nelson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Simplified Basic Java OOP Tasks

Task 1: Add Numbers to a List


Create a method that adds an integer to an instance variable list of integers.
Ensure the list only contains unique values by checking before adding the number.

Example:
```java
import java.util.*;

public class NumberList {


private List<Integer> list;

public NumberList() {
list = new ArrayList<>();
}

public List<Integer> addUniqueNumber(int number) {


if (!list.contains(number)) {
list.add(number);
}
return list;
}
}
```
Test Case:
```java
NumberList numberList = new NumberList();
System.out.println(numberList.addUniqueNumber(4)); // Output: [4]
System.out.println(numberList.addUniqueNumber(2)); // Output: [4, 2]
System.out.println(numberList.addUniqueNumber(4)); // Output: [4, 2]
```

Task 2: Find a Word in a List


Write a method that checks if a specific word exists in an instance variable list of strings.

Example:
```java
import java.util.*;
public class WordList {
private List<String> list;

public WordList(List<String> initialWords) {


list = new ArrayList<>(initialWords);
}

public boolean findWord(String word) {


return list.contains(word);
}
}
```
Test Case:
```java
WordList wordList = new WordList(Arrays.asList("apple", "banana", "cherry"));
System.out.println(wordList.findWord("banana")); // Output: true
System.out.println(wordList.findWord("grape")); // Output: false
```

Task 3: Count the Length of Each Word


Create a method that returns a map with each string in an instance variable list of words as
the key and its length as the value.

Example:
```java
import java.util.*;

public class WordLength {


private List<String> words;

public WordLength(List<String> words) {


this.words = words;
}

public Map<String, Integer> wordLength() {


Map<String, Integer> map = new HashMap<>();
for (String word : words) {
map.put(word, word.length());
}
return map;
}
}
```
Test Case:
```java
WordLength wordLength = new WordLength(Arrays.asList("apple", "banana", "cherry"));
System.out.println(wordLength.wordLength()); // Output: {apple=5, banana=6, cherry=6}
```

Task 4: Remove a Word from a List


Write a method that removes a word from an instance variable list of strings if it exists.

Example:
```java
import java.util.*;

public class WordList {


private List<String> list;

public WordList(List<String> words) {


list = new ArrayList<>(words);
}

public List<String> removeWord(String word) {


list.remove(word);
return list;
}
}
```
Test Case:
```java
WordList wordList = new WordList(Arrays.asList("apple", "banana", "cherry"));
System.out.println(wordList.removeWord("banana")); // Output: [apple, cherry]
```

Task 5: Check If List Is Empty


Write a method that checks if an instance variable list of integers is empty and returns true
or false.

Example:
```java
import java.util.*;

public class NumberList {


private List<Integer> list;

public NumberList() {
list = new ArrayList<>();
}

public boolean isListEmpty() {


return list.isEmpty();
}
}
```
Test Case:
```java
NumberList numberList = new NumberList();
System.out.println(numberList.isListEmpty()); // Output: true

numberList.addUniqueNumber(1);
System.out.println(numberList.isListEmpty()); // Output: false
```

You might also like