189k views
3 votes
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 prints out the array in reverse of the way it was entered all on one line separated by commas (see sample output below).

getLargest - an int method that returns the largest value in the array.

computeTwice- an method that returns an array of int which contains two 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 are your numbers in reverse: 81, 99, 3, 2, 4, 12, 35, 21, 34, 22
The highest number is 99
The array with two times the numbers: [44, 68, 42, 70, 24, 8, 4, 6, 198, 162]

User Birdy
by
4.8k points

1 Answer

3 votes

Answer:

// Program in Java

import java.util.Arrays;

import java.util.Scanner;

public class Integers {

public static void main(String[] args) {

Scanner input = new Scanner (System.in);

int [] numbers = new int [10];

for(int i=0;i<numbers.length;i++)

{

System.out.print("Enter a number:");

numbers[i]=input.nextInt();

}

printReverse(numbers);

System.out.println("The highest number is "+getLargest(numbers));

System.out.println("The array with two times the numbers: "+ Arrays.toString(computeTwice(numbers)));

input.close();

}

public static void printReverse(int [] numbers)

{

int [] revNumbers = new int[numbers.length];

for(int i = numbers.length -1; i >= 0; i--)

{

revNumbers[numbers.length - 1 -i] = numbers[i];

}

System.out.println("Here are your numbers in reverse: "+Arrays.toString(revNumbers));

}

private static int getLargest(int[] numbers) {

int max = numbers[0];

for(int i=1;i<numbers.length;i++) {

max = Math.max(numbers[i],max);

}

return max;

}

public static int[] computeTwice(int[] numbers) {

for(int i = 0; i < numbers.length; i++)

{

numbers[i] = numbers[i] * 2;

}

return numbers;

}

}

Step-by-step explanation:

  • Initialize an array and loop through it to get values from the user.
  • Call the printReverse method by passing the array as an argument.
  • Inside the printReverse method, run the loop in reverse order.
  • Call the getLargest method to print the highest number that uses Math.max function.
  • Call the computeTwice method to print the array with 2 times the original number in the array.

User CYee
by
5.4k points