Write a program with a main method that asks the user to enter an array of 10 in
ID: 3876378 • 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: Enter a number 34 Enter a number: 21 Enter a number 35 Enter a number 12 Enter a number Enter a number: Enter a number: Enter a number: 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
Create a file named code.java and paste given code into it!
code.java
import java.util.Scanner;
public class code {
public static void printreverse(int[] x)
{
System.out.print("Here the array in reverse: ");
for(int i = x.length - 1; i >=0; i--)
{
System.out.print(x[i] + ",");
}
System.out.print(" ");
}
public static void getLargest(int[] x)
{
int maxx = x[0];
for(int i = x.length - 1; i >=0; i--)
{
maxx = Math.max(maxx, x[i]);
}
System.out.println("The Larget number is " + maxx + ".");
}
public static void computeTwice(int[] x)
{
System.out.print("The array with two times the numbers: ");
for(int i = x.length - 1; i >=0; i--)
{
System.out.print(2*x[i] + ",");
}
}
public static void main(String[] args)
{
int [] array = new int[10];
int i = 0;
while(i < 10)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number: ");
int input = scanner.nextInt();
array[i] = input;
i++;
}
printreverse(array);
getLargest(array);
computeTwice(array);
}
}
Sample Output:
Enter a number:
11
Enter a number:
22
Enter a number:
33
Enter a number:
44
Enter a number:
55
Enter a number:
66
Enter a number:
77
Enter a number:
88
Enter a number:
99
Enter a number:
100
Here the array in reverse: 100,99,88,77,66,55,44,33,22,11
The Larget number is 100.
The array with two times the numbers: 200,198,176,154,132,110,88,66,44,22
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.