ICSE Java Programming Array of Integers 2022 Q2 Solved
import java.util.Scanner;
public class ICSEJava2022 {
public static void main(String[] args) {
int N, s;
Scanner in = new Scanner(System.in);
System.out.println("Enter the size of the array:");
N = in.nextInt();
ArraySearch obj = new ArraySearch(N);
obj.inputElements(in);
System.out.println("Enter the name to be searched:");
s = in.nextInt();
obj.searchElement(s);
in.close();
}
}
class ArraySearch {
public void searchElement(int s) {
boolean flag = false;
for (int i = 0; i < this.array_size; i++) {
if (s == this.array[i]) {
System.out.println("The entered element is at position " + (i + 1));
flag = true;
}
}
if (!flag)
System.out.println("The entered element is not present in the array.");
}
public void inputElements(Scanner in) {
System.out.println("Enter the elements of the array:");
for (int i = 0; i < this.array_size; i++) {
System.out.println("Enter the element at position:" + (i + 1));
this.array[i] = in.nextInt();
}
}
ArraySearch(int N) {
this.array_size = N;
this.array = new int[this.array_size];
}
private static int array_size;
private int[] array;
}
No comments:
Post a Comment