0% found this document useful (0 votes)
9 views6 pages

DCIT25 Activity

hwh

Uploaded by

johnybravo0116
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)
9 views6 pages

DCIT25 Activity

hwh

Uploaded by

johnybravo0116
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/ 6

John Rafael M.

Lirio
BSCS 2B

1.Write a java program that appends a specified color to the end of the list of colors. The output should
look like this;
Colors: [Red, Green, Black, White, Pink, Yellow]

import java.util.LinkedList;

public class Append {


public static void main(String[] args) {
LinkedList<String> colors = new LinkedList<>();
colors.add("Red");
colors.add("Green");
colors.add("Black");
colors.add("White");
colors.add("Pink");

System.out.println("Colors: " + colors);


}
}
2.Write a java program that iterates through all the colors in the list of colors. The output should look
like this;
Red
Orange
Yellow
Green
Blue
Indigo
Violet

import java.util.LinkedList;
import java.util.Iterator;

public class Iterate {


public static void main(String[] args) {
LinkedList<String> colors = new LinkedList<>();
colors.add("Red");
colors.add("Orange");
colors.add("Yellow");
colors.add("Green");
colors.add("Blue");
colors.add("Indigo");
colors.add("Violet");

Iterator<String> iterator = colors.iterator();

while (iterator.hasNext()) {
String color = iterator.next();
System.out.println(color);
}
}
}
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;
Original list of flowers: [Daisy, Marigold, Rose]
Finalized list of flowers: [Carnation, Daisy, Marigold, Rose, Sunflower]

import java.util.LinkedList;

public class Insert {


public static void main(String[] args) {
LinkedList<String> flowers = new LinkedList<>();
flowers.add("Daisy");
flowers.add("Marigold");
flowers.add("Rose");

System.out.println("Original list of flowers: " + flowers);

flowers.addFirst("Carnation");
flowers.addLast("Sunflower");

System.out.println("Finalized list of flowers: " + flowers);


}
}
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;
Set of Numbers: [2, 4, 5, 6, 8, 9]
Set of Even Numbers: [2, 4, 6, 8]

import java.util.LinkedList;
import java.util.Iterator;

public class Remove {

public static void main(String[] args) {


LinkedList<Integer> numbers = new LinkedList<>();
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);

Iterator<Integer> it = numbers.iterator();
while (it.hasNext()) {
if (it.next() % 2 != 0) {
it.remove();
}
}

System.out.println("Set of Even Numbers: " + numbers);


}
}
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;
Output: List of Names: [Anna, Brice, Mary, Penelope, Rose]
We had Mary on the list.

import java.util.LinkedList;

public class Check {


public static void main(String[] args) {
LinkedList<String> students = new LinkedList<>();
students.add("Anna");
students.add("Brice");
students.add("Mary");
students.add("Penelope");
students.add("Rose");

System.out.println("List of Names: " + students);

String findStudent = "Mary";

if (students.contains(findStudent)) {
System.out.println("We had " + findStudent + " on the list.");
} else {
System.out.println("We can't find " + findStudent + " on the
list.");
}
}
}
Changed output: List of Names: [Anna, Brice, Mary, Penelope, Rose]
We cant find Danny on the list.

import java.util.LinkedList;

public class Check {


public static void main(String[] args) {
LinkedList<String> students = new LinkedList<>();
students.add("Anna");
students.add("Brice");
students.add("Mary");
students.add("Penelope");
students.add("Rose");

System.out.println("List of Names: " + students);

String findStudent = "Danny";

if (students.contains(findStudent)) {
System.out.println("We had " + findStudent + " on the list.");
} else {
System.out.println("We can't find " + findStudent + " on the list.");
}
}
}

You might also like