0% found this document useful (0 votes)
114 views2 pages

Laboratory Activity 2: Code

The document describes two Java programming exercises: 1. The first creates an ArrayList of colors, adds elements, inserts an element at the first position, updates a specific element, searches for an element, and joins two ArrayLists. 2. The second prompts the user for the number of elements for an integer array, stores user input in the array, prints the array, and then reverses the order of elements in the array by swapping them without using a second array. It prints the reversed array.

Uploaded by

Coolet
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)
114 views2 pages

Laboratory Activity 2: Code

The document describes two Java programming exercises: 1. The first creates an ArrayList of colors, adds elements, inserts an element at the first position, updates a specific element, searches for an element, and joins two ArrayLists. 2. The second prompts the user for the number of elements for an integer array, stores user input in the array, prints the array, and then reverses the order of elements in the array by swapping them without using a second array. It prints the reversed array.

Uploaded by

Coolet
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/ 2

Laboratory Activity 2

A. Laboratory Exercise. Write a Java program given the following specifications:


Create an arraylist, add some colours (string) and print out the collection.
Insert an element into the arraylist at the first position.
Update specific array element by given element.
Search an element in an arraylist.
Join two arraylists.
Code:
package newActivity;
import java.util.ArrayList;
public class LabActivity {
public static void main(String[] args) {
ArrayList<String> Arrlist_Strings = new ArrayList<String>();
ArrayList<String> Arrlist_Strings2 = new ArrayList<String>();
System.out.println("1. Enter the Collection of colours: ");
Arrlist_Strings.add("White");
Arrlist_Strings.add("Blue");
Arrlist_Strings.add("Yellow");
Arrlist_Strings.add("Green");
Arrlist_Strings.add("Red");
System.out.println(Arrlist_Strings);
System.out.println("\n2. Insert element into the array list: Maroon");
Arrlist_Strings.add(0, "Maroon");
System.out.println("Array List After Inserting in the 1st index: "+Arrlist_Strings);
Arrlist_Strings.set(4, "Black");
System.out.println("\n3. Update specific array element: Black");
System.out.println("Array List After Update: "+Arrlist_Strings);
System.out.println("\n4. Search an element in an arraylist: Yellow");
int index = Arrlist_Strings.indexOf("Yellow");
if(index == -1){
System.out.println("The element Yellow is not in the arraylist ");
}else
System.out.println("The element Yellow is in the arraylist at index "+index);
Arrlist_Strings2.add("Violet");
Arrlist_Strings2.add("Browm");
Arrlist_Strings2.add("Grey");
Arrlist_Strings2.add("Orange");
Arrlist_Strings2.add("Indigo");
Arrlist_Strings.addAll(Arrlist_Strings2);
System.out.println("\n5.Join two arraylist:\n" +Arrlist_Strings);}}
B. Write a program that prompts the user for an integer, then asks the user to enter that many
values. Store these values in an array and print the array. Then reverse the array elements so that the
first element becomes the last element, the second element becomes the second to last element,
and so on, with the old last element now first. Do not just reverse the order in which they are printed;
actually change the way they are stored in the array. Do not create a second array; just rearrange the
elements within the array you have. (Hint: Swap elements that need to change places.) When the
elements have been reversed, print the array again.

Code:
package newActivity;
import java.util.ArrayList;
import java.util.Scanner;
public class LabActivity2 {
public static void main(String[] args) {
int a;
Scanner scan = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
a = scan.nextInt();
System.out.println("\nEnter the elements of the Array: ");
int[] array = new int[a];
for(int c = 0; c<a; c++){
array[c] = scan.nextInt();
}
System.out.println("\nArray elements are: ");
for(int c = 0; c<a; c++){
System.out.println(array[c]);
}
System.out.println("\nArray in Reverse order: ");
for(int c = (a-1); c>0;c--){
System.out.println(array[c] + " ");
}
}
}

You might also like