letsdoitjava

One Denominational Arrays

Jul 14th, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. // One Dimensional Array, this program takes in input from user to determine size of array, the array elements are then displayed by increments of 100 one per line
  2. // Renee Waggoner
  3. // July 17, 2019
  4. // Special Requirements: None
  5.  
  6. package one_dimensional;
  7.  
  8. import java.util.Scanner;
  9.  
  10. public class OneDimensionalArrays {
  11.     int[] createIntegers(int size_of_array)
  12.     {    
  13.         // Your code will create an array of ints as large as specified in size_of_array
  14.         int arr[] = new int[size_of_array];
  15.  
  16.         //initializing value as 0
  17.         int value = 0;
  18.  
  19.         // Fill the array in with the values: 0, 100, 200, 300, ....
  20.         for(int x = 0; x <size_of_array; x++)
  21.         {
  22.             //filling each array index with value
  23.             arr[x] = value;
  24.  
  25.             //incrementing value with 100
  26.             value = value + 100;
  27.         }
  28.  
  29.         // Return the array that you just created
  30.         return arr;
  31.     }
  32.  
  33.     void printArray(int[] myArray)
  34.     {    
  35.         System.out.println("Displaying the Array Elements: ");
  36.  
  37.         // Print out your array with one number per line. Get the size of array from the "myArray" parameter (no hard coding the size)
  38.         for(int i = 0; i < myArray.length; i++)
  39.         {
  40.             System.out.println(myArray[i]);
  41.         }
  42.     }
  43.  
  44.     public static void main(String[] args)
  45.     {
  46.  
  47.         Scanner keyboard = new Scanner(System.in);
  48.  
  49.         //prompt for array size
  50.         System.out.print("Enter size of array to create: ");
  51.  
  52.         //reading array size
  53.         int num = keyboard.nextInt();
  54.  
  55.         // Construct an instance of the OneDimensionalArrays class of your array.
  56.         OneDimensionalArrays oda = new OneDimensionalArrays();
  57.  
  58.         // Using this object instance, call createIntegers to create
  59.         int myArray[] = oda.createIntegers(num);
  60.  
  61.         // Then call the printArray method to print out the contents
  62.         oda.printArray(myArray);
  63.  
  64.         keyboard.close();
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment