1 import java.util.Scanner;
2
3 /**
4 This program reads, scales and reverses a sequence of numbers.
5 */
6 public class Reverse
7 {
8 public static void main(String[] args)
9 {
10 double[] values = readInputs(5);
11 multiply(values, 10);
12 printReversed(values);
13 }
14
15 /**
16 Reads a sequence of floating-point numbers.
17 @param numberOfInputs the number of inputs to read
18 @return an array containing the input values
19 */
20 public static double[] readInputs(int numberOfInputs)
21 {
22 System.out.println("Enter " + numberOfInputs + " numbers: ");
23 Scanner in = new Scanner(System.in);
24 double[] inputs = new double[numberOfInputs];
25 for (int i = 0; i < inputs.length; i++)
26 {
27 inputs[i] = in.nextDouble();
28 }
29 return inputs;
30 }
31
32 /**
33 Multiplies all elements of an array by a factor
34 @param data an array
35 @param factor the value with which element is multiplied
36 */
37 public static void multiply(double[] data, double factor)
38 {
39 for (int i = 0; i < data.length; i++)
40 {
41 data[i] = data[i] * factor;
42 }
43 }
44
45 /**
46 Prints an array in reverse order
47 @param data an array of numbers
48 @return an array that contains the elements of data in reverse order
49 */
50 public static void printReversed(double[] data)
51 {
52 // Traverse the array in reverse order, starting with the last element
53 for (int i = data.length - 1; i >= 0; i--)
54 {
55 System.out.print(data[i] + " ");
56 }
57 System.out.println();
58 }
59 }