Advertisement
therrontelford

Arrays processing 1

Jan 15th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class processingArrays {
  4.  
  5.     public static void main(String[] args) {
  6.        
  7.         // processing arrays with specific numbers like evens or 1-9
  8.        
  9.         int[] numbers = new int[9];
  10.         for (int i=0; i< numbers.length; i++){
  11.             numbers[i] = i+1;  
  12.         }
  13.            
  14.        
  15.         // processing arrays with user input
  16.         Scanner kb = new Scanner(System.in);
  17.        
  18.         String[] stringArray = new String[5];
  19.         System.out.println("processing arrays with user input");
  20.         for (int i=0; i< stringArray.length; i++){
  21.             System.out.println("Enter your array value");
  22.             stringArray[i]= kb.nextLine();
  23.            
  24.         }
  25.         System.out.println("Testing \n" + stringArray[2]);
  26.         System.out.println("printed the value at stringArray[2]");
  27.        
  28.         System.out.println();
  29.        
  30.         // initializing an array with random values between 50 and 100
  31.         int [] random = new int[12];
  32.         for (int i=0; i<random.length; i++)
  33.             random[i] = 50 + (int)(Math.random( )* 51);
  34.        
  35.         System.out.println();
  36.        
  37.         // printing arrays.  One way with the for loop.  Print array "numbers"
  38.         System.out.println("printing arrays.  One way with the for loop.  Print array \"numbers\"");
  39.         for (int i =0; i<numbers.length; i++)
  40.             System.out.print(numbers[i] + " ");
  41.        
  42.         System.out.println();// goto next line
  43.         System.out.println();
  44.        
  45.         System.out.println("Printing array \"stringArray\" using for loop");
  46.         for (int i = 0; i<stringArray.length; i++)
  47.             System.out.print(stringArray[i]+ " ");
  48.        
  49.         System.out.println(); // goto next line
  50.         System.out.println();
  51.        
  52.         // printing arrays using a for each loop
  53.         System.out.println("printing arrays using a for each loop");
  54.         for (int i : random)
  55.             System.out.print(i+" ");
  56.        
  57.         // goto next line
  58.         System.out.println();
  59.         System.out.println();
  60.        
  61.         // summing all the elements
  62.         System.out.println("the sum of all the elements of the array");
  63.         double[] decimal = new double[8];
  64.         double sum=0;
  65.         for (int i=0; i<decimal.length; i++){
  66.             decimal[i]= (Math.random() * 10 );
  67.             decimal[i]= ((int)(decimal[i] * 100))/100.0;  // this is a neat way to round
  68.             sum = sum + decimal[i];
  69.             }
  70.         for (double e : decimal){
  71.             System.out.print(e+" ");
  72.         }
  73.         System.out.println();
  74.         System.out.println(sum);
  75.         System.out.println();
  76.        
  77.         // finding the largest element
  78.         System.out.println("the largest element of the array");
  79.         int[] elements = new int[9];
  80.         int indexOfLargestElement=0;
  81.         int largestElement=elements[0];
  82.         for (int i=0; i<elements.length; i++){
  83.             elements[i] = (int)(Math.random()* 31);
  84.         }
  85.         for (int i =1; i< elements.length-1; i++){
  86.             if (elements[indexOfLargestElement]< elements[i]){
  87.                 indexOfLargestElement = i;
  88.                 largestElement=elements[i];
  89.             }
  90.             }
  91.         for (int e : elements)
  92.         System.out.print(e+ " ");
  93.         System.out.println();
  94.         System.out.println(elements[indexOfLargestElement]);
  95.        
  96.     }
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement