-4

The Java assignment is to Prompt user for ten integers which are stored in an array, call a method passing the array and the method will display the values in both the original and reverse order.

public class Module7 {

public static void main(String[] args) {

// prompt the user
System.out.println("Welcome to **yourNameHere**\'s 3 in 1 Programming Assignment (Arrays) for Module 7\n");

// create Scanner Object
Scanner input = new Scanner(System.in);

// loop until break
while(true){

    // new lines for readability
    System.out.print("\n");

    // Prompt user for choices
    System.out.print("\nEnter 1 Random Array \nEnter 2 Reverse Numbers\nEnter 3 Identical Arrays\nAny other input will exit");
    int choice = input.nextInt();
    // Consume newline left-over
    input.nextLine();

    // first feature
    if(choice == 1){
        System.out.println("You Selected Random Array");

        // call the method of feature 1
        feature1();

    }
    else if(choice == 2){

        // Prompt user
        System.out.println("You Selected Reverse Numbers\n");

        feature2();

    }
    else if(choice == 3){

        // Prompt user
        System.out.println("You Selected Identical Arrays !");
        // call the method of feature 3 providing the message, return value is the Letters message
        feature3();

    }
    else{

        // not 1 , 2 or 3
        System.out.println("User didn't select 1,2 or 3. Exiting Program\n");

        // release object
        input.close();

        // exit while with break statement
        break;
    }
} // end of while

} // end main

// create feature1() method here no problems here, this one works

private static void feature1(){

        int[] randomArray = new int[100];
        for (int i = 0; i< randomArray.length; i++)
            randomArray[i] = (int) (Math.random()*100);



        for (int i=0; i<randomArray.length; i++) {
            double currentMin = randomArray[i];
            int currentMinIndex = i;


            //not needed for just sorting
            for (int j=i+1; j<randomArray.length;j++) {
                if (currentMin > randomArray[j]) {
                    currentMin = randomArray[j];
                    currentMinIndex = j;
                }
            }

            //swap list[i] with list[currentMinIndex] if necessary
            if (currentMinIndex !=i) {
                randomArray[currentMinIndex] = randomArray[i];
                randomArray[i] = (int) currentMin;
            }
        }

        for (int i=0; i<randomArray.length; i++) {
              // print randomArray values
              if (i % 10 == 0 && i > 0) {
                  System.out.println();

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



            }

    }

//create feature2() method here I can't seem to get it to look like the example here. I can't get the numbers to be able to be entered one at a time with a space between, then show them reversed. It only works if you enter all of 10 numbers consecutively with no spaces.

private static void feature2(){

        System.out.print("Please enter 10 numbers ");
        java.util.Scanner input = new java.util.Scanner(System.in);
        double[] numbers = new double[10];

        System.out.print("\nEntered: ");
        for (int i = 0; i < numbers.length; i++) {

        int number = input.nextInt();
        System.out.print("Reversed: ");
        reverse(number);
        }
    }
    public static void reverse(int number) {
        while (number !=0) {
            int remainder = number % 10;
            System.out.print(remainder + " ");
            number = number/10;
        }


    }

//create feature3() here. This is where my major problem is. I am terrible with arrays and don't even really know where to begin

 private static void feature3()

`The Java assignment is to Prompt user for ten integers which are stored in an array, call a method passing the array and the method will display the values in both the original and reverse order.

I have tried every which way I know (from reading my class book and watching the lecture videos...) but I can not figure out how to write this code. My output is supposed to look like below:

Output sample

(EDIT) Sorry, will post what I have as soon as I get back to my home computer...this IS homework by the way. I am just terrible with arrays and understanding them. Is there a site that teaches "arrays for dummies"?

Jeremy R
  • 5
  • 2

5 Answers5

1

Try this..

class Assignment{

private Integer[] reverse(Integer[] passtheArray){

//Use Your own logic Whatever you Want to implement and return Your array.
 return null;
}
public static void main(String[] args){
Integer abc[] = new Integer[] {10, 15, 20, 25, 30};
Assignment obj = new Assignment();
obj.reverse(abc);
}
}
Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
1

Since you have not supplied any code I will assume you have not gotten very far on the project. Take a look at looping through Arrays.

for a simple solution you can loop backwards through an array by setting an array to

ArrayUtils.reverse(int[] array)

However, I recommend you do some research on how to reverse through an array using a for loop since you don't seem to have a strong grasp of them yet.

If you post some sample code then we can give you some more direct tips.

Hunter
  • 110
  • 1
  • 9
  • This answer is the same as the other... Why are you recommending something not part of native Java? – OneCricketeer Feb 23 '16 at 05:12
  • Because you don't necessarily need to understand how to do something if you know it works and maybe by introducing more abstract ways of going about it someone will learn something new. – Hunter Feb 23 '16 at 05:14
  • 1
    I disagree as this is homework. Knowing libraries exist for trivial problems is nice and useful post-education, but it doesn't teach people how to write code – OneCricketeer Feb 23 '16 at 05:18
1

This is 1 way to do it

public class swapnumber {

    public static void main(String[] args)

    {

         Scanner s = new Scanner(System.in);
         System.out.print("Enter number of elements you want in array:");
        int n = s.nextInt();
         int a[] = new int[n];
         System.out.println("Enter all the elements:");
         for (int i = 0; i < n; i++) 
         {
             a[i] = s.nextInt();
         }



        swapNumber(a);

    }

    private static void swapNumber(int a[]) {

        System.out.println("Original array" + Arrays.toString(a));
        for(int i = 0; i < a.length / 2; i++)
        {
            int temp = a[i];
            a[i] = a[a.length - i - 1];
            a[a.length - i - 1] = temp;
        }

        System.out.println("Reversed array" + Arrays.toString(a));
    }
}
sanky jain
  • 873
  • 1
  • 6
  • 14
1

Assuming you already have all of the user input values stored in the array, you would want to create two for loops; one that iterates from the beginning of the array to the end, and the other that iterates from the end of the array to the beginning.

for(int i = 0; i < array.length(); ++i) {
   System.out.print(array[i]); //or put that in a string to output later
}


for (int j = array.length() - 1; j >= 0; ++j) {
   System.out.print(array[j]); //or, again, put it in a string
}

Its key to know that arrays in Java start at index 0 and end at index equal to the length of given array minus 1, which is why the count-down loop starts and ends where it does.

Hope that clears part of the issues up!

RJM
  • 73
  • 4
0

Use this, how to reverse an array has been previously mentioned here.

ArrayUtils.reverse(int[] array)
Community
  • 1
  • 1
Meaniegy
  • 62
  • 1
  • 7