You are on page 1of 10

SIES College of Management Studies FYMCA (Revised), Sem I,

Roll No: 25

Assignment 1

1) Write a Java Program to demonstrate various capabilities of interface ‘Collection’.


The program uses two color Arrays naming
a) colorsArray – contains elements as Violet, Indigo, Blue, Green, Yellow, Orange, Red
b) removeColorsArray – contain elements as Blue, Green, Orange
c) First make use of List Interface and create two ArrayLists naming colorList and
removeColorList.
d) Populate both the above ArrayList objects using the two mentioned Arrays.
e) Create a method naming ‘removeColors’ passing two Collection objects c1 and

c2 which accepts both the ArrayLists.


f) Make use of Iterator within the above ‘removeColors’ method to traverse

through the Collections and remove the appropriate color matched in the
another collection.

import java.util.*;
class Colors{
public static void main(String args[])
{

String colorsArray[]= new String[]


{"Violet","Indigo","Blue","Green","Yellow","Orange","Red"};

String removecolorsArray[]=new String[]{"Blue","Green","Orange"};


ArrayList<String>colorsList = new ArrayList<String>();
ArrayList<String>removecolorsList = new ArrayList<String>();
for(int i=0;i<colorsArray.length;i++)
{
colorsList.add(colorsArray[i]);
}
for(int i=0;i<removecolorsArray.length;i++)
{
removecolorsList.add(removecolorsArray[i]);
}

removeColors(colorsList,removecolorsList);
}

static void removeColors(ArrayList color1,ArrayList color2)


{
Iterator<String>iterat=color1.iterator();
while(iterat.hasNext())
{
if(color2.contains(iterat.next()))
Subject: MCAL12 Advanced Java Lab Academic Year First Half
2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25

{
iterat.remove();
}

}
System.out.println(color1);
}
}

2) Write a Java Program to create list and apply following operations:


a) add and display the following elements in the ArrayList object: Google, Apple, Amazon,
Facebook, Twitter, Oracle.
b) Write a Java program to update 3rd element in the above list by “Microsoft”.
c) Write a Java program to remove the last element from the list of objects
d) Write a Java program to check whether an element is available in the list

import java.util.ArrayList;

class Collection {

public static void main(String[] args) {


ArrayList<String> MNC = new ArrayList<String>();

MNC.add("Google");

MNC.add("Apple");

MNC.add("Amazon");

MNC.add("Facebook");

MNC.add("Twitter");

MNC.add("Oracle");

System.out.println("Array List Objects are: " +MNC);


System.out.println();
Subject: MCAL12 Advanced Java Lab Academic Year First Half
2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25

MNC.set(2,"Microsoft");

System.out.println("\n Updated Array List Objects are: " +MNC);


System.out.println();

MNC.remove(MNC.size()-1);

System.out.println("\n Updated Array List Objects are: " +MNC);


System.out.println();

System.out.println("\n Checking if Google is Present in List");

if(MNC.contains("Google"))

System.out.println("\n Element is Present");

else

System.out.println("\n Element is Missing");

3) Write a java program to add 1 to 20 numbers to ArrayList object. Print the following
outputs using foreach loop.
a) The 10 elements from 1 to 20.
b) Only even numbers
c) Only numbers which are perfect squares

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

public class Java1 {


public static void main(String[] args) {
Subject: MCAL12 Advanced Java Lab Academic Year First Half
2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25

ArrayList<Integer> list = new ArrayList<Integer>();

for(int i=1;i<=20;i++){
list.add(i);
}

System.out.println("The 10 elements from 1 to 20 are");

for(int n : list){
if(n<=10){
System.out.println(n);
}
}
System.out.println();
System.out.println("Only even numbers are");

for(int num : list){


if(num%2==0){
System.out.println(num);
}
}

System.out.println();
System.out.println("Only numbers which are perfect squares are");

for(int sq : list){
if((Math.sqrt(sq) - Math.floor(Math.sqrt(sq)) == 0)){
System.out.println(sq);
}
}

}
}

Subject: MCAL12 Advanced Java Lab Academic Year First Half


2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25

4) Write a java program to sort the elements in an ArrayList.

import java.util.ArrayList;

import java.util.Collections;

public class Java1_4 {

public static void main(String[] args) {

ArrayList<Integer> list = new ArrayList<Integer>();


Subject: MCAL12 Advanced Java Lab Academic Year First Half
2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25

list.add(5);

list.add(4);

list.add(9);

list.add(1);

list.add(0,10);

System.out.println("ArrayList before sorting is");

System.out.println(list);

System.out.println();

System.out.println("Sorted ArrayList is");

Collections.sort(list);

System.out.println(list);

5) Write a java program to reverse the elements in an ArrayList.

package Java1;

import java.util.ArrayList;

import java.util.Collections;

Subject: MCAL12 Advanced Java Lab Academic Year First Half


2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25

public class Java14 {

public static void main(String[] args) {

ArrayList<Integer> list = new ArrayList<Integer>();

list.add(5);

list.add(4);

list.add(9);

list.add(1);

list.add(0,10);

System.out.println("ArrayList is");

System.out.println(list);

System.out.println();

System.out.println("Reverse of ArrayList is");

Collections.reverse(list);

System.out.println(list);

Subject: MCAL12 Advanced Java Lab Academic Year First Half


2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25

6) Write a java program to perform the following operations using the LinkedList class:
a. Add elements A to G. Display the contents of LinkedList.
b. Remove element “B”. Remove element at index 3. Remove the first
element. Remove the last element. Display the contents of LinkedList.
c. Insert element “X” as the first element. Insert element “Z” as the last
element.
d. Find the element “E”. If it exists, display, “List contains the element 'E':
else display “List does not contain the element 'E'”.
e. Display the size of LinkedList.

f. Replace element 5 with “Y” and display the contents

import java.util.*;

public class Lists{

public static void main(String args[])

LinkedList<String> list=new LinkedList<String>();

list.add("A");

list.add("B");

list.add("C");

list.add("D");

list.add("E");

list.add("F");

list.add("G");

System.out.println(list);

list.remove("B");

System.out.println(list);

list.remove(3);

Subject: MCAL12 Advanced Java Lab Academic Year First Half


2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25

System.out.println(list);

list.removeFirst();

list.removeLast();

System.out.println(list);

list.add(0,"X");

list.add(4,"Z");

System.out.println(list);

if(list.contains("E"))

System.out.println("List contains the element 'E':");

else

System.out.println("List does notcontains the element 'E':");

System.out.println("LinkedList:" + list);

System.out.println("The size of the linked list is: " + list.size());

list.set(4,"Y");

System.out.println(list);

Subject: MCAL12 Advanced Java Lab Academic Year First Half


2022_23
Batch:2022_24
SIES College of Management Studies FYMCA (Revised), Sem I,
Roll No: 25

Subject: MCAL12 Advanced Java Lab Academic Year First Half


2022_23
Batch:2022_24

You might also like