Question

Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations...

Programming Assignment #7

(Recursion)

This assignment is to write some methods that perform simple array operations recursively.

Specifically, you will write the bodies for the recursive methods of the ArrayRecursion class, available on the class web page.

No credit will be given if any changes are made to ArrayRecursion.java, other than completing the method bodies

Note that the public methods of ArrayRecursioncontains(), getIndexOfSmallest(), and sort() – cannot be recursive because they have no parameters. Each of these methods calls a private recursive “helper” method that does the work recursively. You will write the bodies of these recursive helper methods.

Remember that recursion is not merely another way to implement a loop. Recursive algorithms always have one or more “base” or “trivial” cases that are solved nonrecursively, and each recursive call must be made for a “reduced case” of the problem that gets closer to the trivial case.

To receive credit for a method, you must implement the algorithms given below

No credit will be given for any method that uses a loop. Loops are not necessary and if you use one you are missing the point of the assignment

  1. recursiveGetIndexOfSmallest - return the index of the smallest value in the array, using this algorithm:

Find the smallest value in that portion of the array containing all but the last element. Compare that value to the value in the last element and return the index of the smaller of the two.

  1. recursiveSort – sort the array in descending order, using the selection sort algorithm and the recursiveGetIndexOfSmallest method you wrote in 1.

Find the smallest value in the array and swap it with the last element. Then, repeat the process for that portion of the array containing all but the last element.

  1. recursiveContains – return a boolean indicating whether a given int is on the list

See any of our recursion examples using arrays for hints

Due Date: Thursday, December 5th

CODE:

/*  File: ArrayRecursion.java
 *
 *  Programmer: your name
 *
 */

import java.util.Random;
import javax.swing.JOptionPane;

/**
 * A class that performs some simple array operations recursively
 * @author Greg
 */
public class ArrayRecursion
{
   // instance var's
   private int[] list ;       // array of ints
   private int size ;         // number of elements

   /**
    * Create an ArrayRecursion object. 
    * Creates an array with between 10 and 15 elements, and fills it with
    * random positive 2-digit ints
    */
   public ArrayRecursion()
   {
      Random r = new Random();
      size = r.nextInt(6) + 10;
      list = new int[size];

      for (int i = 0; i < size; i++)
      {
         list[i] = r.nextInt(90) + 10;
      }
   }

   /**
    * Return the list as a string
    * @return a string containing all ints stored in list
    */
   public String toString()
   {
      String out = "";
      for (int i = 0; i < size; i++)
      {
         out += list[i] + "  ";
      }
      return out + "\n";
   }

   /**
    * Returns the index of the smallest value in the array.
    * @return the index of the smallest value in the array
    */
   public int getIndexOfSmallest()
   {
      return recursiveGetIndexOfSmallest(list, size);
   }

   // recursive "helper" method to return index of smallest value
   // called by public method getIndexOfSmallest()
   private int recursiveGetIndexOfSmallest(int[] list, int count)
   {
      return -999;   // bogus value to enable program skeleton to run
   }

   /**
    * Sort the array in descending order using the selection sort
    */
   public void sort()
   {
      recursiveSort(list, size);
   }

   // recursive "helper" method to sort the array
   // called by public method sort()
   private void recursiveSort(int[] list, int count)
   {
   }
   
   /**
    * Indicates whether a given int is on the list
    * @param target the int to search for
    * @return true if target is on the list, false if not
    */
   public boolean contains (int target)
   {
      return recursiveContains(list,size,target) ;
   }
   
   // recursive "helper" method to search the array
   // called by public method contains()
   private boolean recursiveContains(int[] list, int count, int target)
   {
     return false ;   // bogus return value to enable program skeleton to run
   }   
   
   public static void main(String[] args)
   {
      ArrayRecursion list = new ArrayRecursion();

      System.out.println("\nOriginal:  " + list);

      System.out.println("Smallest value is at index: "
              + list.getIndexOfSmallest());
      list.sort();
      System.out.println("\nSorted:    " + list);
     
      String target = JOptionPane.showInputDialog("Number to search for?") ;
      int searchee = Integer.parseInt(target) ;
      
      if (list.contains(searchee))
         System.out.println(searchee + " is on the list");
      else
         System.out.println(searchee + " is not on the list");
   }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOURCE CODE IN JAVA:

/* File: ArrayRecursion.java

*

* Programmer: your name

*

*/

import java.util.Random;

import javax.swing.JOptionPane;

/**

* A class that performs some simple array operations recursively

* @author Greg

*/

public class ArrayRecursion

{

// instance var's

private int[] list ; // array of ints

private int size ; // number of elements

/**

* Create an ArrayRecursion object.

* Creates an array with between 10 and 15 elements, and fills it with

* random positive 2-digit ints

*/

public ArrayRecursion()

{

Random r = new Random();

size = r.nextInt(6) + 10;

list = new int[size];

for (int i = 0; i < size; i++)

{

list[i] = r.nextInt(90) + 10;

}

}

/**

* Return the list as a string

* @return a string containing all ints stored in list

*/

public String toString()

{

String out = "";

for (int i = 0; i < size; i++)

{

out += list[i] + " ";

}

return out + "\n";

}

/**

* Returns the index of the smallest value in the array.

* @return the index of the smallest value in the array

*/

public int getIndexOfSmallest()

{

return recursiveGetIndexOfSmallest(list,size);

}

// recursive "helper" method to return index of smallest value

// called by public method getIndexOfSmallest()

private int recursiveGetIndexOfSmallest(int[] list, int count)

{

if(count==1)

return 0;

else

{

int min=recursiveGetIndexOfSmallest(list,count-1);

if(list[count-1]<list[min])

return count-1;

else

return min;

}

}

/**

* Sort the array in descending order using the selection sort

*/

public void sort()

{

recursiveSort(list, size);

}

// recursive "helper" method to sort the array

// called by public method sort()

private void recursiveSort(int[] list, int count)

{

if(count>1)

{

int min=recursiveGetIndexOfSmallest(list,count);

int temp=list[count-1];

list[count-1]=list[min];

list[min]=temp;

recursiveSort(list,count-1);

}

}

/**

* Indicates whether a given int is on the list

* @param target the int to search for

* @return true if target is on the list, false if not

*/

public boolean contains (int target)

{

return recursiveContains(list,size,target) ;

}

// recursive "helper" method to search the array

// called by public method contains()

private boolean recursiveContains(int[] list, int count, int target)

{

if(count==0)

return false;

else

{

if(list[count-1]==target)

return true;

else

return recursiveContains(list,count-1,target);

}

}

public static void main(String[] args)

{

ArrayRecursion list = new ArrayRecursion();

System.out.println("\nOriginal: " + list);

System.out.println("Smallest value is at index: "

+ list.getIndexOfSmallest());

list.sort();

System.out.println("\nSorted: " + list);

String target = JOptionPane.showInputDialog("Number to search for?") ;

int searchee = Integer.parseInt(target) ;

if (list.contains(searchee))

System.out.println(searchee + " is on the list");

else

System.out.println(searchee + " is not on the list");

}

}

OUTPUT:

33 92 89 34 54 27 29 Original: 87 65 40 23 26 26 Smallest value is at index: 3 Sorted: 92 89 87 6554_40 34 33 29 27 26 26 23

Add a comment
Know the answer?
Add Answer to:
Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a method called printReverse() that takes a string and uses recursion to print the contents...

    Write a method called printReverse() that takes a string and uses recursion to print the contents of the string in reverse order. The string itself should not be reversed; it must be left in its original form. The method has the following header:   void printReverse(String s, int i) where s is a reference to the string, and i is an integer parameter that you may use as you see fit. You do not need to code up this method as...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using...

    JAVA Recursion: For this assignment, you will be working with various methods to manipulate strings using recursion. The method signatures are included in the starter code below, with a more detailed explanation of what function the method should perform. You will be writing the following methods: stringClean() palindromeChecker() reverseString() totalWord() permutation() You will be using tools in the String class like .substring(), .charAt(), and .length() in all of these methods, so be careful with indices. If you get stuck, think...

  • In the USIntsArrayList Class, implement the USIntsArrayListInterface Interface (which is really just the UnSortedInts Class without...

    In the USIntsArrayList Class, implement the USIntsArrayListInterface Interface (which is really just the UnSortedInts Class without the logic. All you are doing in this class is providing the same functionality as UnSortedInts, but using an ArrayList to hold the data. Document appropriately Thank you, package arrayalgorithms; import java.util.ArrayList; /** * Title UnSorted Ints stored in an Array List * Description: Implement all the functionality of the UnSortedInts * class using an ArrayList * @author Khalil Tantouri */ public class USIntsArrayList...

  • This Individual Assignment is a set of three problems. The first is a recursion "warm up"...

    This Individual Assignment is a set of three problems. The first is a recursion "warm up" exercise, and the second two are QuickSort variations. The "warm up" should be implemented as a static method in your main App class and the second two will use additional classes (as instructed below). All three problems should be included in the same NetBeans project (exported to ZIP as usual). ----------------- All of your classes should be properly encapsulated, follow all proper coding conventions...

  • PLEASE EDIT THE LAST 3 METHODS WHERE I PUT STARS AND DO WHAT THE COMMENTS SAY....

    PLEASE EDIT THE LAST 3 METHODS WHERE I PUT STARS AND DO WHAT THE COMMENTS SAY. THE CODE IS IN JAVA PROGRAMMING LANGUAGE. import java.util.AbstractList; import java.util.List; import java.util.RandomAccess; import java.lang.RuntimeException; import java.util.Arrays; public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess {    protected Object[] data; protected int size; public int size() {     return size; }    private void rangeCheck(int index) {     if (index < 0 || index >= size) throw new IndexOutOfBoundsException(""); }    @SuppressWarnings("unchecked") private E...

  • Develop a Generic String List (GSL). NOTE: I have done this lab but someting is wrong...

    Develop a Generic String List (GSL). NOTE: I have done this lab but someting is wrong here is what i was told that was needed. Ill provide my code at the very end. Here is what is missing : Here is my code: public class GSL { private String arr[]; private int size; public GSL() {     arr = new String[10];     size = 0; } public int size() {     return size; } public void add(String value) {    ...

  • Write java class “BinarySearch” that uses recursion to find the index of a target value in...

    Write java class “BinarySearch” that uses recursion to find the index of a target value in an ascending sorted array. If not found, the result is -1. The array has to be unsorted before you sort it. Use “ BinarySearchDriver.java” to drive the “BinarySearch” class /************************************************************* * BinarySearchDriver.java * Student Name * *. *************************************************************/ public class BinarySearchDriver { public static void main(String[] args) {     int[] array = new int[] {55, 88, 33, 5, 8, 12, 16, 23, 45}; /unsorted...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT