Please add in commented steps as well. Thank you so much. Your program should co
ID: 3868360 • Letter: P
Question
Please add in commented steps as well. Thank you so much.
Your program should contain a main method that: . Creates an integer array with a method named createArray() createArray() should take an integer parameter indicating the size of the requested array createArray() should use iteration (looping) to create and populate an array with the requested value pattern/values as described in the assignment createArray() should return the populated integer array to the main method o o o Passes the returned array to a method named printArray() . o printArray() should take an integer array as a parameter o printArray) should print out the contents of the array one element at a time with each element on its own line using iteration (looping) Your program and methods should work for any size array(s). Make sure you comment and format your program appropriately or points will be deducted. The purpose of this project is to test program submissions to Livelab and CSCI 1301 concepts. Code Because Livelab can only process single files, use the following template for your class: public class PAssigne ( // Implement main method // Implement createArray() and printArray() Expected Output (10 Element Array) 6 12
Explanation / Answer
PAssign0.java
import java.util.Scanner;
public class PAssign0 {
//Declaring static variable
static Scanner sc = null;
public static void main(String[] args) {
//Declaring variables
int size;
// Scanner object is used to get the inputs entered by the user
sc = new Scanner(System.in);
//Getting the Size entered by the user
System.out.print("Enter the size of an array :");
size = sc.nextInt();
//Calling an methods
int arr[] = createArray(size);
printArray(arr);
}
//This method will display the elements in the array
private static void printArray(int arr[]) {
System.out.println("Displaying the Elements in the Array :");
//using for loop displaying the Elements in the array
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
/* This method will create an array based on the size and
* getting the inputs entered by the user and populate those
* values into an array and return the array
*/
private static int[] createArray(int size) {
//Create an array base on the size
int arr[] = new int[size];
//using for loop getting the Elements and populate those values into the array
for (int i = 0; i < size; i++) {
System.out.print("Enter the Element#" + (i + 1) + ":");
arr[i] = sc.nextInt();
}
return arr;
}
}
_______________________
Output:
Enter the size of an array :10
Enter the Element#1:0
Enter the Element#2:3
Enter the Element#3:6
Enter the Element#4:9
Enter the Element#5:12
Enter the Element#6:15
Enter the Element#7:18
Enter the Element#8:21
Enter the Element#9:24
Enter the Element#10:27
Displaying the Elements in the Array :
0
3
6
9
12
15
18
21
24
27
________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.