Write a program with a main method that asks the user to enter an array of 10 in
ID: 3879485 • Letter: W
Question
Write a program with a main method that asks the user to enter an array of 10 integers. Your main method then calls each of the three methods described below and prints out the results of the two methods which return values.
printReverse - a void method that reverses the elements of the array and prints out all the elements in one line separated by commas (see sample output below).
getLargest - an int method that returns the largest value in the array.
computeTwice- a method that returns an array of int which contains 2 times of all the numbers in the array (see the sample output below).
Sample output:
Enter a number:
22
Enter a number:
34
Enter a number:
21
Enter a number:
35
Enter a number:
12
Enter a number:
4
Enter a number:
2
Enter a number:
3
Enter a number:
99
Enter a number:
81
Here the array in reverse: 81, 99, 3, 2, 4, 12, 35, 21, 34, 22
The highest number is 99
The array with two times the numbers: [162, 198, 6, 4, 8, 24, 70, 42, 68, 44]
Explanation / Answer
import java.util.Scanner;
public class ArrayPrinting {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int arr[] = new int[10];
for (int i = 0; i < 10; i++) {
System.out.println("Enter a number: ");
arr[i] = sc.nextInt();
}
printReverse(arr);
int max=getLarge(arr);
System.out.println("The highest number is "+max);
int arr2[]=computeTwice(arr);
System.out.print("The array with two times the number:[");
for(int i=0;i<10;i++)
{
if(i<9)
System.out.print(arr2[i]+",");
else
System.out.print(arr2[i]+"]");
}
}
private static int[] computeTwice(int[] arr) {
int arr2[]=new int[10];
for(int i=0;i<10;i++)
arr2[i]=2*arr[i];
return arr2;
}
private static int getLarge(int[] arr) {
int max=arr[0];
for(int i=0;i<10;i++)
{
if(arr[i]>max)
max=arr[i];
}
return max;
}
private static void printReverse(int[] arr) {
System.out.print("Here the array in reverse:");
for(int i=9;i>=0;i--)
{
if(i>1)
System.out.print(arr[i]+",");
else
System.out.println(arr[i]);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.