Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- interface ArrayOperation {
- boolean search();
- void update(int index);
- }
- class IntArray implements ArrayOperation {
- private int[] array;
- IntArray(int[] array) {
- this.array = array;
- }
- public boolean search() {
- Scanner scanner = new Scanner(System.in);
- System.out.print("Enter the value to search in the array: ");
- int value = scanner.nextInt();
- for (int i : array) {
- if (i == value) {
- return true;
- }
- }
- return false;
- }
- public void update(int index) {
- if (index >= 0 && index < array.length) {
- array[index] = array[index] + 1;
- } else {
- System.out.println("Invalid index.");
- }
- }
- }
- class StrArray implements ArrayOperation {
- private String source;
- StrArray(String source) {
- this.source = source;
- }
- public boolean search() {
- Scanner scanner = new Scanner(System.in);
- System.out.print("Enter the string to search in the source string: ");
- String searchString = scanner.nextLine();
- return source.contains(searchString);
- }
- public void update(int index) {
- if (index >= 0 && index < source.length()) {
- char[] chars = source.toCharArray();
- chars[index] = '@';
- source = new String(chars);
- } else {
- System.out.println("Invalid index.");
- }
- }
- }
- public class P3 {
- public static void main(String[] args) {
- int[] intArray = {1, 2, 3, 4, 5};
- ArrayOperation intArrayOperation = new IntArray(intArray);
- String sourceString = "Hello, World!";
- ArrayOperation strArrayOperation = new StrArray(sourceString);
- System.out.println("Search result in intArray: " + intArrayOperation.search());
- intArrayOperation.update(2);
- System.out.println("Search result in strArray: " + strArrayOperation.search());
- strArrayOperation.update(7);
- System.out.println("Updated intArray: " + java.util.Arrays.toString(intArray));
- System.out.println("Updated sourceString: " + sourceString);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment