Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am having difficulties figuring out how to write a program that follows the fo

ID: 3714424 • Letter: I

Question

I am having difficulties figuring out how to write a program that follows the following points:

1. Your function main must read the order and data set size (n) from the keyboard. Then call functions that read the array data from the keyboard and functions that write the data to the keyboard.
2. Function main will create the appropriate array to store integers read from the keyboard.
3. All error handling must be done in main.
4. Define and use a function that reads integers from the keyboard into an array.
5. Define and use a function that writes integers from an array to the screen in forward order.
6. Define and use a function that writes integers from an array to the screen in reverse order.

Explanation / Answer

import java.util.Scanner;
public class ArrayOrderPrint {
   
    public static int[] readArrayFromKeyBoard(int size){
        int[] array = new int[size];
        Scanner scan = new Scanner(System.in);
        for(int i=0;i<size;i++){
            System.out.print("Enter Array element " + (i + 1) +": ");
            array[i] = scan.nextInt();
        }
       
        return array;
    }
   
    public static void printArrayForward(int[] array){
        for(int i=0;i<array.length;i++){
            System.out.print( array[i] + " ");
        }
    }
   
    public static void printArrayReverse(int[] array){
        for(int i=array.length-1;i>=0;i--){
            System.out.print( array[i] + " ");
        }
    }
   
    public static void main(String args[]) {
        String order = "";
        int dataSetSize = 0;
        boolean flag = true;
        do{
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter Data set size: ");
        dataSetSize = scan.nextInt();
        System.out.print("Enter Order R for Reverse and F for Forward: ");
        order = scan.next();
        if(dataSetSize >= 0 && (order.equals("F") || order.equals("R"))){
            flag = false;
        }else{
            System.out.println("Enter correct input");
        }
       
        }while(flag);

        int[] inpArray = readArrayFromKeyBoard(dataSetSize);
        if(order.equals("F"))
            printArrayForward(inpArray);
        else if(order.equals("R"))
            printArrayReverse(inpArray);
    }
}

Output:
Enter Data set size: 4
Enter Order R for Reverse and F for Forward: R
Enter Array element 1: 1
Enter Array element 2: 2
Enter Array element 3: 3
Enter Array element 4: 4
4 3 2 1