Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Recursion Guidance

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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
    /**
    * 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.
    Last edited by Mission_Control; December 2nd, 2011 at 06:57 PM.


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Location
    china
    Posts
    12
    My Mood
    Sad
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default 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

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  4. #4
    Junior Member
    Join Date
    Nov 2011
    Location
    china
    Posts
    12
    My Mood
    Sad
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default 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

Similar Threads

  1. HIBERNATE & JPA GUIDANCE NEEDED
    By Rituparna in forum JDBC & Databases
    Replies: 0
    Last Post: September 3rd, 2011, 03:54 AM
  2. Some guidance with a program please
    By derekxec in forum Java Theory & Questions
    Replies: 3
    Last Post: June 11th, 2011, 05:52 AM
  3. Looking for guidance and instructions
    By coyboss in forum Java Theory & Questions
    Replies: 4
    Last Post: February 12th, 2011, 09:57 AM
  4. Recursion
    By javapenguin in forum Algorithms & Recursion
    Replies: 12
    Last Post: October 18th, 2010, 03:42 PM
  5. [SOLVED] Medication of ArrayDemo.java to include iterative menu
    By xyldon27 in forum Loops & Control Statements
    Replies: 8
    Last Post: June 30th, 2009, 02:10 AM

Tags for this Thread