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

Safik Exp-1b

The document outlines various Java programming exercises focused on arrays and character manipulation. It includes programs for searching elements in an array, identifying duplicate characters, counting occurrences of specific characters, and finding the highest occurred character in an array. Each exercise is accompanied by code snippets and confirms successful execution of the programs.

Uploaded by

annacruze
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)
10 views9 pages

Safik Exp-1b

The document outlines various Java programming exercises focused on arrays and character manipulation. It includes programs for searching elements in an array, identifying duplicate characters, counting occurrences of specific characters, and finding the highest occurred character in an array. Each exercise is accompanied by code snippets and confirms successful execution of the programs.

Uploaded by

annacruze
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.

URK23CO2029

Ex. No. 1 ARRAYS AND ARRAY OF OBJECTS

Date of Exercise

b)

5.

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.

Program

import java.util.Scanner;

import java.util.ArrayList;

public class ArraySearch {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("URK23CO2029");

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

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

int mainArraySize = scanner.nextInt();

int[] mainArray = new int[mainArraySize];

System.out.println("Enter " + mainArraySize + " elements for the main array:");

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

mainArray[i] = scanner.nextInt();

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

int searchArraySize = scanner.nextInt();

int[] searchArray = new int[searchArraySize];

System.out.println("Enter " + searchArraySize + " elements for the search array:");

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

searchArray[i] = scanner.nextInt();

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

int searchElement = searchArray[i];

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

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

if (mainArray[j] == searchElement) {

positions.add(j);

if (positions.isEmpty()) {

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

System.out.println("Element " + searchElement + " not found in the main array.");

} else {

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

for (int position : positions) {

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

System.out.println();

scanner.close();

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

Output Screenshot

Result

The given program has been Successfully executed and the desired output has been.

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

8.

Aim

To perform the following operations: To write a program in Java to print duplicate


characters from an array and write a program in Java to count the number of vowels and
consonants in a character array.

Description

This program identifies and prints duplicate characters from a given character array by using a
basic array for counting occurrences. It checks each character and maintains a separate list of
characters that appear more than once.

Program

public class DuplicateCharacters {

public static void main(String[] args) {

System.out.println("URK23CO2029");

char[] charArray = {'a', 'b', 'c', 'a', 'd', 'b', 'e', 'f', 'g', 'e'};

int[] charCounts = new int[256];

for (char c : charArray) {

charCounts[c]++;

System.out.println("Duplicate characters in the array:");

boolean duplicatesFound = false;

for (int i = 0; i < charCounts.length; i++) {

if (charCounts[i] > 1) {

System.out.println((char) i + " appears " + charCounts[i] + " times");

duplicatesFound = true;

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

if (!duplicatesFound) {

System.out.println("No duplicate characters found.");

Output Screenshot

Result

The given program has been Successfully executed and the desired output has been.

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

10.

Aim

To perform the following operations: write a program in Java to count the occurrence of a
given character in an array and write a program in Java to return the highest occurred character
in an array.

Description

This program counts the number of times a user-specified character occurs in a given character
array. It iterates through the array and compares each element to the target character,
incrementing a counter whenever a match is found and program finds and returns the character
that occurs most frequently in a given character array. It uses an integer array to keep track of
each character's frequency and determines the character with the highest frequency.

Program

import java.util.Scanner;

public class CharacterAnalyzer {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("URK23CO2029");

char[] charArray = {'h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd'};

System.out.print("Enter the character to count occurrences: ");

char targetChar = scanner.next().charAt(0);

int occurrenceCount = countOccurrences(charArray, targetChar);

System.out.println("Character '" + targetChar + "' occurs " + occurrenceCount + " times in


the array.");

char highestOccurredChar = findHighestOccurredCharacter(charArray);

int highestOccurrenceCount = countOccurrences(charArray, highestOccurredChar);

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

System.out.println("The highest occurred character is '" + highestOccurredChar + "' with "


+ highestOccurrenceCount + " occurrences.");

scanner.close();

public static int countOccurrences(char[] charArray, char targetChar) {

int count = 0;

for (char c : charArray) {

if (c == targetChar) {

count++;

return count;

public static char findHighestOccurredCharacter(char[] charArray) {

int[] charCounts = new int[256];

for (char c : charArray) {

charCounts[c]++;

int maxCount = 0;

char highestOccurredChar = '\0';

for (int i = 0; i < charCounts.length; i++) {

if (charCounts[i] > maxCount) {

maxCount = charCounts[i];

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

highestOccurredChar = (char) i;

return highestOccurredChar;

Output Screenshot

Result

The given program has been Successfully executed and the desired output has been.

You might also like