aakash2310

Untitled

Mar 13th, 2024
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. import java.util.*;
  2. interface ArrayOperation {
  3. boolean search();
  4. void update(int index);
  5. }
  6. class IntArray implements ArrayOperation {
  7. private int[] array;
  8.  
  9. IntArray(int[] array) {
  10. this.array = array;
  11. }
  12.  
  13. public boolean search() {
  14. Scanner scanner = new Scanner(System.in);
  15. System.out.print("Enter the value to search in the array: ");
  16. int value = scanner.nextInt();
  17. for (int i : array) {
  18. if (i == value) {
  19. return true;
  20. }
  21. }
  22. return false;
  23. }
  24.  
  25. public void update(int index) {
  26. if (index >= 0 && index < array.length) {
  27. array[index] = array[index] + 1;
  28. } else {
  29. System.out.println("Invalid index.");
  30. }
  31. }
  32. }
  33.  
  34.  
  35. class StrArray implements ArrayOperation {
  36. private String source;
  37.  
  38. StrArray(String source) {
  39. this.source = source;
  40. }
  41.  
  42. public boolean search() {
  43. Scanner scanner = new Scanner(System.in);
  44. System.out.print("Enter the string to search in the source string: ");
  45. String searchString = scanner.nextLine();
  46. return source.contains(searchString);
  47. }
  48.  
  49. public void update(int index) {
  50. if (index >= 0 && index < source.length()) {
  51. char[] chars = source.toCharArray();
  52. chars[index] = '@';
  53. source = new String(chars);
  54. } else {
  55. System.out.println("Invalid index.");
  56. }
  57. }
  58. }
  59.  
  60.  
  61. public class P3 {
  62. public static void main(String[] args) {
  63. int[] intArray = {1, 2, 3, 4, 5};
  64. ArrayOperation intArrayOperation = new IntArray(intArray);
  65.  
  66. String sourceString = "Hello, World!";
  67. ArrayOperation strArrayOperation = new StrArray(sourceString);
  68.  
  69.  
  70. System.out.println("Search result in intArray: " + intArrayOperation.search());
  71. intArrayOperation.update(2);
  72. System.out.println("Search result in strArray: " + strArrayOperation.search());
  73. strArrayOperation.update(7);
  74.  
  75.  
  76. System.out.println("Updated intArray: " + java.util.Arrays.toString(intArray));
  77. System.out.println("Updated sourceString: " + sourceString);
  78. }
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment