Rewrite Fig 6.2 so that the size of the array is specified by the first command-
ID: 3881780 • Letter: R
Question
Rewrite Fig 6.2 so that the size of the array is specified by the first command-line argument. If no command-argument is supplied, use 10 as the default size of the array.
I // Fig. 7.2: InitArray.java 2 /Initializing the elements of an array to default values of zero 4 public class InitArray 6 public static void main(String[] args) 7 // declare variable array and initialize it with an array obje int[] array = new int [10]; // create the array object System.out, printf("%s %8s%n", "Index", "Value"); // column head // output each array element's value 10 12 13 14 15 16 17 for (int counter-0; counterExplanation / Answer
public class InitArray
{
public static void main(String[] args)
{
int[] array;
// if no argument is passed
if(args.length == 0)
array = new int[10];
else
{
// get the first argument and convert it into int
int len = Integer.parseInt(args[0]);
array = new int[len];
}
System.out.printf("%s%s%n", "Index", "Value");
for(int counter = 0 ; counter < array.length ; counter++ )
System.out.printf("%5d%8d%n", counter, array[counter]);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.