Want more features on Pastebin? Sign Up, it's FREE!
Guest

blabla

By: a guest on Nov 14th, 2012  |  syntax: Java  |  size: 5.53 KB  |  views: 150  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.  * Name: Kristina Brend
  3.  * Login: cs11fiv
  4.  * Date: November, 14th, 2012
  5.  * File: ReverseRecurse.java
  6.  * Sources of Help: TA's in lab
  7.  *
  8.  * This is a program that reads user input and makes an array of values, the
  9.  * size is specified by the user and this program reverses the elements in the
  10.  * array through two different recursive methods.
  11.  */
  12.  
  13. import java.util.*;
  14.  
  15. /*
  16.  *Class name: ReverseRecurse
  17.  *Purpose: This is class reverses an array based on user input
  18.  * */
  19.  
  20. public class ReverseRecurse
  21. {
  22.    //Instance variables
  23.    int[] array;
  24.    int[] arrayCopy;
  25.    int[] arrayAdjusted;
  26.  
  27.    /*
  28.    * Name: initArray
  29.    * Purpose: Scan's input from the user en uses the size to make an array of
  30.    * that size, and the numbers gets put into the array
  31.    * Parameters:
  32.    * Return: The array made from user input
  33.    */
  34.  
  35.    public int[] initArray()
  36.    {
  37.  
  38.  
  39.     //asks user for input
  40.     System.out.println ("Maximum number of integers you wish to enter?");
  41.     Scanner input = new Scanner (System.in);
  42.  
  43.     int numOfInts =0;
  44.  
  45.     //checks if there is an integer entered
  46.     if (input.hasNextInt())
  47.     {
  48.  
  49.      numOfInts = input.nextInt();
  50.  
  51.  
  52.      //checks for < = 0 condition
  53.         while(numOfInts <= 0)
  54.         {
  55.         System.out.println("You must enter a value > 0, try again.");
  56.         numOfInts = input.nextInt();
  57.         }
  58.  
  59.    
  60.      }
  61.  
  62.       //program stops with the system exit
  63.      else
  64.      {
  65.      // System.out.println("Empty Array");
  66.      System.exit (1);
  67.      }
  68.  
  69.       // input.hasNextInt();
  70.       // input.nextInt();
  71.  
  72.       //creates an array of the size the user decides
  73.       array = new int[numOfInts];
  74.  
  75.       System.out.println("Enter up to " +numOfInts +" integers");
  76.  
  77. /*      //stores user inputted values into the array
  78.       if (input.hasNextInt())
  79.       {
  80.          for (int index =0; index < numOfInts; index++)
  81.          {
  82.              array[index] = input.nextInt();
  83.          }
  84.       }
  85.       else if(not a next int)
  86.      {
  87.       numOfInts = index + 1
  88.       array.setSize(numofints)
  89.       }
  90.       else
  91.       {
  92.       // System.out.println("Empty Array");
  93.  
  94.       //checks condition if non int is entered and makes an array of length 0
  95.       int indexx =0;
  96.       arrayCopy = new int[indexx];
  97.       System.arraycopy( array, 0, arrayCopy, 0 , indexx);
  98.       array = arrayCopy;
  99.       }  */
  100.    int arraySize = 0;
  101.  
  102.    while(arraySize < numOfInts && input.hasNextInt())
  103.    {
  104.       array[arraySize] = input.nextInt();
  105.       arraySize++;
  106.    }
  107.  
  108.    if(arraySize != numOfInts)
  109.    {
  110.       arrayAdjusted = new int[arraySize];
  111.       System.arraycopy(array, 0, arrayAdjusted, 0, arrayAdjusted.length);
  112.       return arrayAdjusted;
  113.    }
  114.    else
  115.    {
  116.       int index = 0;
  117.       arrayCopy = new int[index];
  118.       System.arraycopy( array, 0, arrayCopy, 0 , index);
  119.       array[arraySize]
  120.    }
  121.    
  122.    return array;
  123.  
  124.   }
  125.  
  126.    /*
  127.    * Name: printArray
  128.    * Purpose: Goes through the array and prints out each element on a line with
  129.    * space in between. Prints Empty array if theres no elements in the array.
  130.    * Parameters: Array of numbers, ints
  131.    * Return: void.
  132.    */
  133.  
  134.    public void printArray(int[] array)
  135.    {
  136.      if(array.length > 0)
  137.      {        
  138.         for(int index = 0; index < array.length; index++)
  139.         {
  140.            System.out.print(array[index] + " ");
  141.         }
  142.         System.out.println("");
  143.      }
  144.      else
  145.      {
  146.      System.out.println("Empty Array");
  147.      }
  148.    }
  149.  
  150.    /*
  151.    * Name: reverse
  152.    * Purpose: Uses the array, nad the lowest and the highest index to swap
  153.    * the first and last element in the array directly. Then we call the method
  154.    * again on the rest of the array.
  155.    * Parameters: int[] originalArray, int low, int high
  156.    * Return: void.
  157.    */
  158.  
  159.    public void reverse(int[] originalArray,int low, int high)
  160.    {
  161.       if(high == low)
  162.       {
  163.          originalArray[low] = originalArray[high];
  164.       }
  165.       else if((low + 1) == high)
  166.       {
  167.          int temp;
  168.          temp = originalArray[low];
  169.          originalArray[low] = originalArray[high];
  170.          originalArray[high] = temp;
  171.       }
  172.       else if(originalArray.length == 0)
  173.       {
  174.    
  175.       }
  176.       else
  177.       {
  178.          int temp;
  179.          temp = originalArray[low];
  180.          originalArray[low] = originalArray[high];
  181.          originalArray[high] = temp;
  182.          reverse(originalArray, ++low, --high);
  183.       }
  184.    }
  185.  
  186.  
  187.    public int[] reverse(int[] originalArray){
  188.    //new array with swapped values created with same length as original
  189.    int newArraySwap[];
  190.    newArraySwap = new int[originalArray.length];
  191.  
  192.    //new int array created that holds the remainder of the values
  193.    int newArrayHolder[];
  194.  
  195.  
  196.    //if the swapped array has length 1, it returns the original array
  197.    if (newArraySwap.length ==1)
  198.    {
  199.    return originalArray;
  200.    }
  201.  
  202.  
  203.    //if the new array has lenght 0, the array is returned
  204.    else if (newArraySwap.length == 0)
  205.    {
  206.    return newArraySwap;
  207.    }
  208.  
  209.  
  210.    //calls the method again till the array is properly swapped
  211.    else
  212.    {
  213.  
  214.    newArraySwap[0] = originalArray[originalArray.length-1];
  215.    newArraySwap[originalArray.length-1] = originalArray[0];
  216.  
  217.  
  218.    int newArrayMiddle[];
  219.    newArrayMiddle = new int[originalArray.length -2];
  220.  
  221.    System.arraycopy(originalArray, 1, newArrayMiddle, 0,
  222.    newArrayMiddle.length);
  223.  
  224.  
  225.    newArrayHolder = reverse(newArrayMiddle);
  226.    System.arraycopy( newArrayHolder, 0, newArraySwap, 1,
  227.    newArrayHolder.length);
  228.    }
  229.  
  230.    return newArraySwap;
  231.    }
  232.    
  233.  
  234. }
clone this paste RAW Paste Data