0% found this document useful (0 votes)
5 views9 pages

Az Oop 1B

The document outlines a Java programming lab exercise focused on arrays and array of objects. It includes tasks such as creating a main array and a search array, finding occurrences of elements, printing duplicate characters, and copying elements into ascending and descending order arrays. The document provides code examples for each task along with input validation and sorting methods.

Uploaded by

rohith07b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

Az Oop 1B

The document outlines a Java programming lab exercise focused on arrays and array of objects. It includes tasks such as creating a main array and a search array, finding occurrences of elements, printing duplicate characters, and copying elements into ascending and descending order arrays. The document provides code examples for each task along with input validation and sorting methods.

Uploaded by

rohith07b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

23DC2002 - Object Oriented Programming Lab Reg.No.

URK23CO2018

Ex. No. 1 b ARRAYS AND ARRAY OF OBJECTS

Date of Exercise 30.07.2024

Aim

To create an array to store a given number by the user. Write a java program to find out all the
search array (another input array) and its positions.

Description

Input Main Array: The user inputs numbers into a main array.

Input Search Array: The user inputs numbers into a search array.

Search Algorithm: The program checks each number in the search array and finds all occurrences of
that number in the main array.

Output: It prints each element from the search array along with its positions in the main array.

An array is a data structure that stores a fixed-size sequential collection of elements of the same type.
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for
each value. The elements in an array are indexed, starting from 0.

Q1. To create an array to store a given number by the user. Write a java program to find out all the
search array (another input array) and its positions.

Program

import java.util.Scanner;

public class ArraySearchPositions {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("URK23CO2018");

// Read the size and elements of the main array

1
23DC2002 - Object Oriented Programming Lab Reg.No. URK23CO2018

System.out.print("Enter the size of the main array: ");

int mainArraySize = scanner.nextInt();

int[] mainArray = new int[mainArraySize];

System.out.println("Enter the elements of the main array:");

for (int i = 0; i < mainArraySize; i++) {

mainArray[i] = scanner.nextInt();

// Read the size and elements of the search array

System.out.print("Enter the size of the search array: ");

int searchArraySize = scanner.nextInt();

int[] searchArray = new int[searchArraySize];

System.out.println("Enter the elements of the search array:");

for (int i = 0; i < searchArraySize; i++) {

searchArray[i] = scanner.nextInt();

// Find and print the positions of search array elements in the main array

for (int i = 0; i < searchArraySize; i++) {

int searchValue = searchArray[i];

System.out.print("Element " + searchValue + " found at positions: ");

boolean found = false;

for (int j = 0; j < mainArraySize; j++) {

if (mainArray[j] == searchValue) {

2
23DC2002 - Object Oriented Programming Lab Reg.No. URK23CO2018

System.out.print(j + " ");

found = true;

if (!found) {

System.out.print("Not found ");

System.out.println();

scanner.close();

Output Screenshot:

3
23DC2002 - Object Oriented Programming Lab Reg.No. URK23CO2018

Q2.Perform the following operations:

Write a program in Java to print duplicate characters from an array? Write a program in Java
to count the number of vowels and consonants in a character array?

Program

import java.util.Scanner;

public class DuplicateCharacters {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("URK23CO2018");
// Read the size and elements of the character array
System.out.print("Enter the size of the character array: ");
int arraySize = scanner.nextInt();
char[] charArray = new char[arraySize];

System.out.println("Enter the elements of the character array:");


for (int i = 0; i < arraySize; i++) {
charArray[i] = scanner.next().charAt(0);
}
// Find and print duplicate characters
boolean hasDuplicates = false;
for (int i = 0; i < charArray.length; i++) {
// Check if current character has already been checked
boolean alreadyChecked = false;
for (int k = 0; k < i; k++) {
if (charArray[i] == charArray[k]) {
alreadyChecked = true;
break;
}
}
if (alreadyChecked) {
continue;
}
// Count occurrences of charArray[i]
int count = 0;
for (int j = 0; j < charArray.length; j++) {
if (charArray[j] == charArray[i]) {
count++;

4
23DC2002 - Object Oriented Programming Lab Reg.No. URK23CO2018

}
}
if (count > 1) {
System.out.println(charArray[i] + " appears " + count + " times");
hasDuplicates = true;
}
}
if (!hasDuplicates) {
System.out.println("No duplicate characters found.");
}
scanner.close();
}
}

Output Screenshot

5
23DC2002 - Object Oriented Programming Lab Reg.No. URK23CO2018

Q3. Write a java program to copy the given N numbers of one array into another 2 arrays in such
a way that one array must contain the numbers in ascending order and the other must contain in
a descending order.

Program

import java.util.Scanner;

public class CopyAndSortArrays {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("URK23CO2018");
// Read the size of the array with manual input validation
int n = 0;
while (true) {
System.out.print("Enter the number of elements: ");
String input = scanner.nextLine();
if (isInteger(input)) {
n = Integer.parseInt(input);
if (n > 0) {
break; // Exit loop if input is a positive integer
} else {
System.out.println("Please enter a positive number.");
}
} else {
System.out.println("Invalid input. Please enter an integer.");
}
}
int[] originalArray = new int[n];
int[] ascendingArray = new int[n];
int[] descendingArray = new int[n];
// Read the elements of the original array with manual input validation
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
while (true) {
System.out.print("Element " + (i + 1) + ": ");
String input = scanner.nextLine();
if (isInteger(input)) {
originalArray[i] = Integer.parseInt(input);
break; // Exit loop if input is a valid integer
} else {

6
23DC2002 - Object Oriented Programming Lab Reg.No. URK23CO2018

System.out.println("Invalid input. Please enter an integer.");


}
}
}
// Copy the elements from the original array to ascending and descending arrays
for (int i = 0; i < n; i++) {
ascendingArray[i] = originalArray[i];
descendingArray[i] = originalArray[i];
}
// Sort the ascendingArray in ascending order using Bubble Sort
bubbleSortAscending(ascendingArray);
// Sort the descendingArray in descending order using Bubble Sort
bubbleSortDescending(descendingArray);
// Print the arrays
System.out.println("Array in ascending order:");
for (int num : ascendingArray) {
System.out.print(num + " ");
}
System.out.println();

System.out.println("Array in descending order:");


for (int num : descendingArray) {
System.out.print(num + " ");
}
System.out.println();

scanner.close();
}
// Method to check if a string is a valid integer
private static boolean isInteger(String str) {
if (str == null || str.isEmpty()) {
return false;
}
for (char c : str.toCharArray()) {
if (!Character.isDigit(c) && (c != '-' || str.indexOf(c) != 0)) {
return false;
}
}
try {
Integer.parseInt(str);
} catch (NumberFormatException e) {

7
23DC2002 - Object Oriented Programming Lab Reg.No. URK23CO2018

return false;
}
return true;
}
// Bubble Sort to sort an array in ascending order
public static void bubbleSortAscending(int[] array) {
int n = array.length;
boolean swapped;
for (int i = 0; i < n - 1; i++) {
swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (array[j] > array[j + 1]) {
// Swap array[j] and array[j + 1]
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
swapped = true;
}
}
// If no elements were swapped, the array is sorted
if (!swapped) break;
}
}
// Bubble Sort to sort an array in descending order
public static void bubbleSortDescending(int[] array) {
int n = array.length;
boolean swapped;
for (int i = 0; i < n - 1; i++) {
swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (array[j] < array[j + 1]) {
// Swap array[j] and array[j + 1]
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
swapped = true;
}
}
// If no elements were swapped, the array is sorted
if (!swapped) break;
}

8
23DC2002 - Object Oriented Programming Lab Reg.No. URK23CO2018

}
}

Output Screenshot

Result

Thus the usage of array is successfully implemented and executed

You might also like