# Recursion Guidance

• December 2nd, 2011, 05:18 PM
Mission_Control
Recursion Guidance, Help Requested
So my professor assigned two problems to the class yesterday to be done recursively:

The first is to find the maximum number in an array. We had predefined methods and a tester, all we had to do was fill in the methods for the class DataSet. This problem appears to work correctly but I would like to know if there is anything wrong with the way that I implemented it for the purpose of doing it using recursion.

Tester
Code :

```/** * A tester class for the recursive maximum */ public class DataTester { public static void main (String[] args) { int[]values = {1, 10, 100, -1, -10, -100, 100, 0};   //OTHER TESTING VALUES //int[]values = {2, 5, 6, 3, 1}; //int[]values = {9, 8, 7, 6, 5}; //int[]values = {9, 2, 4, 9, 0};   DataSet data = new DataSet (values, 0, values.length -1); System.out.println("Max :: " + data.getMaximum()); System.out.println("Expected :: 100"); } }```

Implementation
Code :

```  public class DataSet { private int[] values; private int first; private int last;   /** * Constructor that will set the array values, and where the array begins and ends * @param values * @param first * @param last */ public DataSet (int[] values, int first, int last) { this.values = values; this.first = first; this.last= last; }   /** * This finds the maximum values in an array using recursion * @return */ public int getMaximum() {   //BASE LINE if (first == last) { return values[first]; }   else if (first < last) { DataSet d = new DataSet(values, first, last-1); int temp = d.getMaximum(); if (temp > values[last]) {return temp; } else {return values[last]; } }   return first; } }```

The second problem is to find the number of handshakes that will be made if each person in a room shakes hands with each other person exactly one time. So if two people were in the room, there would be a total of 1 handshake.

I have this working but I'm pretty sure that my implementation is incorrect because I have my handshakes variable as an instance variable. I'm pretty sure that it should be within my "getNumHandshakes()" method but I can't seem to get correct results when I do it that way.

Tester
Code :

```  public class RoomTester { public static void main (String [] args) { Room a = new Room(0); System.out.println("A :: People :: 0 :: HandShakes :: " + a.getNumHandshakes() + " :: Expected :: 0");   Room b = new Room(1); System.out.println("B :: People :: 1 :: HandShakes :: " + b.getNumHandshakes() + " :: Expected :: 0");   Room c = new Room(2); System.out.println("C :: People :: 2 :: HandShakes :: " + c.getNumHandshakes() + " :: Expected :: 1");   Room d = new Room(3); System.out.println("D :: People :: 3 :: HandShakes :: " + d.getNumHandshakes() + " :: Expected :: 3");   Room e = new Room(4); System.out.println("E :: People :: 4 :: HandShakes :: " + e.getNumHandshakes() + " :: Expected :: 6");   Room f = new Room(5); System.out.println("F :: People :: 5 :: HandShakes :: " + f.getNumHandshakes() + " :: Expected :: 10");   Room g = new Room(6); System.out.println("G :: People :: 6 :: HandShakes :: " + g.getNumHandshakes() + " :: Expected :: 15"); }   }```

Implementation
Code :

```  public class Room { private int roomPeople = 0; private int handshakes = 0;   public Room (int people) { roomPeople = people; }   public Room() { roomPeople = 3; }   public int getPeople() { return roomPeople; }   public void setPeople (int p) { roomPeople = p; }   public int getNumHandshakes() {   //No people, no handshakes if (roomPeople == 0) {return 0;}   //One person isn't going to shake their own hand else if (roomPeople == 1) {return 0;}   //Two people = one handshake else if (roomPeople >= 2) { handshakes = handshakes + roomPeople-1; roomPeople--; getNumHandshakes();   return (handshakes); } else { return -1234; } } }```

I'm new to recursion and I can't seem to break myself of thinking iteratively because I want to do these using simple loops but alas that isn't the assignment.

Can you tell me in what direction I need to go to do the second problem correctly using recursion.
Also, any general tips on programming recursively would be very helpful.
• December 2nd, 2011, 08:08 PM
jcloud
Re: Recursion Guidance
1, I try to understsnd your question 1 , but I don`t understand what you say "implemented it" means

2, the handshakes variable should as an instance variable , beacuse you Recursive your method
when you set your handshankes variable in your "getNumHandshakes()" method , handshakes variable will be init in each Recursive
• December 2nd, 2011, 08:17 PM
Mission_Control
Re: Recursion Guidance
When I say ask if there are any problems with the way I "implemented it" I mean are there any problems with the way that the code is written. Based on the requirement that it has to use recursion, is it correct or have I written my code wrong?

I'm not sure I understood what you said with the second point. Are you saying that my variable handshakes should be created and initialized in getNumHandshakes() or that it should be an instance variable under public class room like it already is?
• December 2nd, 2011, 08:35 PM
jcloud
Re: Recursion Guidance
you said " I'm pretty sure that it should be within my "getNumHandshakes()" method but I can't seem to get correct results when I do it that way"
I mean when your handshankes variable in your "getNumHandshakes()" method , handshakes variable will be initialized every time, so you can`t get correct results

handshakes should be an instance variable under public class room

by the way, my english is very poor, I`m not sure I understand what you mean , but I am happy to answer your questions if I can