Badua, Saimon P.
DCIT 25
BSIT – 2B
Asynchronous Activity #4
1) Write a java program that appends a specified color to the end of the list of colors. The
output should look like this:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class AppendColor {
public static void main(String[] args) {
List<String> colors = new ArrayList<>(Arrays.asList("Red", "Green", "Black",
"White", "Pink"));
// Append "Yellow" to the list of colors
colors.add("Yellow");
// Print the list of colors
System.out.println("Colors: " + colors);
}
}
Output:
Colors: [Red, Green, Black, White, Pink, Yellow]
2) Write a java program that iterates through all the colors in the list of colors. The output
should look like this:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class IterateColors {
public static void main(String[] args) {
List<String> colors = new ArrayList<>(Arrays.asList("Red", "Orange", "Yellow",
"Green", "Blue", "Indigo", "Violet"));
// Iterate through the list of colors
for (String color : colors) {
System.out.println(color);
}
}
}
Output:
Red
Orange
Yellow
Green
Blue
Indigo
Violet
3) Write a Java program that inserts elements into the list of flowers at the first and in last
position. The output should look like this:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> flowers = new ArrayList<>();
flowers.add("Daisy");
flowers.add("Marigold");
flowers.add("Rose");
System.out.println("Original list of flowers: " + flowers);
flowers.add(0, "Carnation");
flowers.add("Sunflower");
System.out.println("Finalized list of flowers: " + flowers);
}
}
Output:
Original list of flowers: [Daisy, Marigold, Rose]
Finalized list of flowers: [Carnation, Daisy, Marigold, Rose, Sunflower]
4) Write a Java program that removes the elements that are not even numbers from the list
of numbers (Your code should be flexible). The output should look like this:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>();
numbers.add(2);
numbers.add(4);
numbers.add(5);
numbers.add(6);
numbers.add(8);
numbers.add(9);
System.out.println("Set of Numbers: " + numbers);
numbers.removeIf(n -> n % 2 != 0);
System.out.println("Set of Even Numbers: " + numbers);
}
}
Output:
Set of Numbers: [2, 4, 5, 6, 8, 9]
Set of Even Numbers: [2, 4, 6, 8]
5) Write a Java program that checks if a particular student exists from a list of students
(Your code should be flexible, the output should be open for changes). The output
should look like this:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CheckStudent {
public static void main(String[] args) {
List<String> students = new ArrayList<>(Arrays.asList("Anna", "Brice", "Mary",
"Penelope", "Rose"));
// Check if a particular student exists in the list
String studentToCheck = "Mary";
if (students.contains(studentToCheck)) {
System.out.println("List of Names: " + students);
System.out.println("We had " + studentToCheck + " on the list.");
} else {
System.out.println("List of Names: " + students);
System.out.println("We can't find " + studentToCheck + " on the list.");
}
}
}
Output:
List of Names: [Anna, Brice, Mary, Penelope, Rose]
We had Mary on the list.
To change the student to be checked, simply change the value of the studentToCheck
variable to another name. For example:
String studentToCheck = "Danny";
This way, the output will change according to the provided name.
List of Names: [Anna, Brice, Mary, Penelope, Rose]
We can't find Danny on the list.