Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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
- // Renee Waggoner
- // July 17, 2019
- // Special Requirements: None
- package one_dimensional;
- import java.util.Scanner;
- public class OneDimensionalArrays {
- int[] createIntegers(int size_of_array)
- {
- // Your code will create an array of ints as large as specified in size_of_array
- int arr[] = new int[size_of_array];
- //initializing value as 0
- int value = 0;
- // Fill the array in with the values: 0, 100, 200, 300, ....
- for(int x = 0; x <size_of_array; x++)
- {
- //filling each array index with value
- arr[x] = value;
- //incrementing value with 100
- value = value + 100;
- }
- // Return the array that you just created
- return arr;
- }
- void printArray(int[] myArray)
- {
- System.out.println("Displaying the Array Elements: ");
- // Print out your array with one number per line. Get the size of array from the "myArray" parameter (no hard coding the size)
- for(int i = 0; i < myArray.length; i++)
- {
- System.out.println(myArray[i]);
- }
- }
- public static void main(String[] args)
- {
- Scanner keyboard = new Scanner(System.in);
- //prompt for array size
- System.out.print("Enter size of array to create: ");
- //reading array size
- int num = keyboard.nextInt();
- // Construct an instance of the OneDimensionalArrays class of your array.
- OneDimensionalArrays oda = new OneDimensionalArrays();
- // Using this object instance, call createIntegers to create
- int myArray[] = oda.createIntegers(num);
- // Then call the printArray method to print out the contents
- oda.printArray(myArray);
- keyboard.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment