Advertisement
leahbeah

Project.1

Dec 15th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3. public class BigProject1 {
  4.  
  5. //method to print the array delimited by spaces
  6. public static int[] print(int[] arr) {
  7. for(int i=0; i<arr.length; i++) {
  8. System.out.print(arr[i]+ " ");
  9. }
  10. return arr;
  11. }
  12. //method to sort the array
  13. public static int[] sorted(int[] arr2) {
  14. for(int o=0; o<arr2.length; o++) {
  15. Arrays.sort(arr2);
  16. }
  17. return arr2;
  18. }
  19. //method to count the number of different elements in the array
  20. public static int different(int[] arr3) {
  21. if(arr3.length==0) {
  22. return 0;
  23. }
  24. int counter=1;
  25. for(int x=1; x<arr3.length; x++) {
  26. if(arr3[x]!=arr3[x-1]) {
  27. counter++;
  28. }
  29. }
  30. return counter;
  31. }
  32. /*public static int[] pals(int[] arr4){
  33. int count2=0;
  34. for(int i=0; i<arr4.length; i++) {
  35. int n= arr4[i];
  36. int rev=0;
  37. int tmp=0;
  38. while(n>0) {
  39. tmp=n%10;
  40. n=n/10;
  41. rev=rev*10+tmp;
  42. }
  43.  
  44. if(arr4[i]==rev) {
  45. count2++;
  46. }
  47. else {
  48. System.out.print("");
  49. }
  50. }
  51. return arr4;
  52. }*/
  53.  
  54. public static void main(String[] args) {
  55. /*prompts user for the length of their array, initializes an array with that
  56. * length, loops through all the elements the user inputs, and stores every
  57. * element in the array, then calling the print method to print the array
  58. */
  59. Scanner scan= new Scanner(System.in);
  60. System.out.print("Enter the length of the array: ");
  61. int len= scan.nextInt();
  62. int[] arr= new int[len];
  63. for(int i=0; i<arr.length; i++) {
  64. arr[i]=scan.nextInt();
  65. }
  66.  
  67. print(arr);
  68. //printing the length of the array again for the first objective
  69. System.out.println("\nNumber of elements:"+arr.length);
  70. /*initializing a variable at position 0 of the array, loops through all the
  71. * elements of the array, finds the largest element and prints it
  72. */
  73. int max1=arr[0];
  74. for (int j=0; j<len; j++) {
  75. if (max1 < arr[j]) {
  76. max1 = arr[j];
  77. }
  78. }
  79. System.out.println("Largest number: " +max1);
  80. //calling the print and sort methods to print the sorted array
  81. print(sorted(arr));
  82. /*Initializing two variables,looping through all elements of the array using
  83. *if statements to check which value is the smallest and which is the
  84. *second smallest and printing the second smallest element
  85. */
  86. int min=Integer.MAX_VALUE;
  87. int min2=Integer.MAX_VALUE;
  88. for(int k=0; k<arr.length; k++) {
  89. if(arr[k]==min) {
  90. min2=min;
  91. }
  92. if(arr[k]<min) {
  93. min2=min;
  94. min=arr[k];
  95. }
  96. else if(arr[k]<min2) {
  97. min2=arr[k];
  98. }
  99. }
  100. //System.out.print("\nSmallest number:"+min);
  101. System.out.print("\nSecond smallest number: "+min2);
  102. //counting all even numbers and printing that number
  103. int count=0;
  104. for(int l=0; l<arr.length; l++) {
  105. if(arr[l]%2==0) {
  106. count++;
  107. }
  108. }
  109. System.out.println("\nNumber of evens: "+count);
  110. //counting all odd numbers and printing that number
  111. int count1=0;
  112. for(int m=0; m<arr.length; m++) {
  113. if(arr[m]%2!=0) {
  114. count1++;
  115. }
  116. }
  117. System.out.println("Number of odds: "+count1);
  118. /*initializing a variable with 0, looping through the elements in the array
  119. * setting the variable to be itself plus the element in each position of
  120. * array, printing the sum of all elements
  121. */
  122. int sum=0;
  123. for(int q=0; q<arr.length; q++) {
  124. sum=sum+arr[q];
  125. }
  126. System.out.println("Sum of all numbers: "+sum);
  127.  
  128. /*reversing the numbers in the array and comparing the original number
  129. * with the reversed one, printing the number of numbers with equal
  130. * reversed and original forms
  131. */
  132. int[] palarr=new int[len];
  133. int index=0;
  134. int count2=0;
  135. for(int i=0; i<arr.length; i++) {
  136. int n= arr[i];
  137. int rev=0;
  138. int tmp=0;
  139. while(n>0) {
  140. tmp=n%10;
  141. n=n/10;
  142. rev=rev*10+tmp;
  143. }
  144. if(arr[i]==rev) {
  145. count2++;
  146. arr[i]=palarr[index];
  147. index++;
  148. }
  149. else {
  150. System.out.print("");
  151. }
  152. }
  153. print(palarr);
  154. System.out.println("\nNumber of palindromic numbers: "+count2);
  155. //calling the method that finds the number of different elements and printing
  156. System.out.println("Number of different elements: "+different(arr));
  157.  
  158. /*int count3=1;
  159. for(int x=1; x<arr.length; x++) {
  160. if(arr[x]==arr[x-1]){
  161. count3++;
  162. //System.out.print("Frequency of: "+arr[x]+"is"+count3/arr.length*100+"%");
  163. System.out.println(count3);
  164. }
  165. }*/
  166.  
  167. //printing average of all elements by taking the sum and dividing by the length
  168. System.out.println("Average of all numbers: "+sum/len);
  169.  
  170.  
  171. }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement