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

Vijay 5 Java

The document presents practical Java programming exercises by a student named Vijay Pawar, focusing on the use of arrays and vectors. It includes source code examples demonstrating array initialization, two-dimensional array addition, and various vector operations such as element addition, removal, and capacity management. The outputs of the programs are also provided, showcasing the results of the array and vector manipulations.
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)
30 views6 pages

Vijay 5 Java

The document presents practical Java programming exercises by a student named Vijay Pawar, focusing on the use of arrays and vectors. It includes source code examples demonstrating array initialization, two-dimensional array addition, and various vector operations such as element addition, removal, and capacity management. The outputs of the programs are also provided, showcasing the results of the array and vector manipulations.
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

Name: Vijay pawar

Class: SYCO
Roll No: 7
Subject: Java programming
Subject Code: 314317
Pratical Name: 5.write programs to demonstrate:
 Use of Array.
 Use of Vector.
---------------------------------------------------------------------------------------------------------------------------------------
Source code:
 Use of Array.

public class Array


{
public static void main(String[] args)
{
// Declare and initialize an array of integers
int[] numbers = {1, 2, 3, 4, 5};
// Print the elements of the array
System.out.println("Elements of the array:");
for (int i = 0; i < numbers.length; i++)
{
System.out.println(numbers[i]);
}
// Calculate the sum of the elements in the array
int sum = 0;
for (int i = 0; i < numbers.length; i++)
{
sum += numbers[i];
}
System.out.println("Sum of the elements: " + sum);
}
}

Output:-
Elements of the array:
1
2
3
4
5
Sum of the elements: 15

 Two dimensional array.

public class Addmatrix


{
public static void main(String[] args)
{
// Input matrices
int A[][] = { { 1, 2 }, { 3, 4 } };
int B[][] = { { 1, 1 }, { 1, 1 } };
// Dimensions of the matrix
int rows = A.length;
int cols = A[0].length;
// Resultant matrix to store the sum
int sum[][] = new int[rows][cols];
// Adding two matrices
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
sum[i][j] = A[i][j] + B[i][j];
}
}
// Printing the resultant matrix
System.out.println("Resultant Matrix:");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
// Print elements on the same line
System.out.print(sum[i][j] + " ");
}
// Move to the next line after printing each row
System.out.println();
}

}
Output:-
Resultant Matrix:
2345

 Use of Vector.

import java.util.Vector;
import java.util.Enumeration;
public class VectorExample
{
public static void main(String[] args)
{
// Create a vector
Vector<String> vector = new Vector<>();
// Add elements to the vector
vector.add("Element1");
vector.add("Element2");
vector.addElement("Element3");
// Add all elements from another collection
Vector<String> anotherVector = new Vector<>();
anotherVector.add("Element4");
anotherVector.add("Element5");
vector.addAll(anotherVector);
// Display vector capacity
System.out.println("Capacity: " + vector.capacity());
// Check if vector contains a specific element
System.out.println("Contains 'Element2': " + vector.contains("Element2"));
// Get element at a specific index
System.out.println("Element at index 1: " + vector.elementAt(1));
// Get enumeration of elements
Enumeration<String> elements = vector.elements();
System.out.print("Elements: ");
while (elements.hasMoreElements())
{
System.out.print(elements.nextElement() + " ");
}
System.out.println();

// Check if two vectors are equal


System.out.println("Vectors are equal: " + vector.equals(anotherVector));
// Get element at a specific index using get method
System.out.println("Element at index 2: " + vector.get(2));
// Get index of a specific element
System.out.println("Index of 'Element3': " + vector.indexOf("Element3"));
// Check if vector is empty
System.out.println("Is vector empty: " + vector.isEmpty());
// Remove a specific element
vector.remove("Element2");
System.out.println("After removing 'Element2': " + vector);
// Remove all elements from another collection
vector.removeAll(anotherVector);
System.out.println("After removing all elements from another vector: " + vector);
// Remove elements based on a condition
vector.removeIf(element -> element.startsWith("Element"));
System.out.println("After removing elements starting with 'Element': " + vector);
// Get hash code of the vector
System.out.println("Hash code: " + vector.hashCode());
// Iterate using forEach
vector.add("Element6");
vector.add("Element7");
System.out.print("Using forEach: ");
vector.forEach(element -> System.out.print(element + " "));
System.out.println();

// Ensure capacity
vector.ensureCapacity(20);
System.out.println("Capacity after ensuring: " + vector.capacity());
// Clear the vector
vector.clear();
System.out.println("After clearing: " + vector);
}
}
Output:-
Capacity: 10
Contains 'Element2': true
Element at index 1: Element2
Elements: Element1 Element2 Element3 Element4 Element5
Vectors are equal: false
Element at index 2: Element3
Index of 'Element3': 2
Is vector empty: false
After removing 'Element2': [Element1, Element3, Element4, Element5]
After removing all elements from another vector: [Element1, Element3]
After removing elements starting with 'Element': []
Hash code: 1
Using forEach: Element6 Element7
Capacity after ensuring: 20
After clearing: []

You might also like