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
/** * 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
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
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
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.