[go: up one dir, main page]

0% found this document useful (0 votes)
12 views10 pages

Abdul Subhan Lab Report 2

Uploaded by

Abdul Subhan
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)
12 views10 pages

Abdul Subhan Lab Report 2

Uploaded by

Abdul Subhan
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/ 10

NAME:

ABDUL SUBHAN
REG NO:
B21F0110SE008
DEPT:
SE2
DATED:2/3/2024

LAB#2 REPORT
OBJECTIVES:
This aims on practicing of java basics. We practice arrays 2 dimensional
arrays and constructors and destructors. We will do some tasks to recall the basics of java.

TASKS:
TASK1: Searching for a Specific Element
CODE:
class Main {
// Function to implement
// search operation
static int findElement(int arr[], int n, int key)
{
for (int i = 0; i < n; i++)
if (arr[i] == key)
return i;
// If the key is not found
return -1;
}
// Driver's Code
public static void main(String args[])
{
int arr[] = { 12, 34, 10, 6, 40 };
int n = arr.length;

// Using a last element as search element


int key = 40;
// Function call
int position = findElement(arr, n, key);
if (position == -1)
System.out.println("Element not found");
else
System.out.println("Element Found at Position: "
+ (position + 1));
}
}
OUTPUT:

TASK2: Sorting the Array in Ascending Order


CODE:
import java.util.Arrays;
class GFG {
public static void main(String args[])
{
int[] arr = { 5, 2, 23, 7, 87, 42, 509 };
System.out.println("The original array is: ");
for (int num : arr) {
System.out.print(num + " ");
}
Arrays.sort(arr);
System.out.println("\nThe sorted array is: ");
for (int num : arr) {
}
}
}
OUTPUT:

TASK3: Finding Maximum and Minimum Elements

CODE:
import java.util.*;
class Array {
public static void main(String[] args) {
int a[]={1,423,6,46,34,23,13,53,4};
//Implemented inbuilt function to sort array
Arrays.sort(a);
// after sorting the value at 0th position will minimum and
//nth position will be maximum
System.out.println("min= "+a[0]+" max= "+a[a.length-1]);
}
}
OUTPUT:

TASK4: Analyzing the Array


CODE:
import java.util.Scanner;

public class ArrayAnalyzer {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of elements in the array: ");


int n = scanner.nextInt();

int[] numbers = new int[n];

System.out.println("Enter " + n + " elements:");


for (int i = 0; i < n; i++) {
numbers[i] = scanner.nextInt();
} // Display the original array
System.out.println("Original array:")
displayArray(numbers);

int max = findMax(numbers);


int min = findMin(numbers);
System.out.println("Maximum value: " + max);
System.out.println("Minimum value: " + min);

double average = calculateAverage(numbers);


System.out.println("Average value: " + average);
int[] counts = countEvenOdd(numbers);
System.out.println("Number of even numbers: " + counts[0]);
System.out.println("Number of odd numbers: " + counts[1]);
scanner.close();
}
public static int findMax(int[] arr) {
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
public static int findMin(int[] arr) {
int min = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
public static double calculateAverage(int[] arr) {
int sum = 0;
for (int num : arr) {
sum += num;
}
return (double) sum / arr.length;
}
public static int[] countEvenOdd(int[] arr) {
int evenCount = 0;
int oddCount = 0;

for (int num : arr) {


if (num % 2 == 0) {
evenCount++;
} else {
oddCount++;
}
}
return new int[]{evenCount, oddCount};
}
public static void displayArray(int[] arr) {
for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();
}
}
OUTPUT:

TASK5: Initializing the Array


CODE:
import java.util.Scanner;
import java.util.Random;
public class ArrayInitialization ;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Choose how to initialize the array:");


System.out.println("1. Random values");
System.out.println("2. Input values manually");
System.out.print("Enter your choice (1 or 2): ");
int choice = scanner.nextInt();
int[] numbers;
if (choice == 1) {
System.out.print("Enter the size of the array: ");
int size = scanner.nextInt();
numbers = generateRandomArray(size);
} else if (choice == 2) {
numbers = inputArrayFromUser();
} else {
System.out.println("Invalid choice. Exiting...");
return;
}
// Display the initialized array
System.out.println("Initialized array:");
displayArray(numbers);
scanner.close(); // Close the scanner to prevent resource leak
}// Method to generate an array of random integers
public static int[] generateRandomArray(int size) {
Random random = new Random();
int[] arr = new int[size]; for (int i = 0; i < size; i++) {
arr[i] = random.nextInt();
} return arr;
} // Method to input array elements from the user
public static int[] inputArrayFromUser() {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of elements in the array: ");


int n = scanner.nextInt();
int[] arr = new int[n];
System.out.println("Enter " + n + " elements:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
} return arr;
} public static void displayArray(int[] arr) {
for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();
}}
OUTPUT:
Conclusion:
The Java program successfully fulfills the requirements by implementing methods to
perform various operations on an array of integers. These operations include searching for a
specific element, sorting the array, finding the maximum and minimum elements, and
conducting basic analysis such as calculating the average and counting the number of even
and odd numbers. The program provides flexibility by allowing the user to input array
elements or generating random values for the array. Overall, the program provides a useful
tool for analyzing and manipulating arrays of numbers in Java.

You might also like