Write a Java Code for the following Question 10)(a)Write a function that prints
ID: 3628930 • Letter: W
Question
Write a Java Code for the following
Question 10)(a)Write a function that prints an array of integers. Test the function with arrays of length 0, 1 and7. The function should format the arrays as follows:
// example output
{}
{5}
{4, 8, 12, 54, 3, 0, 2}
(b)Write a function that takes two integers, x and y, as inputs (NOT from the user but use some direct values), and creates an array containing all the
numbers from x to y. The function should return an array. Test the function with two pairs of
x and y, and print the resulting arrays with your function from (a).
// example output
Array from 3 to 9: {3, 4, 5, 6, 7, 8, 9}
Array from 0 to 2: {0, 1, 2}
Please do not input any values from the user.
the functions should return data and should NOT print anything
Example template:
public class One
{
public static void question10()
{
int sum = calcSum(5);
System.out.println("The sum of 1 to 5 is " + sum);
}
public static int calcSum(int y)
{
...
return sum;
}
...
}
Explanation / Answer
please rate - thanks
import java.util.*;
public class Main
{public static void main(String[] args)
{int a[]={5};
int b[]={4, 8, 12, 54, 3, 0, 2};
int c[]={};
print(a);
print(b);
print(c);
}
public static void print(int a[])
{int i;
System.out.print("{");
for(i=0;i<a.length-1;i++)
System.out.print(a[i]+", ");
if(a.length<1)
System.out.println("}");
else
System.out.println(a[a.length-1]+"}");
}
}
------------------
import java.util.*;
public class Main
{public static void main(String[] args)
{
int begin=2, end=9;
int []a=new int[end-begin+1];
create(a,begin,end);
System.out.print("Array from "+begin+" to "+end+": ");
print(a);
}
public static void print(int a[])
{int i;
System.out.print("{");
for(i=0;i<a.length-1;i++)
System.out.print(a[i]+", ");
if(a.length<1)
System.out.println("}");
else
System.out.println(a[a.length-1]+"}");
}
public static void create(int a[],int b, int e)
{int i;
for(i=b;i<=e;i++)
a[i-b]=i;
}
}