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 [Link];
import [Link];
import [Link];
public class AppendColor {
public static void main(String[] args) {
List<String> colors = new ArrayList<>([Link]("Red", "Green", "Black",
"White", "Pink"));
// Append "Yellow" to the list of colors
[Link]("Yellow");
// Print the list of colors
[Link]("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 [Link];
import [Link];
import [Link];
public class IterateColors {
public static void main(String[] args) {
List<String> colors = new ArrayList<>([Link]("Red", "Orange", "Yellow",
"Green", "Blue", "Indigo", "Violet"));
// Iterate through the list of colors
for (String color : colors) {
[Link](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 [Link];
import [Link];
public class Main {
public static void main(String[] args) {
List<String> flowers = new ArrayList<>();
[Link]("Daisy");
[Link]("Marigold");
[Link]("Rose");
[Link]("Original list of flowers: " + flowers);
[Link](0, "Carnation");
[Link]("Sunflower");
[Link]("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 [Link];
import [Link];
public class Main {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>();
[Link](2);
[Link](4);
[Link](5);
[Link](6);
[Link](8);
[Link](9);
[Link]("Set of Numbers: " + numbers);
[Link](n -> n % 2 != 0);
[Link]("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 [Link];
import [Link];
import [Link];
public class CheckStudent {
public static void main(String[] args) {
List<String> students = new ArrayList<>([Link]("Anna", "Brice", "Mary",
"Penelope", "Rose"));
// Check if a particular student exists in the list
String studentToCheck = "Mary";
if ([Link](studentToCheck)) {
[Link]("List of Names: " + students);
[Link]("We had " + studentToCheck + " on the list.");
} else {
[Link]("List of Names: " + students);
[Link]("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.