I've this question due on Tuesday. But I need the code by tomorrow, Monday.
The language is Java.
Ask the user to enter the size of the array. Then fill the array with the multiple of 3 (start at 3.5). (Hint: use for loop)

Display it like Exercise A. (font in red, entered by the user). Name it SecondArray. Feel free to name it anyway you like.

Expected output:

Please enter the size of the array: 10
(Input validation: Do not let the user enter any number less than 1. Must loop until you get the right number.)

Here is the multiple of 3, starting at 3.5

3.5 , 6.5 , 9.5 , 12.5 , 15.5 , 18.5 , 21.5 , 24.5 , 27.5 , 30.5

Now in reverse order

30.5 , 27.5 , 24.5 , 21.5 , 18.5 , 15.5 , 12.5 , 9.5 , 6.5 , 3.5

Respuesta :

tonb

Here it is. Enjoy.

It appears Brainly doesn't like the import statements, so I had to take them out. Can you figure them out by yourself?

You need System, Scanner and DecimalFormat.

public class ArrayDemo  

{

   private int GetArraySize()

   {

       Scanner reader = new Scanner(System.in);

       int arraySize;

       do {

           System.out.print("Please enter the size of the array: ");

           arraySize = reader.nextInt();

           if (arraySize < 1) {

               System.out.println("Enter a number larger than 0.");

           }

       } while (arraySize < 1);

       return arraySize;

   }

   

  private void FillArray(double[] array, double start, double factor)

  {

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

      {

          array[i] = start+factor*i;

      }

  }

   

  private void PrintArray(double[] array)

  {

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

      {

          System.out.print(array[i] + " ");

      }

      System.out.println();

  }

   

  private void ReverseArray(double[] array)

  {

      for(int i=0; i<array.length/2; i++){  

          double temp = array[i];  

          array[i] = array[array.length -i -1];  

          array[array.length -i -1] = temp;  

      }

  }

   

  public void RunDemo(double factor, double start)

  {

      int arraySize = GetArraySize();

      double[] demoArray = new double[arraySize];

      FillArray(demoArray, start, factor);

      DecimalFormat format = new DecimalFormat("0.#");

      System.out.printf("Here is the multiple of %s, starting at %s\n", format.format(factor), format.format(start));

      PrintArray(demoArray);        

      System.out.println("Now in reverse order");

      ReverseArray(demoArray);

      PrintArray(demoArray);

  }

   

  public static void main(String[] args)  

  {

      double factor = 3.0;

      double start = 3.5;

       

      ArrayDemo demo = new ArrayDemo();

      demo.RunDemo(factor, start);        

  }

}