Anilto

Java-OOP

Oct 13th, 2021
728
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //sort
  2.  
  3. import java.util.Arrays;
  4. public class Exercise1 {
  5. public static void main(String[] args){  
  6.    
  7.     int[] my_array1 = {
  8.             1789, 2035, 1899, 1456, 2013,
  9.             1458, 2458, 1254, 1472, 2365,
  10.             1456, 2165, 1457, 2456};
  11.            
  12.     String[] my_array2 = {
  13.             "Java",
  14.            
  15.             "Python",
  16.             "PHP",
  17.             "C#",
  18.             "C Programming",
  19.             "C++"
  20.         };        
  21.     System.out.println("Original numeric array : "+Arrays.toString(my_array1));
  22.     Arrays.sort(my_array1);
  23.     System.out.println("Sorted numeric array : "+Arrays.toString(my_array1));
  24.    
  25.     System.out.println("Original string array : "+Arrays.toString(my_array2));
  26.     Arrays.sort(my_array2);
  27.     System.out.println("Sorted string array : "+Arrays.toString(my_array2));
  28.     }
  29. }
  30.  
  31. //soma
  32.  
  33. public class Exercise2 {
  34. public static void main(String[] args) {      
  35. int my_array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  36. int sum = 0;
  37.  
  38. for (int i : my_array)
  39.     sum += i;
  40. System.out.println("The sum is " + sum);
  41. }
  42. }
  43.  
  44. // media
  45.  
  46. public class Exercise4 {
  47. public static void main(String[] args) {  
  48.  
  49.        int[] numbers = new int[]{20, 30, 25, 35, -16, 60, -100};
  50.        //calculate sum of all array elements
  51.        int sum = 0;
  52.        for(int i=0; i < numbers.length ; i++)
  53.         sum = sum + numbers[i];
  54.        //calculate average value
  55.         double average = sum / numbers.length;
  56.         System.out.println("Average value of the array elements is : " + average);
  57.    }
  58. }
  59.  
  60.  
  61. // quadrado
  62. public class Main {
  63. public static void main(String[] args) {  
  64.  int [][]a = new int[10][10];    
  65.  for(int i = 0; i < 10; i++)
  66.    {
  67.       for(int j = 0; j < 10; j++)
  68.       {
  69.          System.out.printf("%2d ", a[i][j]);
  70.       }
  71.       System.out.println();
  72.    }
  73. }
  74. }
  75.  
  76.  
  77. // contem
  78.  
  79. public class Exercise5 {
  80.   public static boolean contains(int[] arr, int item) {
  81.       for (int n : arr) {
  82.          if (item == n) {
  83.             return true;
  84.          }
  85.       }
  86.       return false;
  87.    }
  88.    public static void main(String[] args) {
  89.           int[] my_array1 = {
  90.             1789, 2035, 1899, 1456, 2013,
  91.             1458, 2458, 1254, 1472, 2365,
  92.             1456, 2265, 1457, 2456};
  93.       System.out.println(contains(my_array1, 2013));
  94.       System.out.println(contains(my_array1, 2015));
  95.    }
  96. }
  97.  
  98. //indice
  99.  
  100. public class Exercise6 {
  101.        public static int  findIndex (int[] my_array, int t) {
  102.         if (my_array == null) return -1;
  103.         int len = my_array.length;
  104.         int i = 0;
  105.         while (i < len) {
  106.             if (my_array[i] == t) return i;
  107.             else i=i+1;
  108.         }
  109.         return -1;
  110.     }
  111.     public static void main(String[] args) {
  112.       int[] my_array = {25, 14, 56, 15, 36, 56, 77, 18, 29, 49};
  113.       System.out.println("Index position of 25 is: " + findIndex(my_array, 25));
  114.       System.out.println("Index position of 77 is: " + findIndex(my_array, 77));
  115.        }
  116. }
  117.  
  118. // remove um elemento
  119. import java.util.Arrays;
  120. public class Exercise7 {
  121.  
  122. public static void main(String[] args) {
  123.    int[] my_array = {25, 14, 56, 15, 36, 56, 77, 18, 29, 49};
  124.    
  125.    System.out.println("Original Array : "+Arrays.toString(my_array));    
  126.    
  127.   // Remove the second element (index->1, value->14) of the array
  128.    int removeIndex = 1;
  129.  
  130.    for(int i = removeIndex; i < my_array.length -1; i++){
  131.         my_array[i] = my_array[i + 1];
  132.       }
  133. // We cannot alter the size of an array , after the removal, the last and second last element in the array will exist twice
  134.     System.out.println("After removing the second element: "+Arrays.toString(my_array));
  135.  }
  136.  }
  137.  
  138.  
  139. //copia array
  140. import java.util.Arrays;
  141. public class Exercise8 {
  142.  public static void main(String[] args) {
  143.    int[] my_array = {25, 14, 56, 15, 36, 56, 77, 18, 29, 49};
  144.    int[] new_array = new int[10];    
  145.  
  146.    System.out.println("Source Array : "+Arrays.toString(my_array));    
  147.    
  148.    for(int i=0; i < my_array.length; i++) {
  149.     new_array[i] = my_array[i];
  150. }
  151.    System.out.println("New Array: "+Arrays.toString(new_array));
  152.  }
  153.  }
  154.  
  155. // insere um elemento
  156. import java.util.Arrays;
  157. public class Exercise9 {
  158.  
  159. public static void main(String[] args) {
  160.  
  161.    int[] my_array = {25, 14, 56, 15, 36, 56, 77, 18, 29, 49};
  162.  
  163.     // Insert an element in 3rd position of the array (index->2, value->5)
  164.    
  165.    int Index_position = 2;
  166.    int newValue    = 5;
  167.  
  168.   System.out.println("Original Array : "+Arrays.toString(my_array));    
  169.    
  170.   for(int i=my_array.length-1; i > Index_position; i--){
  171.     my_array[i] = my_array[i-1];
  172.    }
  173.    my_array[Index_position] = newValue;
  174.    System.out.println("New Array: "+Arrays.toString(my_array));
  175.  }
  176.  }
  177.  
RAW Paste Data